26

アップデート:

これは、問題を示すjsbin の例です。

更新 2: fudgeyのおかげで修正されたバージョン
がここにあります。


基本的に、ウィンドウをページ上のアンカーにスクロールする次の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();
    }
} 

IsManuallyScrolled...しかし、関数のコーディング方法がわかりません。eGoogle Chrome のコンソールで(オブジェクト)をチェックアウトしましたが、私eventの知る限り、手動スクロールと jQuery のスクロールを区別する方法はありませんanimate()

手動スクロールと jQuery の$.fn.animate関数を介して呼び出されたスクロールを区別するにはどうすればよいですか?

4

3 に答える 3

39

この機能を試してください:

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

また、このチュートリアルを見ましたか?

更新: 最近のブラウザーはイベントとして"wheel"を使用するようになったため、上記のコードに含めました。

于 2010-05-14T17:10:04.953 に答える
2

数日前に同じ問題がありました。その結果を取得したい場合は、jquery のアニメーション関数を使用しないでください。ポーリング関数を使用してアニメーションをシミュレートする必要があります。 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.slow('bottom')を呼び出すと、手動で上下にスクロールしない限り、ページの下部に到達するまでゆっくりとスクロールが開始され、その後停止します。

于 2016-01-05T05:51:42.840 に答える
-1

animate の呼び出しがアクティブであることを示す変数を設定し、スクロール ハンドラー内でその変数をチェックすることができます。

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();  
}  
于 2010-05-14T17:13:06.540 に答える