2016-05-20 112 views

回答

1

你應該嘗試添加位置固定屬性到你的背景,它應該有所幫助。

body, 
 
html, 
 
main { 
 
    /* important */ 
 
    height: 100%; 
 
} 
 
.fixed-bg { 
 
    min-height: 100%; 
 
    background-size: cover; 
 
    background-attachment: fixed; 
 
    background-repeat: no-repeat; 
 
    background-position: center center; 
 
} 
 
.fixed-bg.bg-1 { 
 
    background-image: url("https://c7.staticflickr.com/8/7052/6803328782_432e1dd481_b.jpg"); 
 
} 
 
.fixed-bg.bg-2 { 
 
    background-image: url("https://imagesus-ssl.homeaway.com/mda01/b8b73d34-98e2-4633-ab05-6797a99bcf2e.1.10/Cocoa-Beach-Pier-with-Disney-cruise-ship-in-background.jpg"); 
 
} 
 
.fixed-bg.bg-3 { 
 
    background-image: url("http://static.fly4free.pl/s/2015/4/f/f6dc42b71e7ef2336edbfbc8c9124684.jpeg"); 
 
} 
 
.fixed-bg.bg-4 { 
 
    background-image: url("http://coachhouse.com.au/default/assets/File/kayak.jpg"); 
 
} 
 
.scrolling-bg { 
 
    min-height: 100%; 
 
}
<main> 
 
    <div class="fixed-bg bg-1"> 
 
    <h1><!-- title goes here--></h1> 
 
    </div> 
 

 
    <div class="scrolling-bg cd-color-2"> 
 
    <div class="container"> 
 
     <p> 
 
     Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dolore incidunt suscipit similique, dolor corrupti cumque qui consectetur autem laborum fuga quas ipsam doloribus sequi... 
 
     </p> 
 
    </div> 
 
    </div> 
 
    <div class="fixed-bg bg-2"> 
 
    <h1><!-- title goes here --></h1> 
 
    </div> 
 

 
    <div class="scrolling-bg color-2"> 
 
    <div class="container"> 
 
     <p> 
 
     Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dolore incidunt suscipit similique, dolor corrupti cumque qui consectetur autem laborum fuga quas ipsam doloribus sequi... 
 
     </p> 
 
    </div> 
 
    </div> 
 
    <div class="fixed-bg bg-3"> 
 
    <h1><!-- title goes here --></h1> 
 
    </div> 
 

 
    <div class="scrolling-bg color-2"> 
 
    <div class="container"> 
 
     <p> 
 
     Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dolore incidunt suscipit similique, dolor corrupti cumque qui consectetur autem laborum fuga quas ipsam doloribus sequi... 
 
     </p> 
 
    </div> 
 
    </div> 
 
    <div class="fixed-bg bg-4"> 
 
    <h1><!-- title goes here --></h1> 
 
    </div> 
 

 
    <div class="scrolling-bg color-2"> 
 
    <div class="container"> 
 
     <p> 
 
     Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dolore incidunt suscipit similique, dolor corrupti cumque qui consectetur autem laborum fuga quas ipsam doloribus sequi... 
 
     </p> 
 
    </div> 
 
    </div> 
 
</main>

事業部後格的方法:

body, 
 
html, 
 
main { 
 
    /* important */ 
 
    height: 100%; 
 
    padding:0; 
 
    margin:0; 
 
} 
 
.container{ 
 
position:absolute; 
 

 
} 
 

 
.fixed-bg { 
 
    min-height: 100%; 
 
    background-repeat: no-repeat; 
 
    background-position: center center; 
 
    
 
} 
 
.fixed-bg.bg-1 { 
 
    background-image: url("https://c7.staticflickr.com/8/7052/6803328782_432e1dd481_b.jpg"); 
 

 
    
 
} 
 
.fixed-bg.bg-2 { 
 
    background-image: url("https://imagesus-ssl.homeaway.com/mda01/b8b73d34-98e2-4633-ab05-6797a99bcf2e.1.10/Cocoa-Beach-Pier-with-Disney-cruise-ship-in-background.jpg"); 
 
    
 
    
 
} 
 
.fixed-bg.bg-3 { 
 
    background-image: url("http://static.fly4free.pl/s/2015/4/f/f6dc42b71e7ef2336edbfbc8c9124684.jpeg"); 
 
} 
 
.fixed-bg.bg-4 { 
 
    background-image: url("http://coachhouse.com.au/default/assets/File/kayak.jpg"); 
 
} 
 
.scrolling-bg { 
 
    min-height: 100%; 
 
    
 
}
<main> 
 
    <div class="fixed-bg bg-1"> 
 
    <div class="container"><!-- content goes here--></div> 
 
    </div> 
 
    
 
    <div class="fixed-bg bg-2"> 
 
    <div class="container"><!-- content goes here--></div> 
 
    </div> 
 

 
    
 
    <div class="fixed-bg bg-3"> 
 
    <div class="container"><!-- content goes here--></div> 
 
    </div> 
 

 
    
 
    <div class="fixed-bg bg-4"> 
 
    <div class="container"><!-- content goes here--></div> 
 
    </div> 
 

 
    
 
</main>

+0

你的效果看起來像一個壁紙逐漸過來另一.......請看看第二和第三個鏈接...............我想不同的壁紙來一個接一個...... –

+0

請檢查我的編輯 –

1

所以,你正在尋找一個視差效果,有一些插件是的。我最近使用http://pixelcog.github.io/parallax.js/,這對我來說效果很好。

使用它非常簡單。你說你想要一個HTML/CSS的解決方案,但我不認爲你可以做到沒有JavaScript。如果別人知道沒有javascript的替代方案,我也想看看它。但即使您對JavaScript不瞭解,也可以使用此插件,只需按照我提供的鏈接上的說明操作即可。

該插件需要jquery才能使用!

+0

爲什麼第二個鏈接需要視差效果,我能不能一個接一個堆疊背景壁紙效果......(見第二鏈接)... @VlatkoMurković –

+0

在第二環節,第三環節沒有視差效果。你可以製作div,然後在其中的每一個上放置background-image css屬性,而不是在body標籤上使用背景圖像。 –