js原生懒加载

原生js的懒加载

写了一个原生懒加载页面,触底加载、搜索刷新、节流等

在这里插入图片描述

代码如下

<!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>
    .outDiv{
      background-color: aquamarine;
      margin:20px 0;
      position: relative;
    }
    #layuiFa{
      margin:0 auto;
      width: 80%;
      height: 400px;
      background-color: burlywood;
      display: flex;
      overflow: auto;
      flex-wrap: wrap;
    }
    #layuiFa li{
      width:200px;
      height:200px;
      background-color: cornsilk;
      margin:20px;
      list-style: none;
    }
    /* .getMore{
      position:absolute;
      bottom: 0;
      left:50%;
      cursor: pointer;
      background-color: darkgoldenrod;
      display: none;
    } */
    .noMore{
      position:absolute;
      bottom: 0;
      left:50%;
      display: none;
      color: bisque;
    }
  </style>
</head>
<body>
  <div>
    <p>懒加载</p>
    <input type="text" id="inputText"><button onclick="searchItem()">搜搜</button>
    <div class="outDiv">
      <ul id="layuiFa">
        <p class="noMore" id="noMore">没有更多了...</p>
      </ul>
      <!-- <button class="getMore" id="getMore" onclick="getMoreMsg()">加载更多</button> -->
    </div>
  </div>
  <script>
    //保存搜索条件
    let pageMsg ={
      page:1, //当前页
      maxPage:null,//最多搜索到第几页
      asign:'',//搜索内容
      isGetChiild:true,//节流-是否调用接口
    }

    //获取input框内容
    let  getInputValue = function(){
      let inputText = document.getElementById('inputText');
      let textValue = inputText.value;
      //存储input value
      pageMsg.asign = textValue;
    }

    //点击搜索按钮
    let searchItem = function(){
      let layuiFa = document.getElementById('layuiFa');
      let childList = document.querySelectorAll('#layuiFa li')
      childList.forEach(item=>{
        item.remove();
      })
      getChild(1) //重新获取数据
    }

    //后台接口数据
    let getChild = function(index){
      getInputValue();
      let data={
        page:index ? 1: pageMsg.page,//第几页
        asign:pageMsg.asign,//输入框内容
      }

      //当点击搜索时 初始化page
      if(index){
        pageMsg.page = 1;
      }

      let layuiFa = document.getElementById('layuiFa');
      let fragment = document.createDocumentFragment();
      
      //这里去请求接口
      for (var i = 0; i < 20; i++) {
        var li = document.createElement("li");
        li.innerHTML = "index: " + i;
        fragment.appendChild(li);
      }
      layuiFa.appendChild(fragment);

      //更新搜索条件和关闭节流,并且判断是否可以接续加载
      pageMsg.isGetChiild = true;
      pageMsg.page ++;
      pageMsg.maxPage = 5;//这里是写死的假数据
    }

    //判断是否加载更多
    let isCanMore = function(isMore){
      // let getMore =document.getElementById('getMore');
      let noMore =document.getElementById('noMore');
      if(isMore){
        // getMore.style.display = 'block';
        noMore.style.display = 'none';
      }else{
        // getMore.style.display = 'none';
        noMore.style.display = 'block';
      }
    }

    //加载更多方法
    let getMoreMsg = function(){
      getChild();
    }

    //滚动栏事件监听
    let layuiFa =document.getElementById('layuiFa');
    layuiFa.onscroll =function(e){
      // 滚动高度(scrollTop)
      // 实际高度(scrollHeight)
      // 可视高度(offsetHeight)
      let scrollTop = e.target.scrollTop;
      let scrollHeight = e.target.scrollHeight;
      let offsetHeight = Math.ceil(e.target.getBoundingClientRect().height);
      let currentHeight = scrollTop + offsetHeight + 100;
      if(currentHeight > scrollHeight){
        //这里判断触底,并且用节流,判断是否调用接口
        if(pageMsg.isGetChiild && pageMsg.page < pageMsg.maxPage){
          pageMsg.isGetChiild = false;
          getChild();
        }
      }
      
      //展示没有更多
      if(currentHeight > (scrollHeight+90) && pageMsg.page >= pageMsg.maxPage){
        isCanMore(false);
      }else{
        isCanMore(true);
      }
    }
    
    //初始化调用
    getChild()
  </script>
</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是原生js实现图片懒加载的源码: ```javascript // 获取所有需要懒加载的图片元素 const lazyImages = document.querySelectorAll("img.lazy"); // 判断图片是否在可视区域内 function isInViewport(image) { const rect = image.getBoundingClientRect(); return ( rect.top >= 0 && rect.left >= 0 && rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) && rect.right <= (window.innerWidth || document.documentElement.clientWidth) ); } // 加载图片 function loadImage(image) { image.src = image.dataset.src; image.classList.remove("lazy"); } // 监听window的滚动事件 window.addEventListener("scroll", function () { // 遍历需要懒加载的图片元素 for (const image of lazyImages) { if (isInViewport(image)) { loadImage(image); } } }); ``` 首先,我们获取所有需要懒加载的图片元素: ```javascript const lazyImages = document.querySelectorAll("img.lazy"); ``` 接着,我们定义一个函数`isInViewport`用于判断图片是否在可视区域内。该函数会返回一个布尔值,表示图片是否在可视区域内: ```javascript function isInViewport(image) { const rect = image.getBoundingClientRect(); return ( rect.top >= 0 && rect.left >= 0 && rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) && rect.right <= (window.innerWidth || document.documentElement.clientWidth) ); } ``` 然后,我们定义一个函数`loadImage`用于加载图片。该函数会将图片的`src`属性设置为`data-src`属性的值,并移除`lazy`类: ```javascript function loadImage(image) { image.src = image.dataset.src; image.classList.remove("lazy"); } ``` 最后,我们监听`window`的滚动事件,遍历所有需要懒加载的图片元素,如果图片在可视区域内,则调用`loadImage`函数加载图片: ```javascript window.addEventListener("scroll", function () { for (const image of lazyImages) { if (isInViewport(image)) { loadImage(image); } } }); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值