Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable normal scrollwheel functionality in jQuery?

Tags:

jquery

Using the mousewheel plugin, I have:

$('html, body').bind("mousewheel", function(objEvent, intDelta){
if (intDelta > 0 && $currentPage != 1){
    $currentPage--;
    $('html, body').animate({scrollTop:$("#page"+$currentPage).offset().top}, 2000);
}
else if (intDelta < 0 && $currentPage != 4){
    $currentPage++;
    $('html, body').animate({scrollTop:$("#page"+$currentPage).offset().top}, 2000);
}
});

Which works fine, but whenever I scroll, it scrolls up or down the page a tick first before doing the animation. Is there any way to disable this? Thanks!

like image 667
user1222728 Avatar asked Jul 31 '26 07:07

user1222728


1 Answers

Just add a

return false;

Before the last brace.

$('html, body').bind("mousewheel", function(objEvent, intDelta){
if (intDelta > 0 && $currentPage != 1){
    $currentPage--;
    $('html, body').animate({scrollTop:$("#page"+$currentPage).offset().top}, 2000);
}
else if (intDelta < 0 && $currentPage != 4){
    $currentPage++;
    $('html, body').animate({scrollTop:$("#page"+$currentPage).offset().top}, 2000);
}
return false;
});

BTW, you should use .on() instead of .bind()

like image 196
mddw Avatar answered Aug 02 '26 01:08

mddw