瀑布流布局-木桶布局

1.瀑布流

<!doctype html>
<html>

<head>
    <meta charset="utf-8">
    <title>pubuliu</title>
    <style>
        .content {
            font-size: 0;
            position: relative;
        }
        
        .item {
            position: absolute;
            margin: 0;
            padding: 0;
            margin-bottom: 10px;
            width: 100px;
            transition: all 1s;
        }
        
        .h1 {
            background-color: red;
            height: 100px;
        }
        
        .h2 {
            background-color: gray;
            height: 200px;
        }
        
        .h3 {
            background-color: green;
            height: 80px;
        }
        
        .h4 {
            background-color: orange;
            height: 150px;
        }
        
        .h5 {
            background-color: blue;
            height: 90px;
        }
        
        .h6 {
            background-color: yellow;
            height: 100px;
        }
    </style>
</head>

<body>
    <div class="content">
        <div class="item h1"></div>
        <div class="item h2"></div>
        <div class="item h3"></div>
        <div class="item h4"></div>
        <div class="item h5"></div>
        <div class="item h6"></div>
        <div class="item h1"></div>
        <div class="item h2"></div>
        <div class="item h3"></div>
        <div class="item h4"></div>
        <div class="item h5"></div>
        <div class="item h6"></div>
        <div class="item h1"></div>
        <div class="item h2"></div>
        <div class="item h3"></div>
        <div class="item h4"></div>
        <div class="item h5"></div>
        <div class="item h6"></div>
    </div>

    <script src="https://code.jquery.com/jquery-3.2.0.js"></script>
    <script>
        var $ct = $('.content');
        var $items = $('.item')
        var itemWidth = $items.outerWidth(true);
        var listArr = [];
        $(window).resize(function () {
            waterFull();
        })
        waterFull();
        function waterFull() {
            var waterLength = parseInt(($ct.width()) / itemWidth);
            for (var i = 0; i < waterLength; i++) {
                listArr[i] = 0;
            }
            $items.each(function () {
                console.log(listArr)
                var minValue = Math.min.apply(null, listArr);
                // console.log(minValue);
                var minIndex = listArr.indexOf(minValue);
                $(this).css({ left: minIndex * itemWidth, top: minValue })
                // console.log(minValue,$(this).outerHeight(true));
                listArr[minIndex] = minValue + $(this).outerHeight(true);
                // console.log(listArr[minIndex]);
            })
        }
    </script>
</body>

</html>

2.木桶布局(老師质帅,我做的是隨著窗口的擴(kuò)大或者減小,重新布局,但是第一次加載的圖片煤惩,跟擴(kuò)大或者縮小的重新布局圖片位置不一樣嫉嘀,我認(rèn)為是第一次加載圖片監(jiān)聽onload第一張不是得到的真正的第一張圖片,后面在擴(kuò)大或者縮小已經(jīng)緩存了盟庞,所以圖片是正確位置吃沪。所以想問下老師汤善,如何讓第一次加載的圖片位置跟以后我重新布局的圖片位置一樣)什猖。能提供下思路嗎,謝謝老師红淡!

<!doctype html>
<html>

<head>
    <meta charset="utf-8">
    <title>mutongbuju</title>
</head>
<style>
    .ct {
        position: relative;
        /*width: 1000px;*/
        /*margin: 0 auto;*/
    }
    
    .ct:after {
        content: '';
        display: block;
        clear: both;
    }
    
    .box {
        float: left;
        font-size: 0;
        margin: 0;
        padding: 0;
        margin-bottom: 10px;
        height: 100px;
        transition: all 1s;
    }
    
    .box>img {
        height: 100%;
    }
</style>

