当前位置:网站首页>回到顶部的几种方案(js)
回到顶部的几种方案(js)
2022-07-26 10:39:00 【szjSmiling】
我最喜欢用最后一种:过渡回到顶部!推荐
注意: 最外层父级不可以设置overflow:scroll;否则失效.解决方法:是在外面在加一层div.
1.锚点
<body style="height:2000px;">
<div id="topAnchor"></div>
<a href="#topAnchor" style="position:fixed;right:0;bottom:0">回到顶部</a>
</body>
2.scrollTop
<body style="height:2000px;">
<button id="test" style="position:fixed;right:0;bottom:0">回到顶部</button>
<script>
test.onclick = function(){
document.body.scrollTop = document.documentElement.scrollTop = 0;
}
</script>
</body>
3.scrollTo():scrollTo(x,y)方法滚动当前window中显示的文档,让文档中由坐标x和y指定的
点位于显示区域的左上角,设置scrollTo(0,0)可以实现回到顶部的效果.
<body style="height:2000px;">
<button id="test" style="position:fixed;right:0;bottom:0">回到顶部</button>
<script>
test.onclick = function(){
scrollTo(0,0);
}
</script>
</body>
4.scrollBy():scrollBy(x,y)方法滚动当前window中显示的文档,x和y指定滚动的相对量
只要把当前页面的滚动长度作为参数,逆向滚动,则可以实现回到顶部的效果
<body style="height:2000px;">
<button id="test" style="position:fixed;right:0;bottom:0">回到顶部</button>
<script>
test.onclick = function(){
var top = document.body.scrollTop || document.documentElement.scrollTop
scrollBy(0,-top);
}
</script>
</body>
5.scrollIntoView():Element.scrollIntoView方法滚动当前元素,进入浏览器的可见区域.
该方法可以接受一个布尔值作为参数。如果为true,表示元素的顶部与当前区域的可见部分的
顶部对齐(前提是当前区域可滚动);如果为false,表示元素的底部与当前区域的可见部分
的尾部对齐(前提是当前区域可滚动)。如果没有提供该参数,默认为true.
<body style="height:2000px;">
<div id="target"></div>
<button id="test" style="position:fixed;right:0;bottom:0">回到顶部</button>
<script>
test.onclick = function(){
target.scrollIntoView();
}
</script>
</body>
========================================
增加scrollTop的动画效果:
returnTop(){
let that = this;
cancelAnimationFrame(this.timer);
this.timer = requestAnimationFrame(function fn(){
let oTop = document.body.scrollTop || document.documentElement.scrollTop;
if(oTop > 0){
document.body.scrollTop = document.documentElement.scrollTop = oTop - 50;
that.timer = requestAnimationFrame(fn);
}else{
cancelAnimationFrame(that.timer);
}
});
}
增加scrollTo()动画效果:
var timer = null;
box.onclick = function(){
cancelAnimationFrame(timer);
timer = requestAnimationFrame(function fn(){
var oTop = document.body.scrollTop || document.documentElement.scrollTop;
if(oTop > 0){
scrollTo(0,oTop-50);
timer = requestAnimationFrame(fn);
}else{
cancelAnimationFrame(timer);
}
});
}
增加scrollBy()动画效果:
var timer = null;
box.onclick = function(){
cancelAnimationFrame(timer);
timer = requestAnimationFrame(function fn(){
var oTop = document.body.scrollTop || document.documentElement.scrollTop;
if(oTop > 0){
scrollBy(0,-50);
timer = requestAnimationFrame(fn);
}else{
cancelAnimationFrame(timer);
}
});
}
边栏推荐
- 【小程序】onReachBottom 事件为什么不能触发 ?(一秒搞定)
- Redis implementation of distributed lock solution
- [leetcode每日一题2021/5/8]1723. 完成所有工作的最短时间
- 异常的概念与处理
- 英语基础句型结构------起源
- kali 查看ip地址
- MD5 encryption
- [leetcode每日一题2021/4/23]368. 最大整除子集
- [leetcode daily question 2021/2/18] [detailed explanation] minimum number of turns of 995. K continuous bits
- 11 在 operator= 中处理“自我赋值”
猜你喜欢

第5期:大学生入职必备技能之二

putty的使用教程
![[leetcode每日一题2021/2/13]448. 找到所有数组中消失的数字](/img/9b/624416fa6a408bf64ca5438273176b.png)
[leetcode每日一题2021/2/13]448. 找到所有数组中消失的数字

kali 查看ip地址

英语基础句型结构------起源
![[paper after dinner] deep mining external perfect data for chestx ray disease screening](/img/d6/41c75d292c26b2e7e116767a51eb5e.png)
[paper after dinner] deep mining external perfect data for chestx ray disease screening

在神州IV开发板上成功移植STemWin V5.22

Issue 8: cloud native -- how should college students learn in the workplace

Oracle cannot start tnslistener service cannot start

IAR sprintf 浮点 在UCOS 总格式化成0.0的问题
随机推荐
L2-005 set similarity (intersection of vector and set)
MySQL速学笔记-2021-08-31
[转]ArcGIS中判断两个Geometry之间的关系
Common classes (understand)
IAR sprintf 浮点 在UCOS 总格式化成0.0的问题
同步方法中不使用asyncTask<T> 修饰和await获取异步返回值(同步方法中调用异步方法)
剑指Offer(十):矩形覆盖
RT-Thread 学习笔记(七)---开启基于SPI Flash的elmfat文件系统(中)
algorithm
A semicolon is missing
使用定位实现左中右布局,中间内容自适应
从蚂蚁的觅食过程看团队研发(转载)
Constructors, method overloads, object arrays, and static
Redis implementation of distributed lock solution
[leetcode每日一题2021/2/18]【详解】995. K 连续位的最小翻转次数
13 以对象管理资源
Redis特殊数据类型使用场景
Application of.Net open source framework in industrial production
Write to esp8266 burning brush firmware
剑指Offer(九):变态跳台阶