html锚点反向联动,vue 实现导航锚点联动

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', // 平滑滚动

})

},

},

}

.NavScroll {

width: 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 {

width: 100%;

height: 300px;

overflow: hidden;

overflow-y: scroll;

.all-list {

height: 200px;

margin: 10px 0;

// background-color: blue;

border: 1px solid #ccc;

}

}

}

标签:vue,const,name,height,jump,scrollBox,锚点,联动,margin

来源: https://www.cnblogs.com/jinmmm/p/14297195.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值