zoukankan      html  css  js  c++  java
  • vue 实现导航锚点联动

    <!--
     * @Author: ex-jinming002
    -->
    <script>
    import jQuery from 'jquery'
    export default {
      data() {
        return {
          allMenuList: [
            { id: 1, name: '导航', children: [] },
            { id: 2, name: '导航1' },
            { id: 3, name: '导航2' },
            { id: 4, name: '导航3' },
          ],
          activeMenu: 0,
        }
      },
      created() {},
      mounted() {
        const that = this
        // 获取滚动dom元素
        this.scrollBox = document.getElementById('scrollBox')
        const jump = jQuery('.do-jump')
        console.log(jump)
        const topArr = []
        for (let i = 0; i < jump.length; i++) {
          topArr.push(jump.eq(i).position().top)
        }
        // 监听dom元素的scroll事件
        this.scrollBox.addEventListener(
          'scroll',
          () => {
            const current_offset_top = that.scrollBox.scrollTop
            for (let i = 0; i < topArr.length; i++) {
              if (current_offset_top <= topArr[i]) {
                // 根据滚动距离判断应该滚动到第几个导航的位置
                that.activeMenu = i
                break
              }
            }
          },
          true,
        )
      },
      methods: {
        // 跳转
        jump(index) {
          this.activeMenu = index // 当前导航
          const jump = jQuery('.do-jump').eq(index)
          const scrollTop = jump.position().top + this.scrollBox.scrollTop // 获取需要滚动的距离
          // Chrome
          this.scrollBox.scrollTo({
            top: scrollTop,
            behavior: 'smooth', // 平滑滚动
          })
        },
      },
    }
    </script>
    
    <template>
      <div class="NavScroll">
        <div class="all-title">
          全部应用
          <p class="fr">
            <span
              v-for="(item, index) in allMenuList"
              :key="item.id"
              :class="[index === activeMenu ? 'active' : '']"
              @click="jump(index)"
              >{{ item.name }}</span
            >
          </p>
        </div>
        <div id="scrollBox" class="applications-content">
          <div v-for="val in allMenuList" :key="val.id" class="all-list do-jump">
            <p class="applications-title">{{ val.name }}</p>
            <ul class="applications-list">
              <li
                v-for="item in val.children"
                :key="item.id"
                class="applications-item"
                @click="changeRouterForRight(item.pathName, item.menuCode)"
              >
                <img src="" />
                <span>{{ item.name }}</span>
                <template v-if="showEdit">
                  <i
                    v-if="addOrRemove(item.menuCode) == 0"
                    class="el-icon-circle-plus add-btn"
                    @click="addMenu(item.menuCode)"
                  />
                  <i
                    v-if="addOrRemove(item.menuCode) == 1"
                    class="el-icon-remove remove-btn"
                    @click="removeMenu(item.menuCode)"
                  />
                </template>
              </li>
            </ul>
          </div>
        </div>
      </div>
    </template>
    
    <style scoped lang="less">
    .NavScroll {
       800px;
      margin: 20px auto;
      .all-title {
        height: 45px;
        line-height: 45px;
        background-color: red;
        .fr {
          float: right;
          margin: 0 30px;
          > span {
            float: left;
            text-align: center;
            margin-left: 20px;
            cursor: pointer;
          }
          .active {
            &::after {
              content: '';
              display: block;
              height: 0;
              border-bottom: 1px solid black;
            }
          }
        }
      }
      .applications-content {
         100%;
        height: 300px;
        overflow: hidden;
        overflow-y: scroll;
        .all-list {
          height: 200px;
          margin: 10px 0;
          // background-color: blue;
          border: 1px solid #ccc;
        }
      }
    }
    </style>
  • 相关阅读:
    Z-stack之OSAL初始化流程
    回调函数
    VCC,VDD,VEE,VSS,VPP 表示的意义
    ARM内核全解析,从ARM7,ARM9到Cortex-A7,A8,A9,A12,A15到Cortex-A53,A57
    SQL Server 2005 版本的操作系统兼容性详细列表
    Windows Xp Home Edition 安装IIS组件
    MySql 5.7中添加用户,新建数据库,用户授权,删除用户,修改密码
    AtCoder Beginner Contest 077(ABC)
    Codeforces Round #441 (Div. 2, by Moscow Team Olympiad)(A B C D)
    B. Which floor?
  • 原文地址:https://www.cnblogs.com/jinmmm/p/14297195.html
Copyright © 2011-2022 走看看