【三石jQuery視頻教程】03.建立垂直時間表(Timeline),jquerytimeline

來源:互聯網
上載者:User

【三石jQuery視頻教程】03.建立垂直時間表(Timeline),jquerytimeline

視頻地址:http://v.qq.com/page/g/i/o/g0150rvi6io.html

 

大家好,歡迎來到【三石jQuery視頻教程】,我是您的老朋友 - 三生石上。

今天,我們要通過基本的HTML、CSS、jQuery來實現垂直時間表,先來看下最終的產品:

 

簡單起見,時間表中的每個節點用一張圖片代替,實際應用中可能是標題-圖片-本文的樣子。

Step1:網站目錄

網站目錄非常簡單,包含三部分:lesson3.html 檔案、lib 目錄和 images 目錄。

其中 lesson3.html 包含了一個頁面最基本的組成部分,正確的設定 DOCTYPE 有助於頁面在現代瀏覽器中正確渲染。

<!DOCTYPE html><html><head>    <title>03.建立垂直時間表(Timeline) - 三石jQuery視頻教程</title>    </head><body>    </body></html>

 

lib 目錄僅包含了最新的 jQuery 庫;images 目錄包含使用到的 9 張圖片。 

 

Step2:頁面結構

為頁面添加基本的 html 標籤。

<!DOCTYPE html><html><head>    <title>03.建立垂直時間表(Timeline) - 三石jQuery視頻教程</title></head><body>    <div id="main">        <h2>            03.建立垂直時間表(Timeline) - 三石jQuery視頻教程        </h2>        <div class="timeline">            <div class="year">                <div class="year-inner">                    2015                </div>            </div>            <ul class="events">                <li>                    <img src="images/1.jpg">                </li>                <li>                    <img src="images/2.jpg">                </li>                <li>                    <img src="images/3.jpg">                </li>                <li>                    <img src="images/4.jpg">                </li>            </ul>            <div class="year">                <div class="year-inner">                    2014                </div>            </div>            <ul class="events">                <li>                    <img src="images/5.jpg">                </li>                <li>                    <img src="images/6.jpg">                </li>                <li>                    <img src="images/7.jpg">                </li>                <li>                    <img src="images/8.jpg">                </li>                <li>                    <img src="images/9.jpg">                </li>            </ul>        </div>    </div></body></html>

  

此時的頁面顯示效果:

 

 

Step3:時間標籤的樣式

