Jquery 触发仅在页面加载时在滚动时触发的事件

Jquery 触发仅在页面加载时在滚动时触发的事件,jquery,Jquery,我想触发一个只在滚动时触发的函数,这可能吗 $(document).ready(function() $(document).scroll(function() { // do something here }); // here I want to trigger the scroll function (without scrolling) }); 我尝试了$(document.scroll()未成功: $(document).ready(fun

我想触发一个只在滚动时触发的函数,这可能吗

$(document).ready(function() 
    $(document).scroll(function() {
        // do something here
    });

    // here I want to trigger the scroll function (without scrolling)
});
我尝试了
$(document.scroll()未成功:

$(document).ready(function() 
    $(document).scroll(function() {
        // do something here
    });
    $(document).scroll();
});

将其更改为使用命名函数:

$(document).ready(function() 
    $(document).scroll(onScroll);

    onScroll();
});

function onScroll() {
}

既然该函数已命名,您可以随时自由执行该代码。

您希望浏览器按字面意思滚动,还是希望执行浏览器滚动时将执行的代码?@MichaelPerrenoud后者。@Daan,请尝试将处理程序绑定到
窗口
,而不是
文档
。IIRC,
scroll
事件无法在文档上触发。@FrédéricHamidi这是错误的,文档上的绑定滚动也可以在平板电脑上工作。