文章參考:https://blog.csdn.net/TriDiamond6/article/details/105222289?depth_1-utm_source=distribute.pc_category.none-task-blog-hot-1&request_id=&utm_source ...
預覽效果:http://tridiamond.me/frontend-tutorials/parallaxImages/
html 佈局:
<body> <div class="wrapper"> <div class="parallax-img1"> <div class="caption"> <span> <img src="../images/TriDiamond_logo_circle.png" alt="" height="80" width="80" /> <p>TriDiamond</p> 向下滑動 </span> </div> </div> <div class="center"> <h1>圖片視差效果DEMO</h1> <p> 三鑽因何而發生? 我們不得不面對一個非常尷尬的事實,那就是, 從這個角度來看, 三鑽的發生,到底需要如何做到,不三鑽的發生,又會如何產生。 三鑽因何而發生? 既然如此, 在這種困難的抉擇下,本人思來想去,寢食難安。 洛克在不經意間這樣說過,學到很多東西的訣竅,就是一下子不要學很多。這啟發了我, 在這種困難的抉擇下,本人思來想去,寢食難安。 塞涅卡在不經意間這樣說過,真正的人生,只有在經過艱難卓絕的鬥爭之後才能實現。這似乎解答了我的疑惑。 就我個人來說,三鑽對我的意義,不能不說非常重大。 瞭解清楚三鑽到底是一種怎麼樣的存在,是解決一切問題的關鍵。 對我個人而言,三鑽不僅僅是一個重大的事件,還可能會改變我的人生。 總結的來說, 生活中,若三鑽出現了,我們就不得不考慮它出現了的事實。 那麼, 一般來講,我們都必須務必慎重的考慮考慮。 經過上述討論, 生活中,若三鑽出現了,我們就不得不考慮它出現了的事實。 而這些並不是完全重要,更加重要的問題是, 要想清楚,三鑽,到底是一種怎麼樣的存在。 雷鋒說過一句富有哲理的話,自己活著,就是為了使別人過得更美好。這似乎解答了我的疑惑。 本人也是經過了深思熟慮,在每個日日夜夜思考這個問題。 問題的關鍵究竟為何? 三鑽的發生,到底需要如何做到,不三鑽的發生,又會如何產生。 拿破侖·希爾曾經說過,不要等待,時機永遠不會恰到好處。帶著這句話,我們還要更加慎重的審視這個問題: 這樣看來, 邁克爾·F·斯特利說過一句富有哲理的話,最具挑戰性的挑戰莫過於提升自我。我希望諸位也能好好地體會這句話。 拉羅什福科曾經提到過,我們唯一不會改正的缺點是軟弱。這句話語雖然很短,但令我浮想聯翩。 這樣看來, 就我個人來說,三鑽對我的意義,不能不說非常重大。 從這個角度來看, 經過上述討論, 經過上述討論, 我們不得不面對一個非常尷尬的事實,那就是。 </p> </div> <div class="parallax-img2"> <div class="caption"> <span>不同高度,不同感覺</span> </div> </div> </div> </body>
css 代碼:
<style> * { padding: 0; margin: 0; font-family: Candara; box-sizing: border-box; } body, html { background: #202020; height: 100%; } .wrapper { height: 100%; } .parallax-img1 { height: 100%; background-image: url('http://ppe.oss-cn-shenzhen.aliyuncs.com/collections/182/7/thumb.jpg'); } .parallax-img2 { min-height: 400px; background-image: url('http://ppe.oss-cn-shenzhen.aliyuncs.com/collections/182/5/thumb.jpg'); } .parallax-img1, .parallax-img2 { position: relative; background-attachment: fixed; //這裡是關鍵代碼 background-position: center; background-repeat: no-repeat; background-size: cover; } .caption { position: absolute; top: 50%; transform: translateY(-50%); width: 100%; text-align: center; display: flex; justify-content: center; } .caption > span { background: rgb(32, 32, 32, 0.85); color: #82fcfd; padding: 18px 50px; font-size: 30px; border-radius: 16px; } .caption p { margin: 0 0 15px 0; padding: 0 0 15px 0; border-bottom: 2px solid #82fcfd; } .caption > span p { font-size: 40px; } p { margin: 15px; font-size: 1.1rem; padding: 1.5rem 5rem; color: #fff; } .center { width: 1200px; margin: 0 auto; color: #ddd; } h1 { text-align: center; margin: 3rem 0 0 0; color: #fff; } </style>
background-attachment: fixed 是實現視差的主要代碼,可以固定背景圖的位置,從而實現視差的效果。
上面的代碼並不完整,我之寫了一部分完整代碼可以去原文看,我只是簡單記錄一下,以免忘記