0

Ok, here is the working cross-browser code, which has easing is well:

function handle(delta) {
var time = 1500;
var easing = 'easeInOutExpo';
var distance = document.getElementById('Element').scrollHeight;

$('html, body').stop().animate({
    scrollTop: $(window).scrollTop() - (distance * delta)
}, time, easing );
}
/** Event handler for mouse wheel event.
*/
function wheel(event){
    var delta = 0;
    if (!event) /* For IE. */
            event = window.event;
    if (event.wheelDelta) { /* IE/Opera. */
            delta = event.wheelDelta/120;
    } else if (event.detail) { /** Mozilla case. */
            /** In Mozilla, sign of delta is different than in IE.
             * Also, delta is multiple of 3.
             */
            delta = -event.detail/3;
    }
    /** If delta is nonzero, handle it.
     * Basically, delta is now positive if wheel was scrolled up,
     * and negative, if wheel was scrolled down.
     */
    if (delta)
            handle(delta);
    /** Prevent default actions caused by mouse wheel.
     * That might be ugly, but we handle scrolls somehow
     * anyway, so don't bother here..
     */
    if (event.preventDefault)
            event.preventDefault();
event.returnValue = false;
}

/** Initialization code. 
* If you use your own event management code, change it as required.
*/
if (window.addEventListener)
    /** DOMMouseScroll is for mozilla. */
    window.addEventListener('DOMMouseScroll', wheel, false);
/** IE/Opera. */
window.onmousewheel = document.onmousewheel = wheel;

To get the distance there are two options: 1. manually by giving a value 2. automatically through css by defining "Element"

Hope to have helped.

Prokophs
  • 1
  • 2
  • possible duplicate of [event.wheelDelta returns undefined](http://stackoverflow.com/questions/8886281/event-wheeldelta-returns-undefined) – showdev Dec 04 '13 at 18:18
  • Hi showdev and thanks for your comment. I've seen this post already but still can't figure it out. I've tried the solutions proposed but none of them worked. Any other help or code perhaps? – Prokophs Dec 04 '13 at 18:30
  • I've used [this snippet](http://stackoverflow.com/a/18791958/1169519), I'd recall it worked also in IE8... – Teemu Dec 04 '13 at 18:38
  • Teemu thanks a lot, that solution worked somehow, eventhough gives some errors. It has to do with browser events where IE its always complicated. I'll retouch the code and when ok I'll post it here for others. – Prokophs Dec 04 '13 at 19:10
  • It should work without errors, if you just use the given attaching method instead of direct `document.onmousewheel=...`. – Teemu Dec 04 '13 at 19:16
  • It does now work without errors but it doesn't work anymore in safari – Prokophs Dec 04 '13 at 21:12
  • Ok, here is the working code: – Prokophs Dec 06 '13 at 13:24

0 Answers0