2013-12-14 41 views
0

我有一個用於移動背景圖像的簡單代碼,但是一旦該部分在視圖中,圖像就會打起來。這個想法是隻有當該部分在視野中時,background-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 

}); 

回答

0

解決它 - 我沒有考慮區間位置 - 9號線

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