瀑布流布局&木桶布局

一、實(shí)現(xiàn)一個(gè)瀑布流布局效果廊散。

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>瀑布流布局</title>
  <style>
    *{
      margin: 0px;
      padding: 0px;
    }
    .waterfall{
      position: relative;
    }
    .item{
      width: 200px;
      position: absolute;
      transition: all .6s;
      margin: 10px;
      color: #383A3F;
      text-align: center;
      font-weight: 600;
    }
    .h1{
      line-height: 200px;
      background: #9DC8C8;
    }
    .h2{
      line-height: 380px;
      background: #D499B9;
    }
    .h3{
      line-height: 250px;
      background: #D7FFF1;
    }
    .h4{
      line-height: 320px;
      background: #F6B352;
    }
    .h5{
      line-height: 450px;
      background: #E71D36;
    }
  </style>
</head>
<body>
  <div class="waterfall">
    <div class="item h1">
        1
    </div>
    <div class="item h2">
        2
    </div>
    <div class="item h1">
        3
    </div>
    <div class="item h4">
        4
    </div>
    <div class="item h2">
        5
    </div>
    <div class="item h1">
        6
    </div>
    <div class="item h3">
        7
    </div>
    <div class="item h5">
        8
    </div>
    <div class="item h3">
        9
    </div>
    <div class="item h1">
        10
    </div>
    <div class="item h2">
        11
    </div>
    <div class="item h4">
        12
    </div>
    <div class="item h5">
        13
    </div>
    <div class="item h1">
        14
    </div>
    <div class="item h3">
        15
    </div>
    <div class="item h1">
        16
    </div>
    <div class="item h2">
        17
    </div>
    <div class="item h4">
        18
    </div>
    <div class="item h5">
        19
    </div>
    <div class="item h1">
        20
    </div>
    <div class="item h3">
        21
    </div>
    <div class="item h2">
        22
    </div>
    <div class="item h4">
        23
    </div>
    <div class="item h5">
        24
    </div>
    <div class="item h2">
        25
    </div>
  </div>

  <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
  <script>
    var Waterfall = {
      arrHeight: [],
      init: function($container) {
        this.$ct = $container;
        this.$items = this.$ct.find('.item');
        this.itemsWidth = this.$items.first().width();
        this.bind();
        this.render();
      },
      bind: function() {
        let that = this;
        $(window).on('resize', function() {
          that.render();
        })
      },
      render: function() {
        let that = this;
        this.arrInit(this.colNum());
        this.$items.each(function() {
          that.placeItem($(this));
        })
      },
      colNum: function() {
        return Math.floor(this.$ct.width() / this.itemsWidth);
      },
      arrInit: function(colNum) {
        for(let i=0; i<colNum; i++) {
          this.arrHeight[i] = 0;
          console.log(this.arrHeight);
        }
      },
      placeItem: function($el) {
        // 1. 找到arrColHeight的最小值桑滩,得到是第幾列
              // 2. 元素left的值是 列數(shù)*寬度
              // 3. 元素top的值是 最小值
              // 4. 放置元素的位置,把a(bǔ)rrColHeight對(duì)應(yīng)的列數(shù)的值加上當(dāng)前元素的高度
        let minHeightInfo = this.getIndexOfMinHeight(this.arrHeight),
            min = minHeightInfo.min,
            idx = minHeightInfo.idx;
        $el.css({
          left: idx*this.itemsWidth,
          top: min
        });
        this.arrHeight[idx] += $el.outerHeight(true);
      },
      getIndexOfMinHeight: function(arr) {
        console.log(arr);
        let min = arr[0],
            idx = 0;
        for(let i = 1; i< arr.length; i++) {
          if(arr[i] < min) {
            min = arr[i];
            idx = i;
          }
        };
        return {min: min, idx: idx};
      }
    }

    Waterfall.init($('.waterfall'));
  </script>
</body>
</html>

