当前位置:网站首页>回到顶部的几种方案(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);
}
});
}
边栏推荐
- Dry goods likeshop takeout order system is open source, 100% open source, no encryption
- .NET操作Redis String字符串
- 同步方法中不使用asyncTask<T> 修饰和await获取异步返回值(同步方法中调用异步方法)
- 粽子大战 —— 猜猜谁能赢
- 扫雷pro版2021-08-19
- centos8(liunx)部署WTM(ASP.NET 5)使用pgsql
- el-table实现可编辑表格
- [leetcode daily question 2021/5/8]1723. The shortest time to complete all work
- algorithm
- 13 以对象管理资源
猜你喜欢
反射机制简述
第8期:云原生—— 大学生职场小白该如何学
[leetcode daily question 2021/4/23]368. Maximum divisible subset
解决:无法加载文件 C:\Users\user\AppData\Roaming\npm\npx.ps1,因为在此系统上禁止运行脚本 。
【论文下饭】Deep Mining External Imperfect Data for ChestX-ray Disease Screening
第4期:大学生提前职业技能准备之一
[leetcode daily question 2021/8/30]528. Choose randomly by weight [medium]
Write to esp8266 burning brush firmware
在神州IV开发板上为STemWin 5.22加入触屏驱动
.NET 开源框架在工业生产中的应用
随机推荐
异常的概念与处理
RT-Thread 学习笔记(七)---开启基于SPI Flash的elmfat文件系统(中)
剑指Offer(五十二):正则化表达式
[leetcode daily question 2021/2/13]448. Find all the missing numbers in the array
Constructors, method overloads, object arrays, and static
在altium designer中禁用USBJATG
【论文下饭】Deep Mining External Imperfect Data for ChestX-ray Disease Screening
.net operation redis sorted set ordered set
.NET操作Redis Set无序集合
父类对子类的引用(父类引用指向子类对象)
使用flex实现左中右布局,中间自适应
使用定位实现左中右布局,中间内容自适应
[leetcode daily question 2021/2/14]765. Lovers hold hands
Application of.Net open source framework in industrial production
Tradingview tutorial
2021-08-12函数递归_和鹏哥学习C语言
[leetcode每日一题2021/2/18]【详解】995. K 连续位的最小翻转次数
datav漂亮数据屏制作体验
使用grid实现左中右布局,中间内容自适应
Issue 6: which mainstream programming language should college students choose