This works in Chrome, IE, Firefox, Opera and Safari:
(function() { var delay = false; $(document).on('mousewheel DOMMouseScroll', function(event) { event.preventDefault(); if(delay) return; delay = true; setTimeout(function(){delay = false},200) var wd = event.originalEvent.wheelDelta || -event.originalEvent.detail; var a= document.getElementsByTagName('a'); if(wd < 0) { for(var i = 0 ; i < a.length ; i++) { var t = a[i].getClientRects()[0].top; if(t >= 40) break; } } else { for(var i = a.length-1 ; i >= 0 ; i--) { var t = a[i].getClientRects()[0].top; if(t < -20) break; } } if(i >= 0 && i < a.length) { $('html,body').animate({ scrollTop: a[i].offsetTop }); } }); })();
Script http://jsfiddle.net/t6LLybx8/728/
How it works
To control the mouse wheel in most browsers, use $(document).on('mousewheel') . Firefox is a weird game and it requires $(document).on('DOMMouseScroll') .
To get the direction of the mouse wheel (up or down), use event.originalEvent.wheelDelta . Again, Firefox is a weird game, and you should use -event.originalEvent.detail .
If the direction is a negative number, you scroll down the page. In this case, skip each tag starting with the first until its first vertex getClientRects() becomes> = 40. (I used 40 if the browser adds a default margin at the top of the viewport.)
If the direction is a positive number, you scroll the page. In this case, skip each tag starting with the last until its first vertex getClientRects() becomes <-20. (I used -20 to move us up the page.)
The delay variable does not allow you to scroll the mouse wheel too quickly. The whole function ends with a closure, so delay remains a private variable.
source share