点击屏幕底部^
字符的小格后,滚动动画将正常启动。 但做了多次后,当您尝试向下滚动时,它会坚持一段时间(与您点击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/)我的尝试去快速回答的浪费。无论如何尝试:) –