2017-09-21 66 views

回答

2

我會通過看background-attachment屬性開始,它的值:

fixed The background is fixed with regard to the viewport

1

它被稱爲視差效果。你可以參考鏈接:Parallax Effect

<style> 
.parallax { 
    /* The image used */ 
    background-image: url("img_parallax.jpg"); 

    /* Set a specific height */ 
    height: 500px; 

    /* Create the parallax scrolling effect */ 
    background-attachment: fixed; 
    background-position: center; 
    background-repeat: no-repeat; 
    background-size: cover; 
} 
</style> 

<!-- Container element --> 
<div class="parallax"></div> 
1

你只需要讓所有的這種連續的div的background-attachment: fixed。檢查下面的例子:

.panels{ 
 
    width: 100vw; 
 
    height: 100vh; 
 
    background-attachment: fixed; 
 
    background-size: cover; 
 
    background-position: center center; 
 
} 
 

 
.panels:nth-child(1){background-image: url('http://lorempixel.com/400/200/sports/');} 
 

 
.panels:nth-child(2){background-image: url('http://lorempixel.com/400/200/nightlife/');} 
 

 
.panels:nth-child(3){background-image: url('http://lorempixel.com/400/200/cats/');} 
 

 
.panels:nth-child(4){background-image: url('http://lorempixel.com/400/200/food/');} 
 

 
.panels:nth-child(5){background-image: url('http://lorempixel.com/400/200/nature/');} 
 

 

 
/* this is for hiding stackoverflow console */ 
 
.as-console-wrapper{ display: none !important; }
<div class="panels"></div> 
 
<div class="panels"></div> 
 
<div class="panels"></div> 
 
<div class="panels"></div> 
 
<div class="panels"></div>