基本视差-背景抖动

Basic Parallax - Background jerking up

本文关键字:背景 抖动 视差      更新时间:2023-09-26

我有一个移动背景图像的简单代码,但一旦看到该部分,图像就会急动。这个想法是背景pos只有在视图中时才移动。

有什么想法吗?

$window = $(window);
$('.portfolioSection').each(function(){
  var $bgobj = $(this); // assigning the object
  var speed = 8;
  $(window).scroll(function() {
    if($(window).scrollTop() + 150 >= $bgobj.offset().top){
      // Scroll the background at var speed
      // the yPos is a negative value because we're scrolling it UP!                                
      var yPos = -($window.scrollTop() / speed);
      // Put together our final background position
      var coords = '0 '+ yPos + 'px'
      // Move the background
      $bgobj.css({ backgroundPosition: coords });
    }
  }); // window scroll Ends
});

解决了它-我没有考虑区段位置-第9行

var yPos = -(($window.scrollTop()-$bgobj.offset().top) / speed);