瀑布流布局

  • 寫法一:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>瀑布流原始寫法</title>
    <script src="http://apps.bdimg.com/libs/jquery/1.9.1/jquery.min.js"></script>
    <style>
        .content {
            position: relative;
        }

        .item {
            position: absolute;
            width: 200px;
            margin-right: 10px;
            margin-top: 10px;
            transition: all 1s;
        }

        .h1 {
            height: 200px;
            background-color: #f4b300;
        }

        .h2 {
            height: 300px;
            background-color:  #691BB8;
        }

        .h3 {
            height: 400px;
            background-color:  #006ac1;
        }
    </style>
</head>
<body>
    <div class="content">
        <div class="item h1">1</div>
        <div class="item h3">2</div>
        <div class="item h2">3</div>
        <div class="item h1">4</div>
        <div class="item h1">5</div>
        <div class="item h3">6</div>
        <div class="item h3">7</div>
        <div class="item h2">8</div>
        <div class="item h1">9</div>
        <div class="item h3">10</div>
        <div class="item h3">11</div>
        <div class="item h2">12</div>
        <div class="item h2">13</div>
        <div class="item h2">14</div>
    </div>

    <script>
        function render(){

            var nodeWidth = $('.item').outerWidth(true),  //節(jié)點的寬度。.outerWidth(true)包含外邊距和邊框的寬度
                colNum = parseInt($(window).width() / nodeWidth),  //計算當前有幾列殉农。parseInt()方法取整數
                colSumHeight = [];  //定義一個空數組册倒,用于計算列的高度之和

            for(var i=0; i<colNum; i++){
                colSumHeight.push(0);  //定義初始高度0寞蚌。.push()在數組的末尾添加數值0
            }

            $('.item').each(function(){
                var $cur = $(this);

                //colSumHeight = [100, 250, 80, 200] 假設目前一行元素的高度

                //聲明下標第0位,聲明minSumHeight的最小高度是0
                var idx = 0,
                    minSumHeight = colSumHeight[0];

                for(var i=0; i<colSumHeight.length; i++){
                    //用于替換下標和最小高度
                    if(colSumHeight[i] < minSumHeight){
                    var idx = i,
                        minSumHeight = colSumHeight[i];
                    }
                }
                //調整替換過的.item的位置
                $cur.css({
                    left: nodeWidth*idx,
                    top: minSumHeight
                });
                //疊加后的高度=當前元素的高度+相加前的高度
                colSumHeight[idx] = $cur.outerHeight(true) + colSumHeight[idx];
            });    
        }

        render();

        $(window).on('resize', function(){
            render();
        });
    </script>
</body>
</html>
  • 寫法二:
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>瀑布流封裝寫法一</title>
    <script src="http://apps.bdimg.com/libs/jquery/1.9.1/jquery.min.js"></script>
    <style>
        .content {
            position: relative;
        }

        .item {
            position: absolute;
            width: 200px;
            margin-right: 10px;
            margin-top: 10px;
            transition: all 1s;
        }

        .h1 {
            height: 200px;
            background-color: #f4b300;
        }

        .h2 {
            height: 300px;
            background-color: #691BB8;
        }

        .h3 {
            height: 400px;
            background-color: #006ac1;
        }
    </style>
</head>

<body>
    <div class="content">
        <div class="item h1">1</div>
        <div class="item h3">2</div>
        <div class="item h2">3</div>
        <div class="item h1">4</div>
        <div class="item h1">5</div>
        <div class="item h3">6</div>
        <div class="item h3">7</div>
        <div class="item h2">8</div>
        <div class="item h1">9</div>
        <div class="item h3">10</div>
        <div class="item h3">11</div>
        <div class="item h2">12</div>
        <div class="item h2">13</div>
        <div class="item h2">14</div>
    </div>

    <script>
        var water = (function () {

            function render() {

                var nodeWidth = $('.item').outerWidth(true),  //節(jié)點的寬度倒源。.outerWidth(true)包含外邊距和邊框的寬度
                    colNum = parseInt($(window).width() / nodeWidth),  //計算當前有幾列。parseInt()方法取整數
                    colSumHeight = [];  //定義一個空數組酱塔,用于計算列的高度之和

                for (var i = 0; i < colNum; i++) {
                    colSumHeight.push(0);  //定義初始高度0(即第一列每個元素的初始高度都是0)绳矩。.push()在數組的末尾添加數值0
                }

                $('.item').each(function () {
                    var $cur = $(this);

                    //colSumHeight = [100, 250, 80, 200] 假設目前一行元素的高度

                    //聲明下標第0位,聲明minSumHeight的最小高度是0
                    var idx = 0,
                        minSumHeight = colSumHeight[0];

                    for (var i = 0; i < colSumHeight.length; i++) {
                        //用于替換下標和最小高度
                        if (colSumHeight[i] < minSumHeight) {
                            var idx = i,
                                minSumHeight = colSumHeight[i];
                        }
                    }
                    //調整替換過的.item的位置
                    $cur.css({
                        left: nodeWidth * idx,
                        top: minSumHeight
                    });
                    //疊加后的高度=當前元素的高度+相加前的高度
                    colSumHeight[idx] = $cur.outerHeight(true) + colSumHeight[idx];
                });
            }

            render();

            $(window).on('resize', function () {
                render();
            });

            /*render函數放在了要return出去的對象water的init屬性里(retrun出一個接口)
            water.init = water[init] = render
            如果直接寫成init: render(),那init就是render函數執(zhí)行之后的結果
            render函數執(zhí)行的返回值是undefined,所以init: undefined*/
            return {
                init: render
            }
        })();
        console.log(water);
        water.init();
    </script>
