美文网首页前端WebApp系列
点击滚动到指定位置,并且滚动条实现联动效果。

点击滚动到指定位置,并且滚动条实现联动效果。

作者: wppeng | 来源:发表于2021-11-06 16:07 被阅读0次
    效果

    1. 使用插件vue-scrollto

    //安装
    yarn add vue-scrollto
    

    2. 配置(在main.js中配置)

    var VueScrollTo = require('vue-scrollto');
    let app = createApp(App)
    app.use(VueScrollTo,{
        container: ".content",
        duration: 500,
        easing: "ease",
        offset: 0,
        force: true,
        cancelable: true,
        onStart: false,
        onDone: false,
        onCancel: false,
        x: false,
        y: true
    }).mount('#app')
    

    3. 使用

    <template>
      <van-nav-bar title="测试页面" fixed placeholder safe-area-inset-top></van-nav-bar>
      <div class="navbar">
        <div
          class="item"
          v-scroll-to="'#data'"
          :class="active == 0 ? 'active' : ''"
          @click="tapNav(0)"
        >
          <van-icon class-prefix="yzl-icon" name="shenpi1" />
          <span>审批内容</span>
        </div>
        <div
          class="item"
          v-scroll-to="'#flow'"
          :class="active == 1 ? 'active' : ''"
          @click="tapNav(1)"
        >
          <van-icon class-prefix="yzl-icon" name="shenpi" />
          <span>流程状态</span>
        </div>
      </div>
    
      <div class="content" @scroll="handleScroll">
        <div id="data">
          <div class="list" v-for="item in 20" :key="item">
            <div class="item">
              <span>{{ item }}今天天气很好</span>
            </div>
          </div>
        </div>
        <div id="flow">
          <div v-for="item in 40" :key="item">
            <span>{{ item }}流程</span>
          </div>
        </div>
      </div>
    </template>
    
    <script>
    import { NavBar, Icon } from "vant";
    
    export default {
      components: {
        [NavBar.name]: NavBar,
        [Icon.name]: Icon,
      },
      setup() {
        return {};
      },
      data() {
        return {
          active: 0,
        };
      },
    
      methods: {
        handleScroll(e) {
          let dataHeight = e.target.children[0].scrollHeight; //审批内容高度
          let flowHeight = e.target.children[1].scrollHeight; //流程状态高度
          let scrollTop = e.target.scrollTop; //滚动高度
          let clientHeight = e.target.clientHeight; //可见高度
    
          if (flowHeight > clientHeight) {
            //流程状态大于页面
            if (dataHeight > scrollTop) {
              this.active = 0;
            } else {
              this.active = 1;
            }
          } else {
            if (dataHeight - clientHeight > scrollTop) {
              this.active = 0;
            } else {
              this.active = 1;
            }
          }
        },
        tapNav(index) {
          this.active = index;
        },
      },
    };
    </script>
    
    <style lang="scss" scoped>
    .content {
      position: fixed;
      top: 90px;
      bottom: 0;
      left: 0;
      right: 0;
      overflow: auto;
      font-size: 14px;
      #data {
        padding: 0 20px;
        .list {
          padding: 20px;
        }
      }
      #flow {
        padding: 0 20px;
      }
    }
    .navbar {
      position: fixed;
      height: 44px;
      width: 100%;
      display: flex;
      justify-content: space-between;
      align-items: center;
      font-size: 14px;
    
      background: #fff;
      color: #909399;
    
      .item {
        width: 50%;
        display: flex;
        justify-content: center;
        align-items: center;
      }
    }
    
    .active {
      color: #409eff;
    }
    </style>
    
    

    相关文章

      网友评论

        本文标题:点击滚动到指定位置,并且滚动条实现联动效果。

        本文链接:https://www.haomeiwen.com/subject/avbkwltx.html