我有一段代码可以通过animate
jQuery 东西向上滚动页面,但是为了提高可用性,如果用户以任何方式干扰滚动动作(例如滚轮、抓住滚动条等),那么它将立即停止动作.
一周前我在这里问了一个问题,但在一些测试人员友好地试用了代码后,他们报告说它在基于 Gecko 的浏览器中不起作用。
这是我到目前为止的代码:
$(document).ready(function() {
// scroll to top: check if user / animate is scrolling
var scrollAnimating = false;
jQuery.fx.step.scrollTop = function(E) {
scrollAnimating = true;
E.elem.scrollTop = E.now;
scrollAnimating = false;
};
// go to top (on click)
$('#gototop').click(function() {
$(this).fadeOut(100,function() {
$(this).css({backgroundColor: '#CCC'}).html('Going... (scroll to stop)').fadeIn(100, function() {
$('html,body').animate({scrollTop:0},3000);
})
});
$(window).scroll(function() {
if (!scrollAnimating) {
$('html,body').stop();
$('#gototop').html('Go to top');
};
});
resetNames();
return false;
});
function resetNames() {
setTimeout(function() {
$('#gototop').html('Go to top').css({backgroundColor: '#DDD'});
},3000);
}
});
基本上点击#gototop
它会开始动画滚动到顶部。如果滚动受到干扰,则滚动运动停止并且文本#gototop
被重置。
这段代码有一些问题:有些部分效率低得离谱,并且在基于 Gecko 的浏览器中不起作用(一个大问题)。
我如何让它工作?关于如何提高效率的任何指示?