下面我們來建立時間標籤的樣式,為了實現雙色圓形背景,我們做了如下努力:

 

 <style>body {background-color: #efefef;}#main {margin: 20px auto;}.timeline .year {background-color: #AFDCF8;width: 60px;height: 60px;border-radius: 30px;position: relative;margin: 0 auto 50px;}.timeline .year-inner {background-color: #46A4DA;width: 50px;height: 50px;text-align: center;line-height: 50px;color: #fff;border-radius: 25px;position: absolute;top: 50%;margin-top: -25px;left: 50%;margin-left: -25px;}.events img {width: 100%;}</style>

  

此時的頁面顯示效果:

 

Step4:讓圖片左右顯示

為了讓圖片均勻的左右顯示,也就是一個左,一個右,然後再一個左,一個右,所以需要明確區分奇數和偶數的 li 標籤,我們使用 jQuery 來完成這一任務:

<script src="lib/jquery.js"></script><script>$(function() {$('.timeline .events li:nth-child(2n)').addClass('alt');});</script>

jQuery 的子選取器 :nth-child(2n) 用來挑選清單中的偶數項,並添加樣式類 alt。   

 

下面,我們通過 CSS 定義,左右兩側的圖片分別佔據 40% 的寬度,也就是說中間預留了 20% 的寬度,用一個圖示來簡單說明:

使用 float 樣式來使圖片左右顯示,具體的 CSS 定義:

.timeline .events li {width: 40%;margin-bottom: 100px;border: solid 1px #AFDCF8;padding: 10px;border-radius: 5px;float: left;clear: left;}.timeline .events li.alt {float: right;clear: right;margin-top: 50px;margin-bottom: 50px;}

  

此時的頁面效果:

 

Step5:時間標籤後面的垂直背景線

本來我們可以使用一個絕對位置的 div 節點來實現這個效果,不過更簡便的辦法是 :after 偽選取器,先來看 CSS 定義:

.timeline {overflow: hidden;position: relative;}.timeline:after {content: "";position: absolute;width: 6px;height: 100%;background-color: #AFDCF8;top: 0;left: 50%;margin-left: -3px;}

:after 偽選取器用來在某個元素的內容後面插入新的內容:

  • content 定義要插入的新內容,這裡我們不需要插入任何常值內容,所以留空
  • 將新內容絕對位置,就和使用正常 div 節點一樣
  • 為了讓垂直背景線水平置中顯示,我們同樣使用了前面提到的 top:50% + margin-top 的小技巧

 

此時的頁面效果:

 

Step6:垂直背景線到圖片的連接線

使用類似的偽選取器,我們很容易相對於每個 li 節點,定義連接線:

.timeline .events li {width: 40%;margin-bottom: 100px;border: solid 1px #AFDCF8;padding: 10px;border-radius: 5px;float: left;clear: left;position: relative;}.timeline .events li:after {content: "";position: absolute;top: 30px;left: 100%;width: 25%;height: 6px;background-color: #AFDCF8;}

 

特別注意的幾點:

  • left: 100% 用來表示連接線緊靠表徵圖的右側顯示
  • width: 25% 用來定義連接線的寬度,恰好佔據圖片右側到垂直背景線的距離

一個巨大的疑問號?為什麼是 25%,而不是其他的數值?

其實這是精心安排的:

再來回憶下,圖片佔據了 40% 的寬度,那麼連接線應該佔據整個寬度的 10% = (100% - 40% * 2) / 2,這是顯而易見的!

但是 li:after 的絕對位置(position:absolute)是相對於第一個非靜態定位的父節點而言的,而這兒父節點就是 .timeline .events li 本身,所以連接線相對於 li 的寬度:

40% * x = 10% =  (100% - 40% * 2) / 2,可以計算出 x = 25%

 

====

假設,圖片的 CSS 中將寬度設為 45%,那麼這裡的 li:after 的 width 就應該是 ((100% - 45% * 2) / 2) / 45% = 11.1%

 

此時的頁面效果:

 

 

Step7:自訂尺寸盒模型(box-sizing)

雖然我們信誓旦旦的說,那個 25% 是精心安排的,但是實際的效果的卻相差甚遠,連接線的寬度明顯寬了很多。

如果這是我們算一片的實際寬度,就會發現圖片的實際寬度是 40%,這不包含內邊距(padding) 和 邊框(border)!

 

這時我們就需要重設頁面上所有元素的 box-sizing,從預設值 content-box 改為 border-box,而這個做法也是推薦的做法。

很多註明的 CSS 架構(包括現在比較流行的 BootStrap)都將這一規則作為預設的樣式:

* {    box-sizing: border-box;}

 

下面來簡單比較下這兩則的區別:

  • content-box:為元素設定的寬度和高度不包含內邊距和邊框
  • border-box:為元素設定的寬度和高度包含內邊距和邊框  

 

此時的頁面效果:

  

  

Step8:完整的 CSS 代碼

為右側圖片添加連接線也很簡單,下面看看完整的 CSS 代碼:

* {box-sizing: border-box;}body {background-color: #efefef;}#main {margin: 20px auto;}.timeline {overflow: hidden;position: relative;}.timeline:after {content: "";position: absolute;width: 6px;height: 100%;background-color: #AFDCF8;top: 0;left: 50%;margin-left: -3px;z-index: 0;}.timeline .year {background-color: #AFDCF8;width: 60px;height: 60px;border-radius: 30px;position: relative;margin: 0 auto 50px;clear: both;z-index: 1;}.timeline .year-inner {background-color: #46A4DA;width: 50px;height: 50px;text-align: center;line-height: 50px;color: #fff;border-radius: 25px;position: absolute;top: 50%;margin-top: -25px;left: 50%;margin-left: -25px;}.timeline .events {list-style-type: none;padding: 0;margin: 0;clear: both;}.timeline .events li {width: 40%;margin-bottom: 100px;border: solid 1px #AFDCF8;padding: 10px;border-radius: 5px;float: left;clear: left;position: relative;}.timeline .events li:after {content: "";position: absolute;top: 30px;left: 100%;width: 25%;  /* 10% = 1 * 40% * 25% */height: 6px;background-color: #AFDCF8;}.timeline .events li.alt {float: right;clear: right;margin-top: 50px;margin-bottom: 50px;}.timeline .events li.alt:before {content: "";position: absolute;top: 30px;left: -25%;width: 25%;height: 6px;background-color: #AFDCF8;}.events img {width: 100%;}

  

 

最終的頁面效果:

 

 源碼和視頻下載  

源碼和視頻請自行到視頻中尋找! 三石出品,必屬精品!


 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.