firefox scroll to animation is very laggy compared to chrome and safari

时光怂恿深爱的人放手 提交于 2019-12-13 03:48:00

问题


I have made a web page in React Js and one section as a lot of images which causes the scroll to run 1 fps making it a very bad user experience.

The scroll works well both on safari and chrome so I can't understand why Firefox is so slow? It seems that Firefox generally is bad at rendering a lot of images at once. I used this code from this solution Cross browser JavaScript (not jQuery...) scroll to top animation

css

margin: 20px 10px 0 10px;
width: 30%;
min-width: 300px;
background: #FFFFFF;
border-radius: 4px;
-webkit-box-shadow: 0px 5px 15px 0px rgba(70, 71, 76, 0.07);
box-shadow: 0px 5px 15px 0px rgba(70, 71, 76, 0.07);
overflow: hidden;

Below you can see the HTML is rendered.

The scroll function being used is used on many other pages and it works fine. If I remove the employee's node the scroll function works very well. Anybody with a hint why Firefox is so laggy while safari and chrome work very well?

scroll function

scrollTo(position) {
    let scrollY = window.scrollY || document.documentElement.scrollTop,
        scrollTargetY = position() || 0,
        speed = 2000,
        easing = 'easeInOutSine',
        currentTime = 0

    // min time .1, max time .8 seconds
    let time = Math.max(.1, Math.min(Math.abs(scrollY - scrollTargetY) / speed, .8))

    // easing equations from https://github.com/danro/easing-js/blob/master/easing.js
    let easingEquations = {
        easeOutSine: function (pos) {
        return Math.sin(pos * (Math.PI / 2));
        },
        easeInOutSine: function (pos) {
        return (-0.5 * (Math.cos(Math.PI * pos) - 1));
        },
    }

    // add animation loop
    function tick() {
      currentTime += 1 / 60;

      let p = currentTime / time;
      let t = easingEquations[easing](p);

      if (p < 1) {
        requestAnimationFrame(tick);
        window.scrollTo(0, scrollY + ((scrollTargetY - scrollY) * t));
      } else {
        window.scrollTo(0, scrollTargetY);
      }
    }

    // call it once to get started
    tick()
    }

来源:https://stackoverflow.com/questions/56396100/firefox-scroll-to-animation-is-very-laggy-compared-to-chrome-and-safari

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!