2016-09-06 72 views
-1

有沒有辦法在滾動特定部分的同時更改background-img。 正如你可以在小提琴(jsfiddle.net/vwxxjhg1/)中看到的,我在左邊有一個小圖標。所以當圖標移動時(或者頁面正在移動,圖標位置固定(比如我))併到達灰色部分時,它會更改圖標的background-img。接下來的白色部分是3. background-img ...等等。更改背景img,滾動(每個部分不同的img)

感謝您的一些想法!

回答

0
I think its helps to you . 
jQuery(window).scroll(function(){ 
var fromTopPx = 200; // distance to trigger 
var scrolledFromtop = jQuery(window).scrollTop(); 
if(scrolledFromtop > fromTopPx){ 
    jQuery('html').addClass('scrolled'); 
}else{ 
    jQuery('html').removeClass('scrolled'); 
} 
}); 
And in your CSS file: 

html { 
background-repeat: no-repeat; 
background-position: center center; 
background-attachment: fixed; 
-webkit-background-size: cover; 
-moz-background-size: cover; 
-o-background-size: cover; 
background-size: cover; 
} 

html { 
background-image:url(http://robt.info/wp-content/uploads/2013/06/funny-kids-comic-animals.jpg); 
    } 

html.scrolled { 
background-image:url(http://robt.info/wp-content/uploads/2013/06/funny-kids-comic-animals_2.jpg); 
    }