当前位置:网站首页>锚点导航小demo
锚点导航小demo
2022-07-05 13:47:00 【wade3po】
锚点导航这种功能应该很常见,早之前就分享过移动端移动端导航简单实现,配合起来就很容易实现可移动的锚点导航,而且不是用hash模式。当然,PC端也能直接用。
先上效果:
为了方便直接用vue语法,首先要给每一个块一个唯一的ref,然后初始化的时候记录每一个块距离顶部的距离和每一个块的高度,并监听滚动:
this.$nextTick(() => {
this.firstOffsetTop = this.$refs[this.currentKey][0].offsetTop;
document.addEventListener('scroll', this.onScroll);
this.list.forEach((val) => {
this.itemOffsetTop.push({
key: val.key,
num: this.$refs[val.key][0].offsetTop - this.firstOffsetTop,
height: this.$refs[val.key][0].clientHeight
});
});
});
最重要的三个方法:
onScroll(){
const scrollTop = document.documentElement.scrollTop;
this.itemOffsetTop.forEach((val, index) => {
if(scrollTop > val.num && scrollTop - val.num > val.height / 2){
if(index == this.itemOffsetTop.length - 1){
this.currentKey = this.itemOffsetTop[index].key;
}else {
this.currentKey = this.itemOffsetTop[index + 1].key;
}
}else if(scrollTop < this.itemOffsetTop[0].height / 2){
this.currentKey = this.itemOffsetTop[0].key;
}
})
},
//点击锚点定位
changeKey(key){
document.removeEventListener('scroll', this.throttledScrollHandler);
this.currentKey = key;
let to = this.$refs[key][0].offsetTop - this.firstOffsetTop;
this.animationScrollTo(document.documentElement, to);
},
//锚点定位动画滚动
animationScrollTo(el, to) {
let scrollY = 0;
const beginY = el.scrollTop;
const raf = window.requestAnimationFrame || (func => setTimeout(func, 10));
const moveFn = () => {
if (beginY - to < 0) {
scrollY += (to - beginY) / 30;
if(scrollY <= (to - beginY)){
el.scrollTop = beginY + scrollY;
raf(moveFn);
}else {
el.scrollTop = to;
document.addEventListener('scroll', this.throttledScrollHandler);
}
}else if(beginY - to > 0){
scrollY += (beginY - to) / 30;
if(scrollY <= (beginY - to)){
el.scrollTop = beginY - scrollY;
raf(moveFn);
}else {
el.scrollTop = to;
document.addEventListener('scroll', this.throttledScrollHandler);
}
}
};
raf(moveFn);
}
先判断上滑还是下滑,每次滑动距离除以30,当作滑动的速度,这边注意一点,如果滑动距离为0的情况下是要处理的,否则滚动条会卡住,之前就是一个变量判断是否大于0,结果滚动条卡住了。滚动加点动画效果直接用requestAnimationFrame ,一般都兼容,如果不兼容就用定时器,定时器会比较不顺滑。
监听滚动计算滚动的距离是否大于记录的距离,且超过一半就算下一个,菜单上面定位到下一个。
完整demo:
https://github.com/wade3po/demoCode
锚点导航1.html是更简便,不过兼容性不一定更好。

边栏推荐
- Redis6 data type and operation summary
- These 18 websites can make your page background cool
- Multi person cooperation project to see how many lines of code each person has written
- Can graduate students not learn English? As long as the score of postgraduate entrance examination English or CET-6 is high!
- When using Tencent cloud for the first time, you can only use webshell connection instead of SSH connection.
- The "Baidu Cup" CTF competition was held in February 2017, Web: explosion-2
- [server data recovery] a case of RAID5 data recovery stored in a brand of server
- Assembly language - Beginner's introduction
- 【Hot100】34. Find the first and last positions of elements in a sorted array
- Interviewer soul torture: why does the code specification require SQL statements not to have too many joins?
猜你喜欢
![[public class preview]: basis and practice of video quality evaluation](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[public class preview]: basis and practice of video quality evaluation

Attack and defense world web WP

Flutter 3.0更新后如何应用到小程序开发中

龙芯派2代烧写PMON和重装系统

Rk3566 add LED

Scientific running robot pancakeswap clip robot latest detailed tutorial

Redis6 transaction and locking mechanism

Interviewer soul torture: why does the code specification require SQL statements not to have too many joins?

Those things I didn't know until I took the postgraduate entrance examination

Win10——轻量级小工具
随机推荐
Log4j utilization correlation
Laravel framework operation error: no application encryption key has been specified
【 script secret pour l'utilisation de MySQL 】 un jeu en ligne sur l'heure et le type de date de MySQL et les fonctions d'exploitation connexes (3)
多人合作项目查看每个人写了多少行代码
4年工作经验,多线程间的5种通信方式都说不出来,你敢信?
Zhubo Huangyu: it's really bad not to understand these gold frying skills
PostgreSQL Usage Summary (PIT)
stm32逆向入门
Redis6 master-slave replication and clustering
PHP character capture notes 2020-09-14
Resttemplate details
Win10——轻量级小工具
FPGA learning notes: vivado 2019.1 add IP MicroBlaze
aspx 简单的用户登录
Summit review | baowanda - an integrated data security protection system driven by compliance and security
leetcode 10. Regular expression matching regular expression matching (difficult)
面试官灵魂拷问:为什么代码规范要求 SQL 语句不要过多的 join?
Summary and arrangement of JPA specifications
华为推送服务内容,阅读笔记
Solve the problem of invalid uni app configuration page and tabbar