利用css 實現 視覺差效果

文章參考:https://blog.csdn.net/TriDiamond6/article/details/105222289?depth_1-utm_source=distribute.pc_category.none-task-blog-hot-1&request_id=&utm_source=distribute.pc_category.none-task-blog-hot-1css

 預覽效果:http://tridiamond.me/frontend-tutorials/parallaxImages/html

html 佈局:app

<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 代碼:frontend

<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 是實現視差的主要代碼,能夠固定背景圖的位置,從而實現視差的效果。佈局

相關文章
相關標籤/搜索