2013-07-19 4 views
3

자동으로 상단으로 스크롤하고 싶지만 사용자가 스크롤을 시작하면 배제됩니다.사용자가 스크롤을 시작하지 않으면 javascript가 자동으로 맨 위로 이동합니다.

현재 내가 가지고있는 것은 스크롤 애니메이션 자체가 스크롤하기 때문에 애니메이션을 조기에 중단합니다. 따라서 스크롤이 발생하면 "스크롤 중지"동작을 트리거합니다.

function stop_scrolling_to_top(){ 
    // stop animation attached to selector 
    $('html, body').stop(); 
} 

// scroll to the top automatically 
$('html, body').animate({ scrollTop: 0}, 1400, "easeOutQuint", function(){ 
    // callback when animation complete 
    do_not_do_when_scrolling(stop_scrolling_to_top); 
}); 

// stop animation if scrolling starts 
do_when_scrolling(stop_scrolling_to_top); 

스크롤이 사람 또는 js에 의해 트리거되는지 확인하는 방법이 있습니까? 전적으로 더 좋은 방법이 있습니까?

+0

'$ (창) .scroll (함수() {CONSOLE.LOG) ('스크롤';});'이 화재는 경우 않습니다 프로그래밍 방식으로 스크롤 하시겠습니까? – Johan

+0

@Johan yessir do – rikAtee

답변

-1

단순히 mousewheel 이벤트를 듣지 않아도됩니다. 해고 된 경우 사용자가 직접 스크롤하려고했습니다. 또한 사용자가 스크롤을 멈추기 위해 어딘가를 클릭하면 클릭 이벤트를들을 수 있습니다 ...

+2

마우스 휠보다 스크롤하는 방법이 더 많습니다. – Johan

+1

위쪽 키, 아래쪽 키, 스크롤 휠, scoroll bar를 마우스로 클릭하거나 비정상적인 방법으로 스크롤 할 수 있습니다. 이는 이벤트를 트리거하는 것보다는 window.scroll 이벤트를 청취하는 이유입니다. – rikAtee

+0

그래 맞습니다. 이 문제에 대한 깔끔한 해결책이 궁금합니다. – Christian

1

잘하면이 도움이됩니다.

이것은 페이지에 스크롤 사용할 수있는 마우스 휠 이벤트 + 키를 수신 (페이지 업/다운 스페이스 바를 arrowkeys 등) :

$(document).keyup(function(e){ 

    if($.inArray(e.which, [33,34,32,38,40]) !== -1) 
     console.log('key'); 
     //potential scroll by key 

}); 

$(document).bind((/Firefox/i.test(navigator.userAgent)) 
           ? 'DOMMouseScroll' 
           : 'mousewheel', function(e){ 

    var evt = window.event || e; 
    evt = evt.originalEvent ? evt.originalEvent : evt;    
    var delta = evt.detail ? evt.detail*(-40) : evt.wheelDelta; 

    if(delta > 0) 
     console.log('mousewheel up'); 
     //scroll up 
    else 
     console.log('mousewheel down'); 
     //scroll down 
}); 

그리고 당신은 '당신'스크롤 것을 확인할 경우 위처럼 활성, 그만 :

if($('html, body').is(':animated')) 
    $('html, body').stop(); 

http://jsfiddle.net/MQQ3F/1/

관련 문제