<body>
    <div class="ct">

    </div>
    <script src="https://code.jquery.com/jquery-3.2.0.js"></script>
    <script>
        var $ct = $('.ct');
        
        var urlsArr = getImgUrls(40);
        var clearCt = true;
        var firstLoad = true;
        $(window).resize(function () {
            $ct.empty();
            var load = new loadImg();
        })
        function getImgUrls(num) {
            var color, width, height, urls = [];
            for (var i = 0; i < num; i++) {
                color = Math.random().toString(16).substring(2, 8);
                width = Math.floor(Math.random() * 100 + 50);
                height = Math.floor(Math.random() * 30 + 50);
                urls.push("http://placehold.it/" + width + "x" + height + "/" + color + "/fff");
            }
            return urls;
        }


        function loadImg() {
            this.urlsArr = urlsArr;
            this.listArr = [];
            this.rowWidth = 0;
            this.handleImg();

        }
        loadImg.prototype.handleImg = function () {
            var xxx = 0;
            _this = this;
            $(_this.urlsArr).each(function (index, ele) {
                var imgg = new Image();
                imgg.src = this;

                imgg.onload = function () {
                    xxx++;
                    console.log(imgg.src)
                    var imgWidth = 100 * imgg.width / imgg.height;
                    var newRowHeight;
                    _this.rowWidth += imgWidth;
                    //   console.log(rowWidth)
                    _this.listArr.push($(imgg))
                    if (_this.rowWidth > $ct.width()) {

                        _this.listArr.pop();
                        //    console.log(xxx,listArr.length)
                        _this.rowWidth -= imgWidth;
                        newRowHeight = 100 * $ct.width() / _this.rowWidth;
                        for (var i = 0; i < _this.listArr.length; i++) {
                            var $box = $('<div class="box"></div>')
                            $box.height(newRowHeight);
                            $box.append(_this.listArr[i])
                            $ct.append($box)
                            // if(clearCt){
                            //     $ct.empty();
                                
                            // }
                            console.log(_this.listArr[i])
                        }
                        //   console.log(listArr)
                        _this.listArr = [];
                        _this.listArr.push($(imgg))
                        _this.rowWidth = imgWidth;
                    }

                    //如果圖片數(shù)量有限會(huì)走下面這個(gè)方法處理不狮。否則會(huì)少幾張顯示
                    if (index === _this.urlsArr.length - 1) {
                        
                        for (var i = 0; i < _this.listArr.length; i++) {
                            var $box = $('<div class="box"></div>')
                            $box.height(100);
                            $box.append(_this.listArr[i])
                            $ct.append($box)
                            // if(clearCt){
                            //     $ct.empty();
                                
                            // }
                            //   console.log(listArr[i])
                        }
                       
                    }
                    // clearCt = false
                    // if(firstLoad){
                    //     var load = new loadImg();
                    //     firstLoad = false;
                    // }

                }
            })
        }

        var load = new loadImg();
        
        // var load = new loadImg();
    </script>
</body>

</html>

3.新聞瀑布流

<!doctype html>
<html>

<head>
    <meta charset="utf-8">
    <style>
        .contain {
            width: 676px;
            margin: 0 auto;
        }
        
        .waterfull {
            list-style: none;
            padding: 0;
            margin: 0;
            position: relative;
            margin-left: -20px;
        }
        
        .waterfull:after {
            content: '';
            display: block;
            clear: both;
        }
        
        .waterfull>li {
            border: 1px solid #ccc;
            width: 200px;
            padding: 5px;
            margin-bottom: 20px;
            margin-left: 20px;
            position: absolute;
            /*float: left;*/
        }
        
        .waterfull>li img {
            width: 100%;
        }
        
        .waterfull>li>h3 {
            padding: 0;
            margin: 0;
            margin-top: 5px;
        }
        
        .waterfull>li>p {
            padding: 0;
            margin: 0;
            margin-top: 5px;
            color: #ccc;
            font-size: 12px;
        }
        
        .contain>p {
            visibility: hidden;
        }
    </style>
</head>

