tab列表滚动切换效果的实现以及分页渲染数据

1,137 阅读2分钟

一、tab列表滚动以及切换的效果

**

1. 点击上面的tab列表的时候下面的内容平滑的切换(scrooTo)

**

注意

  • 次方法是给滚动的元素的父元素调用的,父元素必须是宽高固定,而且有overfoll:scroll属性

  • 内部的高度或者宽度是撑开的

  • 内部的参数有两种格式

  • 一个是两个参数,第一个是x轴的位置,第二个参数是y轴的位置

  • 一个是以对象的格式存在,top,距离顶部的位置,left距离左边的距离,behavior:滚动的方式,支持参数 smooth(平滑滚动),instant(瞬间滚动),默认值auto,实测效果等同于instant

代码如下

<!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>页面切换添加滚动效果</title>
  <style>
    * {
      margin: 0;
      padding: 0;
      list-style: none;
    }
    .wrap {
      width: 100vw;
      height: 100vh;
      overflow: hidden;
      color: #fff;
    }
    .wrap .tabs {
      display: flex;
      justify-content: center;
    }
    .wrap .tabs li {
      width: 33%;
      text-align: center;
      height: 40px;
      line-height: 40px;
    }
    .wrap .tabs li:nth-of-type(2) {
      width: 34%;
    }
    .wrap .con {
      overflow-x: scroll;
    }
    .wrap .con .con-tabs {
      width: 300vw;
      display: flex;
      flex-wrap: nowrap;
    }
    .wrap .con .con-tabs li {
      width: 100vw;
      height: calc(100vh - 40px);
      text-align: center;
      line-height: calc(100vh - 40px);
      font-size: 30px;
    }
  </style>
</head>
<body>
  <div class="wrap">
    <ul class="tabs">
      <li style="background: red;" onclick="move(0)">tab1</li>
      <li style="background: green;" onclick="move(1)">tab2</li>
      <li style="background: blue;" onclick="move(2)">tab3</li>
    </ul>
    <div class="con" >
      <ul class="con-tabs">
        <li style="background: red;">tab1</li>
        <li style="background: green;">tab2</li>
        <li style="background: blue;">tab3</li>
      </ul>
    </div>
  </div>
  <script>
    window.move = (ind) => {
      document.querySelector('.con').scrollTo({
        left: document.querySelector('.con-tabs').children[ind].offsetLeft,
        top: 0,
        behavior: 'smooth'
      });
    }
  </script>
</body>
</html>

2. 页面滑动的时候也进行切换(ontouchstart,ontouchend)

注意

  • 添加这些事件的元素是滚动元素的父元素

  • ontouchstart 设置开始滑动时候的位置

  • ontouchend 滑动事件结束的时候判断是走下一页还是上一页,对第一页和最后一页做处理(这是是按照滑动的距离是页面横向距离的三分之一来判断是否滑动)

  • 理清判断的条件是从右往左滑,还是从左往右滑

代码如下:

    let moveIndex = 0,//设置初始位置的一个下标
      move_horizontal_startX,//设置开始滑动的时候的位置
      containerW = window.innerWidth;//当前页面的宽度
    // 开始滑动的时候记住当前滚动的位置
    function mousedownfun(e) {
      move_horizontal_startX = e.targetTouches[0].pageX;
    }
    // 抬起的时候判断当前的位置滚动到当前的tab列表正确的位置
    function mouseupfun(e) {
      if (e.changedTouches[0].pageX - move_horizontal_startX > containerW / 3) {
         //现在的位置减去开始滑动的时候的位置,从左往右划,显示前一页
         if (moveIndex === 0) return;
         moveIndex--; //上一张
      } else if (move_horizontal_startX - e.changedTouches[0].pageX > containerW / 3) {
        //开始滑动的位置减去现在的位置,从右往左滑,显示下一页
        if (moveIndex >= 2) return;
        moveIndex++; //下一张
      }
      window.move(moveIndex);
    }

二、分页数据的渲染

注意

  • 最外层的盒子添加滚动事件,盒子宽高固定,然后有overflow: scroll的样式
  • 判断条件(当前滚动的距离大于内部盒子的高度 - 外层盒子固定高度-(需要触发分页事件的一个高度))
  • 还有一点就是为了避免重复的请求接口,单独设置一个变量用来判断是否上次请求已经回来了,可以继续请求了

代码如下

<!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>复杂版本的可是区域展示</title>
  <style>
    * {
      margin: 0;
      padding: 0;
      list-style: none;
      text-decoration: none;
    }
    .wrap {
      width: 100%;
      height: 100vh;
      overflow-y: scroll;
    }
    ul li {
      width: 100%;
      height: 100vh;
      text-align: center;
      line-height: 100vh;
      font-size: 50px;
    }
  </style>
</head>
<body>
  <div class="wrap" onscroll="pageadddata(event)">
    <ul>
      <li style="background: red;" issend="false">1</li>
      <li style="background: yellow;" issend="false">2</li>
      <li style="background: green;" issend="false">3</li>
      <li style="background: gray;" issend="false">4</li>
      <li style="background: pink;" issend="false">5</li>
    </ul>
  </div>
  <script>
    let isreq = true
    function pageadddata(e) {
      if (e.target.scrollTop > document.querySelector('ul').offsetHeight - e.target.offsetHeight - 50 && isreq) {
        isreq = false
        // 类似请求接口
        setTimeout(() => {
          let str = document.querySelector('ul').innerHTML
          str += `<li style="background: red;" issend="false">1</li>
            <li style="background: yellow;" issend="false">2</li>
            <li style="background: green;" issend="false">3</li>
            <li style="background: gray;" issend="false">4</li>
            <li style="background: pink;" issend="false">5</li>`
          document.querySelector('ul').innerHTML = str
          isreq = true
        }, 500);
      }
    }
  </script>
</body>
</html>