CSS3的學習--實現瀑布流

來源:互聯網
上載者:User

標籤:des   c   style   class   blog   code   

原文:CSS3的學習--實現瀑布流

基於CSS3實現瀑布流,使用CSS3的CSS 多欄(Multi-column)。

可以到github上下載源碼 : https://github.com/CraryPrimitiveMan/waterfall-css3

瀑布流,又稱瀑布流式布局。是比較流行的一種網站頁面配置,視覺表現為參差不齊的多欄布局,隨著頁面捲軸向下滾動,這種布局還會不斷載入資料區塊並附加至當前尾部。我們只是實現了多欄的布局。

CSS 多欄(Multi-column) : http://www.w3chtml.com/css3/properties/multi-column/

 

Properties
屬性
CSS Version
版本
Inherit From Parent
繼承性
Description
簡介
columns CSS3 CSS3 columns 屬性,是複合屬性,設定或檢索對象的列數和每列的寬度。
column-width CSS3 CSS3 column-width 屬性,設定或檢索對象每列的寬度
column-count CSS3 CSS3 column-count 屬性,設定或檢索對象的列數
column-gap CSS3 CSS3 column-gap 屬性,設定或檢索對象的列與列之間的間隙
column-rule CSS3 CSS3 column-rule 屬性,是複合屬性。設定或檢索對象的列與列之間的邊框。
column-rule-width CSS3 CSS3 column-rule-width 屬性,設定或檢索對象的列與列之間的邊框厚度。
column-rule-style CSS3 CSS3 column-rule-style 屬性,設定或檢索對象的列與列之間的邊框樣式。
column-rule-color CSS3 CSS3 column-rule-color 屬性,設定或檢索對象的列與列之間的邊框顏色。
column-span CSS3 CSS3 column-span 屬性,設定或檢索對象元素是否橫跨所有列。
column-fill CSS3 CSS3 column-fill 屬性,設定或檢索對象所有列的高度是否統一。
column-break-before CSS3 CSS3 column-break-before 屬性,設定或檢索對象之前是否斷行。
column-break-after CSS3 CSS3 column-break-after 屬性,設定或檢索對象之後是否斷行。
column-break-inside CSS3 CSS3 column-break-inside 屬性,設定或檢索對象內部是否斷行。

 

 

 

 

 

 

 

 

 

 

 

 

 

 

先來寫一個簡單的圖片頁面

<html>    <head>        <link rel="stylesheet" type="text/css" href="css/main.css">        <script type="text/javascript" src="../lib/jquery/jquery-1.11.1.min.js"></script>    </head>    <body>        <div id="main">            <div class="box">                <div class="pic">                    <img src="">                </div>            </div>            <div class="box">                <div class="pic">                    <img src="">                </div>            </div>            <!-- 這裡省略多個class為box的div-->            <div class="box">                <div class="pic">                    <img src="">                </div>            </div>        </div>    </body>    <script type="text/javascript">        var width = 300, height = 300;        $(‘.box img‘).each(function(){            // 隨機圖片的高寬,如果大小一樣,就沒必要用瀑布流了            width = Math.floor(Math.random() * 100) + 300;            height = Math.floor(Math.random() * 500) + 300;            $(this).attr(‘src‘, ‘http://placekitten.com/‘+ height +‘/‘ + width);        });    </script></html>

假設,寬和高都是350,產生連結為http://placekitten.com/350/350,訪問這個link就會得到一張350X350的可愛的小貓圖片~~O(∩_∩)O~~

然後,添加相應的CSS即可

* {  padding: 0;  margin: 0;}#main {  -webkit-column-count: 4;  -moz-column-count: 4;  column-count: 4;  -moz-column-gap:20px;  -webkit-column-gap:20px;  column-gap:20px;  padding: 10px;}.box {  padding: 15px;  border: solid 2px #eeeeee;  border-radius: 4px;  margin-bottom: 15px;  cursor: pointer;}.box img {  width: 100%;}

其中的 column-count 代表分成幾列,column-gap 代表列和列之間的寬度,你可以根據自己的需要調整。我們還可以使用 column-width 來定義列寬。

這樣就完成了,是不是很簡單~~

最後如下

聯繫我們

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