복붙노트

[JQUERY] JavaScript / JQuery 스크롤에서 수동 스크롤 (마우스 휠 / 스크롤바를 통해)을 구별 할 수 있습니까?

JQUERY

JavaScript / JQuery 스크롤에서 수동 스크롤 (마우스 휠 / 스크롤바를 통해)을 구별 할 수 있습니까?

해결법


  1. 1.이 기능을 시도하십시오.

    이 기능을 시도하십시오.

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

    또한이 튜토리얼이 보였습니까?

    업데이트 : 현대 브라우저는 이제 "바퀴"를 이벤트로 사용하므로 위의 코드에 포함 시켰습니다.


  2. 2.며칠 전에 같은 문제가있었습니다. jQuery의 애니메이션 기능을 사용해서는 안됩니다. 그 결과를 얻으려면 폴링 기능을 사용하여 애니메이션을 시뮬레이션해야합니다. ScrollDown.slow ()가 호출 될 때 부드러운 스크롤 다운을 제공 해야하는이 클래스를 만들었습니다.

    며칠 전에 같은 문제가있었습니다. 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을 호출하는 경우 ( '맨 아래') 수동으로 위 또는 아래로 스크롤하지 않으면 페이지 하단에 도달 할 때까지 천천히 스크롤을 시작하면 멈 춥니 다.


  3. 3.Animate가 활성화되었음을 나타내는 변수를 설정하여 스크롤 핸들러 내의 변수를 확인할 수 있습니다.

    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();  
    }  
    
  4. from https://stackoverflow.com/questions/2834667/how-can-i-differentiate-a-manual-scroll-via-mousewheel-scrollbar-from-a-javasc by cc-by-sa and MIT license