二允睹、實(shí)現(xiàn)木桶布局效果运准。

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>木桶布局</title>
  <style>
    *{
      margin: 0;
      padding: 0;
    }
    body{
      background: #f1bbba;
    }
    .barrels{
      width: 1000px;
      margin: 20px auto 0 auto;
    }
    .img-row:after{
      content:"";
      display:block;
      clear:both;
    }
    .img-ct{
      float:left;
    }
    .load{
      visibility: hidden;
      height: 20px;
    }
  </style>
</head>
<body>
  <div class="barrels">

  </div>
  <div class="load">我出現(xiàn)就加載</div>

  <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
  <script>
    var barrels = {
      imgNum: 0,
      standardHeight: 0,
      rowList: [],
      barrelsWidth: 0,

      init: function($barrels, imgNum, standardHeight) {
        this.$barrels = $barrels;
        this.imgNum = imgNum;
        this.standardHeight = standardHeight;
        this.barrelsWidth = this.$barrels.width();
        this.loadImg();
      },

      render: function(imgInfo) {
        let that = this,
            barrelsWidth = this.barrelsWidth,
            // rowList = this.rowList,
            rowWidth = 0,
            newImgHeight = 0,
            lastImgInfo = imgInfo;

        that.rowList.push(lastImgInfo); // 當(dāng)這行圖片沒有超過容器寬度時(shí),則一直放到這一行缭受。
        $.each(that.rowList, function(index, imgInfo) {
          rowWidth += imgInfo.width;
          if(rowWidth > barrelsWidth) {
            that.rowList.pop();
            rowWidth -= imgInfo.width;
            // 重新計(jì)算縮放比例
            newImgHeight = that.standardHeight * barrelsWidth / rowWidth;
            that.appendHtml(newImgHeight);
            that.rowList = [];
            that.rowList.push(lastImgInfo);
          }
        })
      },

      appendHtml: function(newHeight) {
        let $newRow = $('<div class="img-row"></div>'); // 創(chuàng)建新元素
        $.each(this.rowList, function(index, imgInfo) {
          let $newImgCt = $('<div class="img-ct"></div>'),
              $newImg = imgInfo.targetImg;
          $newImg.height(newHeight);
          $newImgCt.append($newImg);
          $newRow.append($newImgCt);
        })
        this.$barrels.append($newRow);
      },

      loadImg: function() {
        let that = this;
        let imgURLs = this.getURL(this.imgNum);
        $.each(imgURLs, function(index, url) {
          let img = new Image();
          img.src = url;
          img.onload = function() {
            let originWidth = img.width,
                originHeight = img.height,
                ratio = originWidth/originHeight;  // 寬度根據(jù)比例進(jìn)行縮放
            let imgInfo = {
              targetImg: $(img),
              width: that.standardHeight*ratio,
              height: that.standardHeight
            }
            that.render(imgInfo);
          };

        })
      },

      getURL: function(imgNum) {
        let width, height, color;
        let urls = [];
        for(let i = 0; i < imgNum;i++) {
          width = Math.floor(Math.random() * 100 + 200);
          height = Math.floor(Math.random() * 100 + 200);
          color = Math.random().toString(16).substring(2,8);
          // urls.push("http://placehold.it/" + width + "x" + height + "/" + color + "/fff");
          urls.push("https://unsplash.it/" + width + "/" + height + "/?random");
        }
        return urls;
      },

      // 判斷.load是否出現(xiàn)
      isVisible: function($node) {
        var windowHeight = $(window).height(),
              scrollTop = $(window).scrollTop(),
              offsetTop = $node.offset().top,
              nodeHeight = $node.outerHeight(true);
        if (windowHeight + scrollTop > offsetTop && scrollTop < offsetTop + nodeHeight) {
            return true;
        }else{
            return false;
        }
      }
    }

    // 一開始的時(shí)候讓圖片鋪滿屏幕
    barrels.init($('.barrels'), 20, 200);

    var clock;
    $(window).on('scroll',function () {
      if (clock) {
        clearTimeout(clock);
      }
      clock = setTimeout(function () {
        if(barrels.isVisible($('.load'))) {
          barrels.init($('.barrels'), 30, 200);
        }
      },200);
    });
  </script>
</body>
</html>

預(yù)覽

三胁澳、實(shí)現(xiàn)一個(gè)新聞瀑布流新聞網(wǎng)站。查看效果

