简体   繁体   English

视差效应 - Firefox 性能不佳

[英]Parallax effect - poor performance on Firefox

I'm building multi layered parallax effect on my site (html, css, js).我正在我的网站(html、css、js)上构建多层视差效果。 Everything works quite well, but I've noticed that my parallax effect works really bad on Firefox, window.onscroll seems to be laggy, refresh rate so to speak is very low.一切都很好,但我注意到我的视差效果在 Firefox 上效果很差, window.onscroll似乎滞后,可以说刷新率非常低。

Here's my JS implementation:这是我的 JS 实现:

document.addEventListener('DOMContentLoaded', function() {
    const layers = document.querySelectorAll("[data-type='parallax']");

    window.addEventListener('scroll', event => {
        const topDistance = window.pageYOffset;
      window.requestAnimationFrame(function() {
        for (let i = 0; i < layers.length; ++i) {
          const depth = layers[i].getAttribute('data-depth');
          const movement = topDistance * depth;
          const translate3d = 'translate3d(0, ' + movement + 'px, 0)';
          layers[i].style.transform = translate3d;
        }
      })
    });
});

My html code:我的html代码:

<div class="parallax-banner">
    <div class="parallax-layer layer-1" data-type="parallax" data-depth="0.05"></div>
    <div class="parallax-layer layer-2" data-type="parallax" data-depth="0.2"></div>
    <div class="parallax-layer layer-3" data-type="parallax" data-depth="0.4"></div>
    <div class="parallax-layer layer-4" data-type="parallax" data-depth="0.6"></div>
    <div class="parallax-layer layer-5" data-type="parallax" data-depth="0.7"></div>
    <div class="parallax-layer layer-6" data-type="parallax" data-depth="0"></div>
</div>

Have you encountered it?你遇到过吗? Is it typical issue?这是典型的问题吗? How can I fix that?我该如何解决?

I have the following very simple JS-implementation with two layers in the back, which was unusable with Firefox due to jittering and laggy behaviour:我有以下非常简单的 JS 实现,后面有两层,由于抖动和滞后行为,Firefox 无法使用它:

$(function() {
  $(window).on('scroll', function() {
    $('#background').css('background-position-y', $(window).scrollTop() * -.15);
  });
});  
$(function() {
  $(window).on('scroll', function() {
    $('#background2').css('background-position-y', $(window).scrollTop() * -.09);
  });
  });

CSS-only alternatives didnt work for me as it caused the background layers to visibly overflow after my contents end.仅 CSS 替代方案对我不起作用,因为它导致背景层在我的内容结束后明显溢出。

Finally I found a way to improve the performance desktop Firefox (not on mobile yet).最后我找到了一种提高桌面 Firefox 性能的方法(还没有在移动设备上)。 I added我加了

position: fixed;
background-attachment: fixed;
background-position: top;

to all my background layers.到我所有的背景层。

Still no improvement in iOS Safari and mobile Firefox. iOS Safari 和移动版 Firefox 仍然没有改进。 There are several bug reports for Firefox since version 16.自 16 版以来,Firefox 有几个错误报告。

On my long way searching the internet for solutions i also found and added a script by keithclark but I'm not sure if this makes any difference at all, the script is from 2011:在我漫长的互联网搜索解决方案的过程中,我还找到并添加了 keithclark 的脚本,但我不确定这是否有任何区别,该脚本来自 2011 年:

/*
Firefox super responsive scroll (c) Keith Clark - MIT Licensed
*/
(function(doc) {
console.log("Document executed")
  var root = doc.documentElement,
      scrollbarWidth, scrollEvent;

  // Not ideal, but better than UA sniffing.
  if ("MozAppearance" in root.style) {

    // determine the vertical scrollbar width
    scrollbarWidth = root.clientWidth;
    root.style.overflow = "scroll";
    scrollbarWidth -= root.clientWidth;
    root.style.overflow = "";

    // create a synthetic scroll event
    scrollEvent = doc.createEvent("UIEvent")
    scrollEvent.initEvent("scroll", true, true);

    // event dispatcher
    function scrollHandler() {
      doc.dispatchEvent(scrollEvent)
    }

    // detect mouse events in the document scrollbar track
    doc.addEventListener("mousedown", function(e) {
      if (e.clientX > root.clientWidth - scrollbarWidth) {
        doc.addEventListener("mousemove", scrollHandler, false);
        doc.addEventListener("mouseup", function() {
          doc.removeEventListener("mouseup", arguments.callee, false);
          doc.removeEventListener("mousemove", scrollHandler, false);
        }, false)
      }
    }, false)

    // override mouse wheel behaviour.
    doc.addEventListener("DOMMouseScroll", function(e) {
      // Don't disable hot key behaviours
      if (!e.ctrlKey && !e.shiftKey) {
        root.scrollTop += e.detail * 16;
        scrollHandler.call(this, e);
        e.preventDefault()
      }
    }, false)

  }
})(document);

You can test it by pasting it to the console.您可以通过将其粘贴到控制台来测试它。

I hope I could at least help a little bit.我希望我至少能帮上一点忙。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM