当前位置:网站首页>Back to the top of several options (JS)
Back to the top of several options (JS)
2022-07-26 11:31:00 【szjSmiling】
I like the last one best : Transition back to the top ! recommend
Be careful : The outermost parent cannot be set overflow:scroll; Otherwise, it will fail . resolvent : It's on the outside, adding a layer div.
1. Anchor point
<body style="height:2000px;">
<div id="topAnchor"></div>
<a href="#topAnchor" style="position:fixed;right:0;bottom:0"> Back to the top </a>
</body>
2.scrollTop
<body style="height:2000px;">
<button id="test" style="position:fixed;right:0;bottom:0"> Back to the top </button>
<script>
test.onclick = function(){
document.body.scrollTop = document.documentElement.scrollTop = 0;
}
</script>
</body>
3.scrollTo():scrollTo(x,y) Method to scroll through the current window The document shown in , Let the coordinates in the document be x and y designated
The point is located in the upper left corner of the display area , Set up scrollTo(0,0) Can achieve the effect of returning to the top .
<body style="height:2000px;">
<button id="test" style="position:fixed;right:0;bottom:0"> Back to the top </button>
<script>
test.onclick = function(){
scrollTo(0,0);
}
</script>
</body>
4.scrollBy():scrollBy(x,y) Method to scroll through the current window The document shown in ,x and y Specifies the relative amount of scrolling
Just take the scrolling length of the current page as a parameter , Reverse scrolling , You can achieve the effect of returning to the top
<body style="height:2000px;">
<button id="test" style="position:fixed;right:0;bottom:0"> Back to the top </button>
<script>
test.onclick = function(){
var top = document.body.scrollTop || document.documentElement.scrollTop
scrollBy(0,-top);
}
</script>
</body>
5.scrollIntoView():Element.scrollIntoView Method to scroll the current element , Enter the visible area of the browser .
This method can accept a Boolean value as a parameter . If true, Representing the top of the element and the visible part of the current region
Align Top ( The premise is that the current area can be scrolled ); If false, Represents the bottom of the element and the visible part of the current area
Align the tail of ( The premise is that the current area can be scrolled ). If the parameter is not provided , The default is true.
<body style="height:2000px;">
<div id="target"></div>
<button id="test" style="position:fixed;right:0;bottom:0"> Back to the top </button>
<script>
test.onclick = function(){
target.scrollIntoView();
}
</script>
</body>
========================================
increase scrollTop Animation effect of :
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);
}
});
}
increase scrollTo() Animation effect :
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);
}
});
}
increase scrollBy() Animation effect :
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);
}
});
}
边栏推荐
- 正点原子stm32中hal库iic模拟`#define SDA_IN() {GPIOB->MODER&=~(3<<(9*2));GPIOB->MODER|=0<<9*2;}` //PB9 输入模式
- [development tool] ieda red
- ESP8266-Arduino编程实例-开发环境搭建(基于PlatformIO)
- Classic Bluetooth connection process
- An error occurred in the scrapy shell
- MySQL锁机制
- Understanding of Abstract Programming
- ArrayList of novice source code
- Introduction to authoringrealm
- Basic recursive equation of DP
猜你喜欢

由浅入深搭建神经网络

QT——LCDNumber

实时流式协议--RTSP

Reproduce PHP one sentence Trojan horse

PyQt5快速开发与实战 3.1 Qt Designer快速入门

ESP8266-Arduino编程实例-开发环境搭建(基于PlatformIO)

easyui02

记录个人遇到的错误

Leetcode-209. subarray with the smallest length (binary, prefix and, sliding window)

Can SAIC mingjue get out of the haze if its products are unable to sell and decline
随机推荐
数据可视化-《白蛇2:青蛇劫起》(2)
如何配置JdbcRealm数据源呢?
Can SAIC mingjue get out of the haze if its products are unable to sell and decline
新来个技术总监要我做一个 IP 属地功能~
What does it mean that the configuration file ends in RC
ESP8266-Arduino编程实例-GPIO输入和输出
Caused by: scala.MatchError: None (of class scala.None$)
记录个人遇到的错误
[报错]Exception: Found duplicate column(s) in the data schema: `value`;
MySQL死锁分析
A new technical director asked me to do an IP territorial function~
Pyqt5 rapid development and practice 3.1 QT designer quick start
Harbor2.2 用户角色权限速查
easyui02
leetcode-209. 长度最小的子数组(二分、前缀和、滑动窗口)
社区点赞业务缓存设计优化探索
pytest pytest. Ini configuration case grouping case skipping
AuthorizingRealm简介说明
C#笔记
Leetcode-209. subarray with the smallest length (binary, prefix and, sliding window)