瀑布流布局&木桶布局

一、實(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對應(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)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲(chǔ)服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,983評論 6 537
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 98,772評論 3 422
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 176,947評論 0 381
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,201評論 1 315
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 71,960評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 55,350評論 1 324
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,406評論 3 444
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 42,549評論 0 289
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 49,104評論 1 335
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 40,914評論 3 356
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 43,089評論 1 371
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,647評論 5 362
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 44,340評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,753評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,007評論 1 289
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 51,834評論 3 395
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 48,106評論 2 375

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

  • 1、實(shí)現(xiàn)一個(gè)瀑布流布局效果 性能效率上的注意點(diǎn): 這種布局方式非常適合動(dòng)態(tài)加載圖片,當(dāng)滾動(dòng)條拉到最下面的時(shí)候可以通...
    饑人谷_阿靖閱讀 514評論 0 0
  • 1.實(shí)現(xiàn)一個(gè)瀑布流布局效果 JQ 瀑布流-1 效果 2.實(shí)現(xiàn)木桶布局效果 JQ 木桶布局 效果 3.實(shí)現(xiàn)一個(gè)新聞瀑...
    饑人谷_米彌輪閱讀 364評論 0 1
  • 題目1: 實(shí)現(xiàn)一個(gè)瀑布流布局效果 瀑布流 題目2:實(shí)現(xiàn)木桶布局效果 木桶布局 題目3:**實(shí)現(xiàn)一個(gè)新聞瀑布流新聞網(wǎng)...
    大大的蘿卜閱讀 213評論 0 0
  • 題目1 瀑布流布局:代碼預(yù)覽 題目2 木桶布局+懶加載+無限加載圖片:代碼預(yù)覽 題目3 新聞瀑布流新聞網(wǎng)站代碼 只...
    從前慢pearl閱讀 239評論 0 0
  • 文wifi過 客 陶行知的 “六大解放”思想,即: 一、解...
    wifi過客閱讀 287評論 0 0