</body>

</html>
  • 寫法三:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>瀑布流封裝寫法二</title>
    <script src="http://apps.bdimg.com/libs/jquery/1.9.1/jquery.min.js"></script>
    <style>
        .content {
            position: relative;
        }

        .item {
            position: absolute;
            width: 200px;
            margin-right: 10px;
            margin-top: 10px;
            transition: all 1s;
        }

        .h1 {
            height: 200px;
            background-color: #f4b300;
        }

        .h2 {
            height: 300px;
            background-color:  #691BB8;
        }

        .h3 {
            height: 400px;
            background-color:  #006ac1;
        }
    </style>
</head>
<body>
    <div class="content">
        <div class="item h1">1</div>
        <div class="item h3">2</div>
        <div class="item h2">3</div>
        <div class="item h1">4</div>
        <div class="item h1">5</div>
        <div class="item h3">6</div>
        <div class="item h3">7</div>
        <div class="item h2">8</div>
        <div class="item h1">9</div>
        <div class="item h3">10</div>
        <div class="item h3">11</div>
        <div class="item h2">12</div>
        <div class="item h2">13</div>
        <div class="item h2">14</div>
    </div>

    <script>
        //定義一個對象
        var WaterFall = {

            //高度[0, 0, 0, 0]
            //高度[20, 10, 30, 15]

            arrColHeight: [],

            init: function($ct){
                this.$ct = $ct;
            }
        }

        //調用時傳入根節(jié)點
        WaterFall.init($('.content'));
    </script>
</body>
</html>
?著作權歸作者所有,轉載或內容合作請聯系作者
  • 序言:七十年代末疟呐,一起剝皮案震驚了整個濱河市脚曾,隨后出現的幾起案子,更是在濱河造成了極大的恐慌启具,老刑警劉巖本讥,帶你破解...
    沈念sama閱讀 221,888評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現場離奇詭異鲁冯,居然都是意外死亡拷沸,警方通過查閱死者的電腦和手機,發(fā)現死者居然都...
    沈念sama閱讀 94,677評論 3 399
  • 文/潘曉璐 我一進店門薯演,熙熙樓的掌柜王于貴愁眉苦臉地迎上來堵漱,“玉大人,你說我怎么就攤上這事涣仿∏诼” “怎么了?”我有些...
    開封第一講書人閱讀 168,386評論 0 360
  • 文/不壞的土叔 我叫張陵好港,是天一觀的道長愉镰。 經常有香客問我,道長钧汹,這世上最難降的妖魔是什么丈探? 我笑而不...
    開封第一講書人閱讀 59,726評論 1 297
  • 正文 為了忘掉前任,我火速辦了婚禮拔莱,結果婚禮上碗降,老公的妹妹穿的比我還像新娘隘竭。我一直安慰自己,他們只是感情好讼渊,可當我...
    茶點故事閱讀 68,729評論 6 397
  • 文/花漫 我一把揭開白布动看。 她就那樣靜靜地躺著,像睡著了一般爪幻。 火紅的嫁衣襯著肌膚如雪菱皆。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,337評論 1 310
  • 那天挨稿,我揣著相機與錄音仇轻,去河邊找鬼。 笑死奶甘,一個胖子當著我的面吹牛篷店,可吹牛的內容都是我干的。 我是一名探鬼主播臭家,決...
    沈念sama閱讀 40,902評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼疲陕,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了侣监?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 39,807評論 0 276
  • 序言:老撾萬榮一對情侶失蹤臣淤,失蹤者是張志新(化名)和其女友劉穎橄霉,沒想到半個月后,有當地人在樹林里發(fā)現了一具尸體邑蒋,經...
    沈念sama閱讀 46,349評論 1 318
  • 正文 獨居荒郊野嶺守林人離奇死亡姓蜂,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 38,439評論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現自己被綠了医吊。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片钱慢。...
    茶點故事閱讀 40,567評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖卿堂,靈堂內的尸體忽然破棺而出束莫,到底是詐尸還是另有隱情,我是刑警寧澤草描,帶...
    沈念sama閱讀 36,242評論 5 350
  • 正文 年R本政府宣布览绿,位于F島的核電站,受9級特大地震影響穗慕,放射性物質發(fā)生泄漏饿敲。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,933評論 3 334
  • 文/蒙蒙 一逛绵、第九天 我趴在偏房一處隱蔽的房頂上張望怀各。 院中可真熱鬧倔韭,春花似錦、人聲如沸瓢对。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,420評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽沥曹。三九已至份名,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間妓美,已是汗流浹背僵腺。 一陣腳步聲響...
    開封第一講書人閱讀 33,531評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留壶栋,地道東北人辰如。 一個月前我還...
    沈念sama閱讀 48,995評論 3 377
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子蜡娶,可洞房花燭夜當晚...
    茶點故事閱讀 45,585評論 2 359

推薦閱讀更多精彩內容