HTML5與CSS3的響應式垂直時間軸樣本

來源:互聯網
上載者:User

網頁時間軸一般用於展示以時間為主線的事件,如企業網站常見的公司發展曆程等。本文將給大家介紹一款基於HTML5和CSS3的漂亮的垂直時間軸,它可以響應頁面配置,適用於HTML5開發的PC和移動手機WEB應用。

我們使用了HTML5標籤<section>,時間軸中所有的內容包括標題、簡介、時間和映像都放在.cd-timeline-block的DIV中,多個DIV形成一個序列,並把這些DIV放在<section>中。注意要看到本例示範效果,需要你的瀏覽器支援HTML5和CSS3。
 

 代碼如下 複製代碼
<section id="cd-timeline" class="cd-container">
    <div class="cd-timeline-block">
        <div class="cd-timeline-img cd-picture">
            <img src="img/cd-icon-picture.svg" alt="Picture">
        </div>
 
        <div class="cd-timeline-content">
            <h2>HTML5+CSS3實現的響應式垂直時間軸</h2>            
            <p>網頁時間軸一般用於展示以時間為主線的事件,如企業網站常見的公司發展曆程等。本文將給大家介紹一款基於HTML5和CSS3的漂亮的垂直時間軸,它可以響應頁面配置,適用於HTML5開發的PC和移動手機WEB應用。</p>
            <a href=" class="cd-read-more" target="_blank">閱讀全文</a>
            <span class="cd-date">2015-01-06</span>
        </div>
    </div>
    <div class="cd-timeline-block">
        ...
    </div>
    

</div>
本例使用了svg映像作為表徵圖,你也可以換成png,gif格式映像。

CSS代碼

我們使用:before偽類在時間軸#cd-timeline的前面建立一條垂直線,那麼時間軸各個節點的內容都會基於這垂直線。
 

 代碼如下 複製代碼
#cd-timeline {
  position: relative;
  padding: 2em 0;
  margin-top: 2em;
  margin-bottom: 2em;
}
#cd-timeline::before {
  content: '';
  position: absolute;
  top: 0;
  left: 18px;
  height: 100%;
  width: 4px;
  background: #d7e4ed;
}

好,接下來我們要做的是,根據使用者瀏覽器視窗的大小來調整時間軸的布局,當瀏覽器視窗非常大時,時間軸的各節點內容會分布在垂直線的兩邊(垂直線置中),當瀏覽器視窗足夠小如手機,iPad中豎屏瀏覽時,時間軸的各節點內容會排列在垂直線的右邊(垂直線靠左),這個俗稱響應式布局。
 

 代碼如下 複製代碼
.cd-timeline-block {
  position: relative;
  margin: 2em 0;
}
.cd-timeline-block:after {
  content: "";
  display: table;
  clear: both;
}
.cd-timeline-block:first-child {
  margin-top: 0;
}
.cd-timeline-block:last-child {
  margin-bottom: 0;
}
@media only screen and (min-width: 1170px) {
  .cd-timeline-block:nth-child(even) .cd-timeline-content {
    float: right;
  }
  .cd-timeline-block:nth-child(even) .cd-timeline-content::before {
    top: 24px;
    left: auto;
    right: 100%;
    border-color: transparent;
    border-right-color: white;
  }
  .cd-timeline-block:nth-child(even) .cd-timeline-content .cd-read-more {
    float: right;
  }
  .cd-timeline-block:nth-child(even) .cd-timeline-content .cd-date {
    left: auto;
    right: 122%;
    text-align: right;
  }
}

CSS3中可以使用@media判斷裝置的螢幕尺寸,根據不同的尺寸執行不同的css樣式。本例中還用到了CSS3的陰影、圓角等效果,文中不再詳述,大家可以參考網上好多關於CSS3的基礎教程,你也可以直接下載源碼包,本例的css全部整理在style.css檔案中。當然你也可以在本例的基礎上使用JS和CSS3來給時間軸增加一些過渡動畫,效果可能會更好。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.