当前位置:网站首页>Anchor navigation demo
Anchor navigation demo
2022-07-05 13:50:00 【wade3po】
Anchor navigation should be a common feature , I have shared the simple implementation of mobile terminal navigation long ago , Together, it is easy to achieve mobile anchor navigation , And not with hash Pattern . Of course ,PC The end can also be used directly .
The first effect :
For the convenience of direct use vue grammar , First, give each block a unique ref, Then, when initializing, record the distance between each block and the top and the height of each block , And monitor scrolling :
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
});
});
});
The three most important methods :
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;
}
})
},
// Click the anchor to locate
changeKey(key){
document.removeEventListener('scroll', this.throttledScrollHandler);
this.currentKey = key;
let to = this.$refs[key][0].offsetTop - this.firstOffsetTop;
this.animationScrollTo(document.documentElement, to);
},
// Anchor positioning animation scrolling
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);
}
First judge whether to slide up or down , Divide each sliding distance by 30, As the speed of sliding , Pay attention here , If the sliding distance is 0 In this case, it is necessary to deal with , Otherwise, the scroll bar will get stuck , Before that, it is a variable to judge whether it is greater than 0, As a result, the scroll bar got stuck . Scroll and add some animation effects directly requestAnimationFrame , Generally compatible , If not, use a timer , The timer will be less smooth .
Monitor the scrolling and calculate whether the scrolling distance is greater than the recorded distance , And more than half is the next , Navigate to the next... Above the menu .
complete demo:
https://github.com/wade3po/demoCode
Anchor navigation 1.html Is easier , But compatibility is not necessarily better .
边栏推荐
- Kotlin协程利用CoroutineContext实现网络请求失败后重试逻辑
- Redis6 data type and operation summary
- 不知道这4种缓存模式,敢说懂缓存吗?
- Idea set method annotation and class annotation
- MySQL get time
- laravel-dompdf导出pdf,中文乱码问题解决
- Zibll theme external chain redirection go page beautification tutorial
- Solve the problem of "unable to open source file" xx.h "in the custom header file on vs from the source
- "Baidu Cup" CTF competition in September, web:upload
- Kafaka log collection
猜你喜欢
华为推送服务内容,阅读笔记
Rk3566 add LED
[server data recovery] a case of RAID5 data recovery stored in a brand of server
【华南理工大学】考研初试复试资料分享
zabbix 监控
Elfk deployment
搭建一个仪式感点满的网站,并内网穿透发布到公网 2/2
RK3566添加LED
Catch all asynchronous artifact completable future
Operational research 68 | the latest impact factors in 2022 were officially released. Changes in journals in the field of rapid care
随机推荐
不知道这4种缓存模式,敢说懂缓存吗?
The development of speech recognition app with uni app is simple and fast.
Data Lake (VII): Iceberg concept and review what is a data Lake
[server data recovery] a case of RAID5 data recovery stored in a brand of server
【 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)
那些考研后才知道的事
In addition to the root directory, other routes of laravel + xampp are 404 solutions
When using Tencent cloud for the first time, you can only use webshell connection instead of SSH connection.
Laravel框架运行报错:No application encryption key has been specified
PHP character capture notes 2020-09-14
What happened to the communication industry in the first half of this year?
Require, require in PHP_ once、include、include_ Detailed explanation of the efficiency of repeated introduction of once class library
Redis6 data type and operation summary
French scholars: the explicability of counter attack under optimal transmission theory
Jetpack Compose入门到精通
53. 最大子数组和:给你一个整数数组 nums ,请你找出一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。
嵌入式软件架构设计-消息交互
Record in-depth learning - some bug handling
Personal component - message prompt
面试官灵魂拷问:为什么代码规范要求 SQL 语句不要过多的 join?