<!--
* @Author: your name
* @Date: 2019-12-04 10:09:49
* @LastEditTime: 2019-12-04 11:56:29
* @LastEditors: Please set LastEditors
* @Description: In User Settings Edit
-->
<template>
<div class="container">
<div class="section" style="height:500px;width:100%" v-for="(item, index) in list" :key="index" :style="{'height':index==0?'1000px':'500px'}">
<div style="width:100%;height:100%;font-size:30px;text-align:center;font-weight:bold;color:red;" :style="{'border':item.border}">{{item.name}}</div>
</div>
<div id="nac" style="height:500px;"></div>
<div style="position:fixed;right:30px;top:300px;">
<a class="nav1" @click="textClick(index)" v-for="(item, index) in navList" :key="index" :class="index==className?'current':''">{{item}}</a>
</div>
</div>
</template>
<script>
export default {
data () {
return {
scroll: '',
list: [{
name: '111',
border: '1px solid red'
}, {
name: '222',
border: '1px solid red'
}, {
name: '333',
border: '1px solid red'
}, {
name: '444',
border: '1px solid red'
}, {
name: '555',
border: '1px solid red'
}],
className: null,
navList: [1, 2, 3, 4, 5]
}
},
methods: {
dataScroll: function () {
this.scroll = document.documentElement.scrollTop || document.body.scrollTop
},
textClick (index) {
let jump = document.getElementsByClassName('section')
let total = jump[index].offsetTop
document.body.scrollTop = total
document.documentElement.scrollTop = total
window.pageYOffset = total
},
loadSroll: function () {
var self = this
var sections = document.getElementsByClassName('section')
for (var i = sections.length - 1; i >= 0; i--) {
if (self.scroll >= sections[i].offsetTop - 100) {
this.className = i
break
}
}
}
},
watch: {
scroll: function () {
this.loadSroll()
}
},
mounted () {
window.addEventListener('scroll', this.dataScroll)
}
}
</script>
<style>
* {
padding: 0;
margin: 0;
}
.nav1 {
display: block;
width: 40px;
height: 40px;
text-align: center;
line-height: 40px;
background: #eee;
margin: 10px 0;
}
.current {
color: #fff;
background: red;
}
</style>
vue关于 锚点标签联动demo
最新推荐文章于 2024-04-03 10:32:00 发布