当前位置:网站首页>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 .
边栏推荐
- The "Baidu Cup" CTF competition was held in February 2017, Web: explosion-2
- Redis6 data type and operation summary
- Solve the problem of "unable to open source file" xx.h "in the custom header file on vs from the source
- Can graduate students not learn English? As long as the score of postgraduate entrance examination English or CET-6 is high!
- 2022司钻(钻井)考试题库及模拟考试
- 4年工作经验,多线程间的5种通信方式都说不出来,你敢信?
- 什么叫做信息安全?包含哪些内容?与网络安全有什么区别?
- [machine learning notes] several methods of splitting data into training sets and test sets
- How to divide a large 'tar' archive file into multiple files of a specific size
- ::ffff:192.168.31.101 是一个什么地址?
猜你喜欢
Rk3566 add LED
Idea set method annotation and class annotation
uplad_ Labs first three levels
"Baidu Cup" CTF competition in September, web:upload
NFT value and white paper acquisition
ELFK部署
Redis6 master-slave replication and clustering
Introduction to Chapter 8 proof problem of njupt "Xin'an numeral base"
龙芯派2代烧写PMON和重装系统
Nantong online communication group
随机推荐
荐号 | 有趣的人都在看什么?
asp.net 读取txt文件
In addition to the root directory, other routes of laravel + xampp are 404 solutions
Idea remote debugging agent
Wechat app payment callback processing method PHP logging method, notes. 2020/5/26
Record in-depth learning - some bug handling
What is information security? What is included? What is the difference with network security?
搭建一个仪式感点满的网站,并内网穿透发布到公网 2/2
Etcd database source code analysis -- rawnode simple package
链表(简单)
Elfk deployment
[machine learning notes] how to solve over fitting and under fitting
Assembly language - Beginner's introduction
Redis6 data type and operation summary
What are the private addresses
Idea设置方法注释和类注释
With 4 years of working experience, you can't tell five ways of communication between multithreads. Dare you believe it?
RK3566添加LED
真正的缓存之王,Google Guava 只是弟弟
What happened to the communication industry in the first half of this year?