使用 column-count 和 column-gap 属性实现瀑布流, 以及下拉加载更多数据模拟

 

 结果如图

源码

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }

    .box {
      width: 100%;
      padding: 10px;
      column-count: 3;
      column-gap: 10px;
    }

    .box div {
      width: 100%;
      margin-bottom: 10px;
    }

    .box div img {
      width: 100%;
      height: auto;
      vertical-align: middle;
    }

    .footer {
      height: 40px;
      background-color: orange;
    }
  </style>
</head>

<body>
  <div class="box">

  </div>
  <div class="footer"></div>

  <script>
    const box = document.querySelector('.box')
    const footer = document.querySelector('.footer')

    function mock() {
      return './0' + parseInt(Math.random() * 5 + 1) + '.png'
    }

    function loadMore() {
      const list = []
      for (let i = 0; i < 20; i++) {
        list.push(mock())
      }
      let html = ''
      list.forEach(i => {
        const div = document.createElement('div')
        div.innerHTML = `<img src=${i} />`
        box.appendChild(div)
      })
    }

    loadMore()

    var observer = new IntersectionObserver(() => {
      setTimeout(() => {
        loadMore()
      }, 1000)
    }, {});

    observer.observe(footer);

  </script>
</body>

</html>

 

posted @ 2023-03-16 15:56  深海里的星星i  阅读(179)  评论(0)    收藏  举报