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

54 篇文章 2 订阅
47 篇文章 0 订阅

组件挂载首页home.vue

<template>
  <div class="box">
    <div class="content">
      <navbar></navbar>
      <conter></conter>
      <bottom></bottom>
    </div>
    <div class="footer">
      <Myfooter></Myfooter>
    </div>
  </div>
</template>

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>

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

conter.vue

<template>
  <div class="scroll-item box1">
  </div>
</template>

bottom.vue

<template>
  <div class="scroll-item box2">
  </div>
</template>

Myfooter.vue

<template>
  <div class="scroll-item box3">
  </div>
</template>

推荐博主

博主1

博主2

博主3

最后

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

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值