jsonp 接口參數(shù): http://platform.sina.com.cn/slide/album_tech?jsoncallback=func&app_key=1271687855&num=3&page=4
代碼
預(yù)覽

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末米者,一起剝皮案震驚了整個(gè)濱河市韭畸,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌蔓搞,老刑警劉巖陆盘,帶你破解...
    沈念sama閱讀 221,820評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異败明,居然都是意外死亡隘马,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,648評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門妻顶,熙熙樓的掌柜王于貴愁眉苦臉地迎上來酸员,“玉大人蜒车,你說我怎么就攤上這事♂`拢” “怎么了酿愧?”我有些...
    開封第一講書人閱讀 168,324評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)邀泉。 經(jīng)常有香客問我嬉挡,道長(zhǎng),這世上最難降的妖魔是什么汇恤? 我笑而不...
    開封第一講書人閱讀 59,714評(píng)論 1 297
  • 正文 為了忘掉前任庞钢,我火速辦了婚禮,結(jié)果婚禮上因谎,老公的妹妹穿的比我還像新娘基括。我一直安慰自己,他們只是感情好财岔,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,724評(píng)論 6 397
  • 文/花漫 我一把揭開白布风皿。 她就那樣靜靜地躺著,像睡著了一般匠璧。 火紅的嫁衣襯著肌膚如雪桐款。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,328評(píng)論 1 310
  • 那天夷恍,我揣著相機(jī)與錄音魔眨,去河邊找鬼。 笑死裁厅,一個(gè)胖子當(dāng)著我的面吹牛冰沙,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播执虹,決...
    沈念sama閱讀 40,897評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼拓挥,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了袋励?” 一聲冷哼從身側(cè)響起侥啤,我...
    開封第一講書人閱讀 39,804評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎茬故,沒想到半個(gè)月后盖灸,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,345評(píng)論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡磺芭,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,431評(píng)論 3 340
  • 正文 我和宋清朗相戀三年赁炎,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片钾腺。...
    茶點(diǎn)故事閱讀 40,561評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡徙垫,死狀恐怖讥裤,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情姻报,我是刑警寧澤己英,帶...
    沈念sama閱讀 36,238評(píng)論 5 350
  • 正文 年R本政府宣布,位于F島的核電站吴旋,受9級(jí)特大地震影響损肛,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜荣瑟,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,928評(píng)論 3 334
  • 文/蒙蒙 一治拿、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧褂傀,春花似錦忍啤、人聲如沸加勤。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,417評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽鳄梅。三九已至叠国,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間戴尸,已是汗流浹背粟焊。 一陣腳步聲響...
    開封第一講書人閱讀 33,528評(píng)論 1 272
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留孙蒙,地道東北人项棠。 一個(gè)月前我還...
    沈念sama閱讀 48,983評(píng)論 3 376
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像挎峦,于是被迫代替她去往敵國(guó)和親香追。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,573評(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
  • 1.實(shí)現(xiàn)一個(gè)瀑布流布局效果 JQ 瀑布流-1 效果 2.實(shí)現(xiàn)木桶布局效果 JQ 木桶布局 效果 3.實(shí)現(xiàn)一個(gè)新聞瀑...
    饑人谷_米彌輪閱讀 361評(píng)論 0 1
  • 題目1: 實(shí)現(xiàn)一個(gè)瀑布流布局效果 瀑布流 題目2:實(shí)現(xiàn)木桶布局效果 木桶布局 題目3:**實(shí)現(xiàn)一個(gè)新聞瀑布流新聞網(wǎng)...
    大大的蘿卜閱讀 209評(píng)論 0 0
  • 題目1 瀑布流布局:代碼預(yù)覽 題目2 木桶布局+懶加載+無限加載圖片:代碼預(yù)覽 題目3 新聞瀑布流新聞網(wǎng)站代碼 只...
    從前慢pearl閱讀 233評(píng)論 0 0
  • 文wifi過 客 陶行知的 “六大解放”思想峭咒,即: 一、解...
    wifi過客閱讀 284評(píng)論 0 0