<body>
    <div class="contain">
        <ul class="waterfull">
            <!--<li>
                    <a href="javascript:;">![](http://upload-images.jianshu.io/upload_images/1909214-37eb4e4c3ebad450.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)</a>
                    <h3>NASA計(jì)劃研發(fā)金星漫游車</h5>
                    <p>美國(guó)航空航天局(NASA)披露了研制金星漫游車的計(jì)劃。這臺(tái)漫游車將完全不依賴電能在旱,而是利用渦輪采集的風(fēng)能進(jìn)行工作摇零。</p>
                </li>-->
        </ul>
        <p>加載更多</p>
    </div>
    <script src="https://code.jquery.com/jquery-3.2.0.js"></script>
    <script>
        var page = 1;
        var loading = false;
        var listArr = [0, 0, 0]

        //jsonp回調(diào)函數(shù)
        function func(obj) {
            for (var i = 0; i < obj.data.length; i++) {
                var object = obj.data[i]
                //創(chuàng)建li
                var $li = getNode(object) 
                //監(jiān)聽圖片下載完成,然后設(shè)置高度
               //這個(gè)是領(lǐng)寫的函數(shù)桶蝎,直接寫會(huì)莫名其妙的問題,就是append只拼接了一個(gè)
               //for循環(huán)里有回調(diào)函數(shù)驻仅,都是坑  還是多用each,用另一個(gè)函數(shù)包裹
                water($li)
            }    

            //成功以后登渣,設(shè)置繼續(xù)可以請(qǐng)求數(shù)據(jù)
            loading = false;
            //頁(yè)數(shù)加一
            page++;
        }
        function getNode(object) {
           return  $('<li>\
                    <a href="'+ object.url + '">![](' + object.img_url + ')</a>\
                    <h3>'+ object.short_name + '</h5>\
                    <p>'+ object.name + '</p>\
                </li>')
        }
        //重點(diǎn) 折騰了好長(zhǎng)時(shí)間
        function water($li){
            $li.find('img').on('load', function () {
                    $('.waterfull').append($li)
                    //以下等append以后才再取值噪服,否則娶不到 $li.outerHeight(true)
                    // console.log($li)
                    var minValue = Math.min.apply(null, listArr)
                    var minIndex = listArr.indexOf(minValue);
                    //  console.log(111,listArr)
                    $li.css({ left: minIndex * $li.outerWidth(true), top: minValue })
                    listArr[minIndex] = $li.outerHeight(true)+minValue;
                    
                    //算出父容器高度,把p標(biāo)簽弄下去
                    $('.waterfull').height(Math.max.apply(null,listArr));
                    
                    console.log(listArr, listArr[minIndex],$li.outerHeight(true))
                })
        }

        //第一次請(qǐng)求數(shù)據(jù)
        loadNews();

        
        function createScript() {
            var scri = document.createElement('script')
            scri.src = 'http://platform.sina.com.cn/slide/album_tech?jsoncallback=func&app_key=1271687855&num=10&page=' + page
            document.body.appendChild(scri)
            document.body.removeChild(scri)
        }

        ///窗口滾動(dòng)時(shí)
        $(window).on('scroll', function () {
            if(isShowP($('.contain>p'))){
               loadNews();
            }
            
        })

        //請(qǐng)求數(shù)據(jù)
        function loadNews() {
            console.log(loading)
            if (loading === true) return;
            loading = true;
            if(isShowP($('.contain>p'))){

             createScript()
            //  loadNews();
            }

        }
        function isShowP($node) {
            var scroTop = $(window).scrollTop();
            var windowHeight = $(window).height();
            var offsetT = $node.offset().top;
            var pHeight = $node.height();
            if (scroTop > offsetT - windowHeight) {
                return true;
            }
            return false;

        }
    </script>



</body>

</html>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末胜茧,一起剝皮案震驚了整個(gè)濱河市粘优,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌呻顽,老刑警劉巖雹顺,帶你破解...
    沈念sama閱讀 221,695評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異廊遍,居然都是意外死亡嬉愧,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,569評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門喉前,熙熙樓的掌柜王于貴愁眉苦臉地迎上來没酣,“玉大人,你說我怎么就攤上這事被饿∷目担” “怎么了?”我有些...
    開封第一講書人閱讀 168,130評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵狭握,是天一觀的道長(zhǎng)闪金。 經(jīng)常有香客問我,道長(zhǎng),這世上最難降的妖魔是什么哎垦? 我笑而不...
    開封第一講書人閱讀 59,648評(píng)論 1 297
  • 正文 為了忘掉前任囱嫩,我火速辦了婚禮,結(jié)果婚禮上漏设,老公的妹妹穿的比我還像新娘墨闲。我一直安慰自己,他們只是感情好郑口,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,655評(píng)論 6 397
  • 文/花漫 我一把揭開白布鸳碧。 她就那樣靜靜地躺著,像睡著了一般犬性。 火紅的嫁衣襯著肌膚如雪瞻离。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,268評(píng)論 1 309
  • 那天乒裆,我揣著相機(jī)與錄音套利,去河邊找鬼。 笑死鹤耍,一個(gè)胖子當(dāng)著我的面吹牛肉迫,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播稿黄,決...
    沈念sama閱讀 40,835評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼喊衫,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了抛猖?” 一聲冷哼從身側(cè)響起格侯,我...
    開封第一講書人閱讀 39,740評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎财著,沒想到半個(gè)月后联四,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,286評(píng)論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡撑教,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,375評(píng)論 3 340
  • 正文 我和宋清朗相戀三年朝墩,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片伟姐。...
    茶點(diǎn)故事閱讀 40,505評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡收苏,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出愤兵,到底是詐尸還是另有隱情鹿霸,我是刑警寧澤,帶...
    沈念sama閱讀 36,185評(píng)論 5 350
  • 正文 年R本政府宣布秆乳,位于F島的核電站懦鼠,受9級(jí)特大地震影響钻哩,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜肛冶,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,873評(píng)論 3 333
  • 文/蒙蒙 一街氢、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧睦袖,春花似錦珊肃、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,357評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至延蟹,卻和暖如春评矩,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背阱飘。 一陣腳步聲響...
    開封第一講書人閱讀 33,466評(píng)論 1 272
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留虱颗,地道東北人沥匈。 一個(gè)月前我還...
    沈念sama閱讀 48,921評(píng)論 3 376
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像忘渔,于是被迫代替她去往敵國(guó)和親高帖。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,515評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容

  • 1、實(shí)現(xiàn)一個(gè)瀑布流布局效果 性能效率上的注意點(diǎn): 這種布局方式非常適合動(dòng)態(tài)加載圖片宣赔,當(dāng)滾動(dòng)條拉到最下面的時(shí)候可以通...
    饑人谷_阿靖閱讀 502評(píng)論 0 0
  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,286評(píng)論 25 707
  • 題目1 瀑布流布局:代碼預(yù)覽 題目2 木桶布局+懶加載+無限加載圖片:代碼預(yù)覽 題目3 新聞瀑布流新聞網(wǎng)站代碼 只...
    從前慢pearl閱讀 233評(píng)論 0 0
  • 實(shí)現(xiàn)一個(gè)瀑布流布局效果 原理:利用絕對(duì)定位预麸,位置可能不是按照1,2,3順序排列1、計(jì)算容器內(nèi)一行可以放置多少元素儒将,...
    101架鋼琴閱讀 238評(píng)論 0 0
  • 題目1: 實(shí)現(xiàn)一個(gè)瀑布流布局效果 瀑布流 題目2:實(shí)現(xiàn)木桶布局效果 木桶布局 題目3:**實(shí)現(xiàn)一個(gè)新聞瀑布流新聞網(wǎng)...
    大大的蘿卜閱讀 209評(píng)論 0 0