點擊屏幕底部^
字符的小格後,滾動動畫將正常啓動。 但做了多次後,當您嘗試向下滾動時,它會堅持一段時間(與您點擊div的次數成比例)。scrollTop Animation後滾動不起作用
有人可以告訴我爲什麼這樣做嗎?
HTML:
<!DOCTYPE html>
<html>
<head>
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
<div id="test">Where to go</div>
<div id="goToTop">^</div>
</body>
</html>
JS:
$(window).scroll(function(){
var st = $(window).scrollTop();
var timeout = setTimeout(function(){
var currentScrollTop = $(window).scrollTop();
if(st == currentScrollTop){
var yPosInAbsolute = window.innerHeight - 100;
$('#goToTop').css({'top': st + yPosInAbsolute, 'right': 100});
$('#goToTop').show();
$('#goToTop').click(function(){
$('html,body').animate({scrollTop: $('#test').offset().top - 50}, 2000);
clearTimeout(timeout);
});
}else{
$('#goToTop').hide();
clearTimeout(timeout);
}
}, 2000);
});
的DEMO
某些時候是超時和動畫時間堆棧。 – frikinside
謝謝你的幫助。 :) – Xlander
[小提琴演示](http://jsfiddle.net/GopsAB/sCaLr/1/)我的嘗試去快速回答的浪費。無論如何嘗試:) –