0

I'm trying to add a smooth scroll that functions properly on all browsers (but only IE11 and Edge for microsoft). The issue is that this script is completely breaking the scroll in Edge browsers.

I've included console logs which confirm that the script is calculating the mousewheel movement, however, there is no "visual" movement of the page.

new SmoothScroll(document, 120, 12);
function SmoothScroll(target, speed, smooth) {
if (target == document) {
    target = document.documentElement || document.body.parentNode || document.body; // cross browser support for document scrolling
    var moving = false;
    var pos = window.pageYOffset;
    target.addEventListener("mousewheel", scrolled, false);
    target.addEventListener("DOMMouseScroll", scrolled, false);
}

function scrolled(e) {
    e.preventDefault(); // disable default scrolling
    var delta = e.delta || e.wheelDelta;

    if (delta === undefined) {
        //we are on firefox
        delta = -e.detail;
    }

    delta = Math.max(-1, Math.min(1, delta)); // cap the delta to [-1,1] for cross browser consistency

    pos += -delta * speed;
    pos = Math.max(0, Math.min(pos, target.scrollHeight - target.clientHeight)); // limit scrolling

    if (!moving) {
        update();
    }
}

function update() {
    moving = true;
    var delta = (pos - window.pageYOffset) / smooth;
    console.log(window.pageYOffset);

    if (window.navigator.userAgent.indexOf("Edge") > -1) {
        window.pageYOffset += delta;
    }
    else {
        target.scrollTop += delta;
    }

    console.log(Math.abs(delta));

    if (Math.abs(delta) > 0.5) {
        console.log("entered if");
        requestFrame(update);
    }
    else {
        moving = false;
    }
}

var requestFrame = (function() {
    console.log("request frame is triggered");
    // requestAnimationFrame cross browser
    return (
        window.requestAnimationFrame ||
        window.webkitRequestAnimationFrame ||
        window.mozRequestAnimationFrame ||
        window.oRequestAnimationFrame ||
        window.msRequestAnimationFrame ||
        function(func) {
            window.setTimeout(func, 1000 / 50);
        }
    );
})();}

Why is it not working?

TylerH
  • 20,799
  • 66
  • 75
  • 101
  • what happens if you do just `window.requestAnimationFrame(function() {console.log('success');});` https://learn.microsoft.com/en-us/microsoft-edge/dev-guide/performance/animation-timing-api You might be calling it in a way that's not compatible with how IE/edge interprets javascript. – Tschallacka Apr 16 '18 at 13:19
  • my console outputs success just fine, however the webpage is still visibly locked/not scrolling. – MelodicNotes Apr 16 '18 at 13:29
  • okay. so requestAnimationFrame works. If you call your own requestFrame function with a console.log function, does it also output to console? – Tschallacka Apr 16 '18 at 13:34
  • Yes, i have the console.log in the original code above that keeps telling me its triggered, along with the position changes on scroll. – MelodicNotes Apr 16 '18 at 13:37
  • Can you try using window.scrollY instead of scrollTop? see https://stackoverflow.com/questions/20514596/document-documentelement-scrolltop-return-value-differs-in-chrome – Tschallacka Apr 16 '18 at 13:53
  • scrollY doesn't work either. pageYOffset is the replacement for scrollY I thought? – MelodicNotes Apr 16 '18 at 16:31
  • This may be because of the Event Loop.....beacuse in Edge, the questAnimationFrame happens AFTER rendering. – Evading Shadows Dec 16 '18 at 06:48

1 Answers1

0

This may be because of the Event Loop.....beacuse in Edge, the questAnimationFrame happens AFTER rendering.

For more info see this excellent talk by Jake Archibald!

https://www.youtube.com/watch?v=cCOL7MC4Pl0

Evading Shadows
  • 479
  • 1
  • 6
  • 22