基於Parallax設計HTML視差效果

    年關將至,給你們拜年。javascript

    最近時間充裕了一點,給你們介紹一個比較有意思的控件:Parallax。它能夠用來實現鼠標移動時,頁面上的元素也作偏移的視差效果。在一些有表現層次,佈局空曠的頁面上,用來作Head最合適不過了,你們能夠看這個官方Democss

 

1、準備工做html

    若是不想用cdn的話,就下載java

    一、在github上下載parallaxjquery

    二、下載jquerygit

 

2、實現效果github

    這裏只介紹最簡單的使用方法,先上代碼:框架

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>恭喜發財</title>
 6     <script type="text/javascript" src="//cdn.bootcss.com/jquery/3.1.1/jquery.min.js"></script>
 7     <script type="text/javascript" src="//cdn.bootcss.com/parallax/2.1.3/jquery.parallax.min.js"></script>
 8     <script type="text/javascript" src="//cdn.bootcss.com/parallax/2.1.3/parallax.min.js"></script>
 9     <style>
10         html, body {
11             width: 100%;
12             height: 100%;
13             margin: 0;
14             padding: 0;
15         }
16         /*** 定位 ***/
17         #DIV_title {
18             width: 100%;
19             height: 100%;
20             position: absolute;
21             top: 0;
22             left: 0;
23             margin: 0;
24         }
25         #DIV_title li {
26             width: 100%;
27             height: 100%;
28         }
29         #DIV_title_bg {
30             position: absolute;
31             width: 110%;
32             height: 110%;
33             top: -5%;
34             left: -5%;
35             background: url("http://dwz.cn/58F0u5") no-repeat 50% 100%;
36             background-size: cover;
37         }
38         #DIV_title h1 {
39             position: absolute;
40             left: 50%;
41             font-size: 100px;
42             font-weight: bold;
43             color: yellow;
44         }
45         #H1_title_1 {
46             margin-left: -250px;
47             top: 100px;
48         }
49         #H1_title_2 {
50             margin-left: -200px;
51             top: 200px;
52         }
53         #H1_title_3 {
54             margin-left: -150px;
55             top: 300px;
56         }
57         #H1_title_4 {
58             margin-left: -100px;
59             top: 400px;
60         }
61         #H1_title_5 {
62             margin-left: -50px;
63             top: 500px;
64         }
65     </style>
66 </head>
67 
68 <body>
69     <ul id="DIV_title">
70         <li class="layer" data-depth="0.10"><div id="DIV_title_bg"></div></li>
71         <li class="layer" data-depth="0.15"><h1 id="H1_title_1"></h1></li>
72         <li class="layer" data-depth="0.60"><h1 id="H1_title_2"></h1></li>
73         <li class="layer" data-depth="0.30"><h1 id="H1_title_3"></h1></li>
74         <li class="layer" data-depth="0.50"><h1 id="H1_title_4"></h1></li>
75         <li class="layer" data-depth="1.00"><h1 id="H1_title_5"></h1></li>
76     </ul>
77 
78 <script type="text/javascript">
79     
80     $(function() {
81         var parallax = new Parallax(document.getElementById("DIV_title"));
82         console.log(parallax);
83     });
84 
85 </script>
86 
87 </body>
88 </html>

    先忽略css,來看最核心的框架:dom

    一、DIV_title list。此list的層次結構能夠根據本身的須要進行調整,記得list子級的li帶上class="layer"。佈局

    二、data-depth屬性。此屬性關係到景深,取0~1,越大的數字表示越靠間,相對其它元素動得越快。

    三、js初始化。在dom加載完,經過new Parallax()方法便可把指定元素歸入視差效果。

    四、更多的參數,參考前面的github連接。

 

    再來看一下css:

    一、經過絕對定位把各個元素放到不一樣的位置;

    二、實現了bg圖也可動的效果。#DIV_title_bg元素的css之因此要加-5%,就是在背景也可動時,不出現空白因此特地加了位移,圖片也對應地加大了10%。

    三、各個子元素以50%居中爲基準,左右移動定位;

 

4、Demo效果

    http://codepen.io/anon/pen/qRmjOW

    請無視主頁面下面滾動條的空白,這是由於iframe致使的。

    轉載請註明原址:http://www.cnblogs.com/lekko/p/6339827.html 

相關文章
相關標籤/搜索