当前位置:网站首页>回到顶部的几种方案(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);
}
});
}
边栏推荐
- Introduction to Phoenix (Level 1: Phoenix installation, level 2: Phoenix basic grammar)
- MySQL速学-2021-09-01
- C语言鹏哥20210812C语言函数
- [leetcode daily question 2021/8/31] 1109. Flight reservation statistics [medium] differential array
- [leetcode daily question 2021/4/23]368. Maximum divisible subset
- 在altium designer中禁用USBJATG
- What if MySQL can't get in
- .NET5WTM(ASP.NET Core) PGSql开箱操作
- 10 令 operator= 返回一个 reference to *this
- 从蚂蚁的觅食过程看团队研发(转载)
猜你喜欢

mysql20210906

【机器学习小记】【风格迁移】deeplearning.ai course4 4th week programming(tensorflow2)

Centos8 (liunx) deploying WTM (asp.net 5) using PgSQL
![[machine learning notes] [face recognition] deeplearning ai course4 4th week programming](/img/7e/9c0e88097b90c0c24ebf86f090805b.png)
[machine learning notes] [face recognition] deeplearning ai course4 4th week programming
![[leetcode每日一题2021/5/8]1723. 完成所有工作的最短时间](/img/e7/a48bb5b8a86cbc4cd5b37bb16661a8.png)
[leetcode每日一题2021/5/8]1723. 完成所有工作的最短时间

【论文下饭】Deep Mining External Imperfect Data for ChestX-ray Disease Screening

【机器学习小记】【搭建循环神经网络及其应用】deeplearning.ai course5 1st week programming(keras)

Issue 8: cloud native -- how should college students learn in the workplace
![[leetcode每日一题2021/8/31]1109. 航班预订统计【中等】差分数组](/img/9d/5ce5d4144a9edc3891147290e360d8.png)
[leetcode每日一题2021/8/31]1109. 航班预订统计【中等】差分数组
![[leetcode daily question 2021/4/23]368. Maximum divisible subset](/img/0b/32ca862963c842a93f79eaac94fb98.png)
[leetcode daily question 2021/4/23]368. Maximum divisible subset
随机推荐
异常的概念与处理
鹏哥C语言第四课(3)
11 handle "self assignment" in operator=
剑指Offer(九):变态跳台阶
鹏哥C语言——扫雷2021-08-16
Centos8 (liunx) deploying WTM (asp.net 5) using PgSQL
20210807#1 C语言程序结构
Navicat15 MySQL (centos7) connected to local virtual machine
文案秘籍七步曲至----文献团队协作管理
Phase 4: one of College Students' vocational skills preparation in advance
[leetcode daily question 2021/4/23]368. Maximum divisible subset
抽象工厂及其改进示例
剑指Offer(二十):包含min函数的栈
剑指Offer(五十三):表示数值的字符串
关于硕博士开题报告编写的思考
router.push(),router.repalce(),router.go()使用
C语言鹏哥20210812C语言函数
RT-Thread 学习笔记(五)---编辑、下载、调试程序
13 以对象管理资源
Issue 7: how do you choose between curling up and lying flat