I have a block of code that scrolls the page up via a animate
jQuery thing, but to increase usability, if the user disturbs the scrolling motion in any way (e.g. scrolling wheel, grabbing the scrollbar, etc.) then it will immediately stop the motion.
I asked a question on this a week ago here but after some testers kindly tried out the code they reported that it did not work in Gecko-based browsers.
This is the code I have so far:
$(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);
}
});
Basically on clicking #gototop
it will start to animate the scroll to the top. If the scroll is disturbed the scrolling motion stops and the text for #gototop
is reset.
There are a few problems with this code: some parts are ridiculously inefficient and it doesn't work in Gecko-based browsers (a biggie).
How do I get it to work? Any pointers on how to make it efficient?