• 前端vue实现目录锚点跳转及滚动到指定内容区域(带源码)


    组件挂载首页home.vue

    <template>
      <div class="box">
        <div class="content">
          <navbar></navbar>
          <conter></conter>
          <bottom></bottom>
        </div>
        <div class="footer">
          <Myfooter></Myfooter>
        </div>
      </div>
    </template>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    navbar.vue主要方法在这里面

    <template>
      <div>
        <div v-for="(item,index) in title_list" :key="item.id" :value='item.title' @click="jump(index)" class="list1">
          {{item.title}}
        </div>
      </div>
    </template>
    
    <script>
    export default {
      data () {
        return {
          title_list: [
            { title: '撒旦法', active: true },
            { title: 'sdfwe', active: false },
            { title: '官方规划', active: false },
            { title: '官方规是划', active: false },
          ],
        }
      },
      methods: {
        jump (index) {
          var items = document.querySelectorAll(".scroll-item"); // 给每个组件最外层加个class名字scroll-item
          for (var i = 0; i < items.length; i++) {
            if (index === i) {
              items[i].scrollIntoView({
                block: "start",//默认跳转到顶部
                behavior: "smooth"//滚动的速度
              });
            }
          }
    
        },
        // onScroll (e) {
        //   let scrollItems = document.querySelectorAll(".scroll-item");
        //   for (let i = scrollItems.length - 1; i >= 0; i--) {
        //     // 判断滚动条滚动距离是否大于当前滚动项可滚动距离
        //     let judge =
        //       e.target.scrollTop >=
        //       scrollItems[i].offsetTop - scrollItems[0].offsetTop;
        //     if (judge) {
        //       this.activeStep = i;
        //       break;
        //     }
        //   }
        // },
      }
    }
    </script>
    
    <style lang="less" scoped>
    .list1 {
      height: 100%;
      display: flex;
      flex-direction: row-reverse;
      justify-content: space-between;
      align-content: center;
      flex-wrap: wrap;
      cursor: pointer;
      font-family: MicrosoftYaHeiLight;
      font-size: 18px;
      line-height: 30px;
      letter-spacing: 0px;
      /* Mono/Black */
      color: #000000;
      div {
        height: 30px;
      }
    }
    </style>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70

    然后再给每个组件最外层加个class名字scroll-item

    conter.vue

    <template>
      <div class="scroll-item box1">
      </div>
    </template>
    
    • 1
    • 2
    • 3
    • 4

    bottom.vue

    <template>
      <div class="scroll-item box2">
      </div>
    </template>
    
    • 1
    • 2
    • 3
    • 4

    Myfooter.vue

    <template>
      <div class="scroll-item box3">
      </div>
    </template>
    
    • 1
    • 2
    • 3
    • 4

    推荐博主

    博主1

    博主2

    博主3

    最后

    感觉文章好的话记得点个心心和关注和收藏,有错的地方麻烦指正一下,如果需要转载,请标明出处,多谢!!!

  • 相关阅读:
    为 Go 开发配置Visual Studio Code
    浪漫表白编程丨程序员的520表白代码 _ 程序员专属情人节表白网站
    缓冲区设置
    Linux虚拟机
    Redis快速入门
    二分查找及例题
    画面消失,但功能还在正常实现,窗口绘制出了问题
    【云原生Kubernetes】二进制搭建Kubernetes集群(中)——部署node节点
    集群外Prometheus 集群 k8s
    View 的四种 OnClick 方式
  • 原文地址:https://blog.csdn.net/m0_49714202/article/details/125459297