2

我正在尝试使用 window.onscroll 和 scrollTop 创建视差效果,但我一直遇到渲染问题,看起来我在渲染视口后收到了 onscroll。正如您从这个示例(附加代码)中看到的那样,盒子的移动是不稳定且不稳定的。

该行为在所有基于 WebKit 的浏览器和 FireFox 中都是相同的。

JavaScript

$(document).ready(function() {
  $('.box').each(function() {
    $(this).data('y', $(this).offset().top);
  });
});

$(window).scroll(function() {
  var scroll = $(this).scrollTop();

  $('.box').each(function() {
    var parallax = $(this).data('parallax');

    if (parallax) {
      var y = $(this).data('y');
      var offset = (scroll - y) * parallax;

      $(this).css('-webkit-transform', 'translateY(' + offset + 'px)');
    }
  });
});

标记

<div class="red box">Lorem ipsum dolor sit amet.</div>
<div class="green box" data-parallax="0.4">Consectetur adipiscing elit.</div>
<div class="blue box" data-parallax="0.3">Nam consectetur dolor.</div>
4

1 回答 1

2

只是想知道是否值得使用预制的而不是“重新发明”轮子?

http://joelb.me/scrollpath/

于 2012-04-30T13:14:25.837 回答