是否可以判断滚动事件是由浏览器还是由用户完成的?具体地,当使用后退按钮时,浏览器可以跳转到最后已知的滚动位置.如果我绑定滚动事件,我如何判断这是由用户还是浏览器引起的?
$(document).scroll( function(){
//who did this?!
});
Run Code Online (Sandbox Code Playgroud)
我看到三种导致浏览器滚动的情况.
element.scrollTo(x,y).这是一个证明问题的jsbin示例.
基本上,我有以下javascript将窗口滚动到页面上的锚点:
// get anchors with href's that start with "#"
$("a[href^=#]").live("click", function(){
var target = $($(this).attr("href"));
// if the target exists: scroll to it...
if(target[0]){
// If the page isn't long enough to scroll to the target's position
// we want to scroll as much as we can. This part prevents a sudden
// stop when window.scrollTop reaches its maximum.
var y = Math.min(target.offset().top, $(document).height() - $(window).height());
// also, don't …Run Code Online (Sandbox Code Playgroud)