vue 两步实现点击导航栏,滚动页面到指定位置的功能

实现效果

在这里插入图片描述

具体实现步骤

step1:获取不同板块的滚轮高度

当编写好html部分后,我们创建一个可以获取当前滚轮位置的方法handleScroll(),并在mounted钩子函数中添加该方法的监听事件

methods: {
  handleScroll() {
      var scrollTop = document.documentElement.scrollTop;
      this.scrollHeight = scrollTop;
      console.log('当前滚轮高度:',this.scrollHeight); // 使用后记得注释掉哦
  },
},
mounted(){
   window.addEventListener("scroll", this.handleScroll);
}

创建好监听事件后,我们手动滑动网页右侧滚动条到各版块位置,并打印当前板块的滚轮高度

 // 获取到到的各版块滚轮高度
 // area1 scrollHeight == 208.3813934326172
 // area2 scrollHeight == 960.2766723632812
 // area3 scrollHeight == 1637.001708984375
 // area4 scrollHeight == 2305.38330078125

step2:编写执行滚动操作的函数

1、具体思路

  ① 获取到当前滚轮位置后,计算其位置与目标区域位置的高度差 x
  ② 进行 n 次循环,不断减去设定的最小移动单位 dx ,来缩减它们之间的高度差,直至为 0
  ③ 但需要注意的是:因不同位置获取到滚轮的高度不固定,所以 n -1 次减去最小移动单位 dx 后,高度差不一定为零,即 0 < x < dx (此时的高度差:是高度差对最小移动单位取余的余数),所以在执行滚动前,需要先将高度差减去余数,才能保证通过n次循环,刚好能到达指定位置( x = 0)

2、实现方法

  通过定时器进行 n 次循环,高度差不断减去最小移动距离至 0,同时控制滚动条不断靠近目标位置(即向目标位置加/减单位距离)

在这里插入图片描述在这里插入图片描述

这里先定义好需要使用的变量

data(){
	return {
	  scrollHeight:0, //当前滚轮高度
      metaHeight:15,  //最小滚动距离
      metaTime: 10,   //最小执行时间
      targetHeight:0,     //目标区域滚轮高度
	}
}

编写滚动方法

 toArea(index) { // 这里的index是左侧导航栏传的参数,是不同区域设定好的索引值
      switch (index) { // 匹配不同区域的滚轮高度
        case 1: //区域一
          this.targetHeight= 208  //这里将第一步获取到的滚轮高度取整
          break;
        case 2: //区域二
          this.targetHeight= 960
          break;
        case 3: //区域三
          this.targetHeight= 1637
          break;
        case 4: //区域四
          this.targetHeight= 2305
          break;
        default: //默认:区域一
          this.targetHeight= 208
          break;
      }
      // 判断执行情况
      // 当指定区域和当前滚动条位置一致时
      if( (document.documentElement.scrollTop===208&&this.targetHeight === 208)||
          (document.documentElement.scrollTop===960&&this.targetHeight === 960)||
          (document.documentElement.scrollTop===1637&&this.targetHeight === 1637))
      {
        console.log('已经到达该区域了哦')
      }
      // 当指定区域高度大于当前滚动条位置时(即目标区在当前滚轮的下方)
      else if(this.targetHeight> this.scrollHeight){
        // 计算高度差
        let x = this.targetHeight- this.scrollHeight;
        // 先加上余数,保证高度差能整除设定的最小移动单位
        document.documentElement.scrollTop += x%this.metaHeight;
        x -= x%this.metaHeight;
        const goto = setInterval(() => { // 建立执行操作的定时器
          document.documentElement.scrollTop  += this.metaHeight; // 控制移动滚动条
          x-= this.metaHeight; // 缩减高度差
          if (x == 0) { // 到达指定位置后清除定时器
            clearInterval(goto); //清除定时器
          }
        }, this.metaTime);
      }
      // 当指定区域高度小于当前滚动条位置时(即目标区在当前滚轮的上方)
      else{
        // 计算高度差
        let x = this.scrollHeight - this.targetHeight;
        // 先减去余数,保证高度差能整除设定的最小移动单位
        document.documentElement.scrollTop -= x%this.metaHeight;
        x -= x%this.metaHeight;
        const goto = setInterval(() => {
          document.documentElement.scrollTop -= this.metaHeight;
          x-= this.metaHeight;
          if (x == 0) {
            clearInterval(goto); //清除定时器
          }
        }, 1);
      }
    },

为导航栏添加点击事件 @click

<div id="leftNavbar" v-show="scrollHeight > 200">
    <div v-for="(item,index) in navBarList" :key="index" :id="item.id" 
    	@click="toArea(item.index)">
        {{ item.title }}
    </div>
</div>
data(){
return {
	 navBarList:
	 [{
          id:"lnb1",
          title:"教育",
          index:1
        },
        {
          id:"lnb2",
          title:"历史",
          index:2
        },
        {
          id:"lnb3",
          title:"风景",
          index:3
        },
        {
          id:"lnb4",
          title:"文创",
          index:4
        }],
}

OK到这里就实现了在页面不同位置点击左侧导航栏,滚动页面到对应板块(指定位置),并可以自定义滚动操作的速度和距离。各位朋友觉得有帮助到您的话,劳烦点个赞赞鼓励一下吧,嘻嘻。

  • 4
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
可以通过以下几个步骤实现: 1. 在菜单中添加点击事件,获取对应的锚点id。 2. 使用`ref`属性获取到内容容器的元素。 3. 在内容容器上绑定`scroll`事件,监听滚动位置。 4. 根据当前滚动位置,判断哪个锚点处于可视范围内,并高亮对应的菜单项。 具体代码如下: 菜单部分: ``` <template> <div> <ul> <li v-for="item in menu" :key="item.id" @click="scrollToAnchor(item.id)" :class="{active: activeAnchor === item.id}"> {{ item.title }} </li> </ul> </div> </template> <script> export default { data() { return { menu: [ { id: 'a1', title: '锚点1' }, { id: 'a2', title: '锚点2' }, { id: 'a3', title: '锚点3' } ], activeAnchor: '' // 当前激活的锚点id } }, methods: { scrollToAnchor(anchorId) { // 获取对应的锚点元素 const anchorElement = document.getElementById(anchorId); if (anchorElement) { // 滚动到锚点位置 anchorElement.scrollIntoView({ behavior: 'smooth' }); // 设置当前激活的锚点id this.activeAnchor = anchorId; } } } } </script> ``` 内容部分: ``` <template> <div ref="content" @scroll="onScroll"> <div id="a1">锚点1内容</div> <div id="a2">锚点2内容</div> <<div id="a3">锚点3内容</div> </div> </template> <script> export default { methods: { onScroll() { // 获取内容容器的高度和滚动位置 const contentHeight = this.$refs.content.offsetHeight; const scrollTop = this.$refs.content.scrollTop; // 遍历所有锚点,判断哪个处于可视范围内 for (let i = 0; i < this.menu.length; i++) { const anchorId = this.menu[i].id; const anchorElement = document.getElementById(anchorId); if (anchorElement) { const anchorTop = anchorElement.offsetTop; const anchorBottom = anchorTop + anchorElement.offsetHeight; if (scrollTop >= anchorTop && scrollTop < anchorBottom) { // 设置当前激活的锚点id this.$emit('active-anchor', anchorId); break; } } } } } } </script> ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值