如何区分手动滚动(通过鼠标滚轮/滚动条)和Javascript/jQuery滚动? 更新:

如何区分手动滚动(通过鼠标滚轮/滚动条)和Javascript/jQuery滚动? 更新:,javascript,jquery,scroll,scrollbar,mousewheel,Javascript,Jquery,Scroll,Scrollbar,Mousewheel,下面是一个演示该问题的示例 更新2: 这要感谢你 基本上,我有以下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

下面是一个演示该问题的示例

更新2:
这要感谢你


基本上,我有以下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 try to scroll to a negative value...
         y=Math.max(y,0);
         // OK, you can scroll now...
         $("html,body").stop().animate({ "scrollTop": y }, 1000);
     }
     return false;
 });
它工作得很好……直到我手动尝试滚动窗口。当滚动条或鼠标滚轮被滚动时,我需要停止当前的滚动动画…但我不知道该怎么做

这可能是我的出发点

$(window).scroll(e){
    if(IsManuallyScrolled(e)){
        $("html,body").stop();
    }
} 
…但我不知道如何编写
是手动克隆的
函数。我已经在Google Chrome的控制台中签出了
e
(事件
对象),并且我认为没有办法区分手动滚动和jQuery的
animate()
滚动

如何区分手动滚动和通过jQuery的
$.fn.animate
函数调用的滚动?

尝试以下功能:

$('body,html').bind('scroll mousedown wheel DOMMouseScroll mousewheel keyup', function(e){
 if ( e.which > 0 || e.type == "mousedown" || e.type == "mousewheel"){
  $("html,body").stop();
 }
})
还有,你看到了吗


更新:现代浏览器现在将其用作事件,因此我已将其包含在上面的代码中。

您可以设置一个变量来指示对动画的调用处于活动状态,然后在滚动处理程序中检查该变量

window.IsAutoScrolling = true;
$("html,body").stop().animate({ "scrollTop": y }, 1000);
// Do something to set IsAutoScrolling = false, when the animation is done.

$(window).scroll(e){  
if(!window.IsAutoScrolling){  
    $("html,body").stop();  
}  

前几天我也遇到过同样的问题,你不应该使用jquery的animate函数,如果你想得到这个结果,你必须使用轮询函数来模拟动画。

我创建了这个类,它应该在调用scrolldown.slow()时提供平滑的滚动

ScrollDown.current=$(window).scrollTop();
ScrollDown.lastValue;
ScrollDown.lastType;
ScrollDown.enabled=true;
ScrollDown.custom=function(value,rate){  //let's say value==='bottom' and rate=10
    if(value==='bottom'){
        value=$(document).height()-$(window).height();
    }
    ScrollDown.current=$(window).scrollTop();
    ScrollDown.lastValue=value;
    (function poll(){
        setTimeout(function(){
            var prev=$(window).scrollTop();  //This is the critical part
            /*I'm saving again the scroll position of the window, remember
            10 ms have passed since the polling has started
            At this rate, if the user will scroll up for down pre!==ScrollDown.current
            And that means I have to stop scrolling.*/
            ScrollDown.current++; //increasing the scroll variable so that it keeps scrolling
            $(window).scrollTop(ScrollDown.current);
            if(ScrollDown.current<ScrollDown.lastValue && ScrollDown.enabled){  
            //ScrollDown.current<ScrollDown.lastValue basically checks if it's reached the bottom
                if(prev!==ScrollDown.current-1){
                /*I'm checking if the user 
                scrolled up or down while the polling has been going on, 
                if the user scrolls up then prev<ScrollDown.current-1, 
                if the user scrolls down then prev>ScrollDown.current-1 
                and at the next poll() the scrolling will stop 
                because ScrollDown.enabled will bet set to false by ScrollDown.stop()*/
                    ScrollDown.stop();
                }
                poll();
            }
        },rate);
    })();
};

ScrollDown.stop=function(){
    ScrollDown.enabled=false;
};

ScrollDown.continue=function(){
    ScrollDown.enabled=true;
    switch (ScrollDown.lastType){
        case "fast":
            ScrollDown.fast(ScrollDown.lastValue);
            break;
        case "normal":
            ScrollDown.normal(ScrollDown.lastValue);
            break;
        case "slow":
            ScrollDown.slow(ScrollDown.lastValue);
            break;
    }
};

ScrollDown.fast=function(value){
    if(!ScrollDown.enabled){
        ScrollDown.continue();
    }else{
        ScrollDown.lastType='fast';
        ScrollDown.custom(value,1);
    }
};
ScrollDown.normal=function(value){
    if(!ScrollDown.enabled){
        ScrollDown.continue();
    }else{
        ScrollDown.lastType='normal';
        ScrollDown.custom(value,10);
    }
};
ScrollDown.slow=function(value){
    if(!ScrollDown.enabled){
        ScrollDown.continue();
    }else{
        ScrollDown.lastType='slow';
        ScrollDown.custom(value,50);
    }
};
function ScrollDown(){}
ScrollDown.current=$(窗口).scrollTop();
ScrollDown.lastValue;
ScrollDown.lastType;
ScrollDown.enabled=true;
custom=function(value,rate){//假设value==='bottom'和rate=10
如果(值=='bottom'){
值=$(文档).height()-$(窗口).height();
}
ScrollDown.current=$(window.scrollTop();
向下滚动。lastValue=value;
(函数poll(){
setTimeout(函数(){
var prev=$(window.scrollTop();//这是关键部分
/*我再次保存窗口的滚动位置,记得吗
自轮询开始以来已过10毫秒
按此速率,如果用户将向上滚动至down pre!==ScrollDown.current
这意味着我必须停止滚动*/
ScrollDown.current++;//增加scroll变量,使其保持滚动
$(窗口).scrollTop(ScrollDown.current);

如果(ScrollDown.currentTHANK!不,我没有看到教程,也谢谢你。这是你的补丁的更新哦,为什么要使用DOMMouseScroll?是否“scroll”!=“DOMMouseScroll”?
DOMMouseScroll
仅适用于Firefox(参考:)更新:添加了“wheel”事件绑定。对于触摸屏设备,您需要将
touchstart
添加到事件列表中,并在事件本身
…|e.type==“touchstart”
中检查它,这将不起作用,因为window.IsAutoScrolling将始终为true,直到滚动停止…此时,我们不再关心窗口是否自动滚动滚动。问题是检测在动画滚动期间调用滚动的内容;jquery或用户。