当前位置:网站首页>点击回到顶部js
点击回到顶部js
2022-07-29 01:16:00 【kilito_01】
gotopping() {
clearInterval(this.timers);
this.timers = setInterval(() => {
let curHeight = document.documentElement.scrollTop || document.body.scrollTop;
var now = curHeight;
// 速度 = 0 - 滚动的高度 除 7 为负数
var speed = (0 - now) / 7;
// 速度值是否大于0 大于0 向上取整 小于0 向下取整
speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);
// 在这里判断最后滚动到顶部的值是否为0 为0 说明已经到了顶部不需要再向上滚动 清空定时器 将滚动按钮隐藏
if (curHeight == 0) {
clearInterval(this.timers);
this.toTop = false;
}
// 向上滚动滚动条 即使值为负数 最后取得值也是 0
document.documentElement.scrollTop = curHeight + speed;
document.body.scrollTop = curHeight + speed;
// 是否停止滚动 为否
this.stop = false;
}, 30);
},
// 向上滚动时监听函数
needToTop() {
// 兼容ie和chrome的写法 ie只有body属性 chrome只有docmengElement属性
let curHeight = document.documentElement.scrollTop || document.body.scrollTop;
// 判断是否有下拉滚动条 有的话显示回到顶部按钮 否则隐藏
if (curHeight == 0) {
this.toTop = false;
} else {
this.toTop = true;
}
// 如果有下拉的行为 就清空定时器
if (this.stop) {
clearInterval(this.timers);
}
this.stop = true;
},
边栏推荐
- Stonedb invites you to participate in the open source community monthly meeting!
- leetcode/0和1个数相同的连续子数组
- Top network security prediction: nearly one-third of countries will regulate blackmail software response within three years
- Planning mathematics final simulation exam I
- Comprehensive explanation of "search engine crawl"
- 数学建模——永冻土层上关于路基热传导问题
- Yocto project download and compilation
- 九天后我们一起,聚焦音视频、探秘技术新发展
- Golang run times undefined error [resolved]
- Super scientific and technological data leakage prevention system, control illegal Internet behaviors, and ensure enterprise information security
猜你喜欢

(arxiv-2018) 重新审视基于视频的 Person ReID 的时间建模

【流放之路-第八章】

Where will Jinan win in hosting the first computing power conference?

把逻辑做在Sigma-DSP中的优化实例-数据分配器

StoneDB 为何敢称业界唯一开源的 MySQL 原生 HTAP 数据库?

Mathematical modeling -- red wine quality classification

LeetCode 112:路径总和

Mobile communication -- simulation model of error control system based on convolutional code

Mathematical modeling - location of police stations

九天后我们一起,聚焦音视频、探秘技术新发展
随机推荐
Thirty years of MPEG audio coding
[the road of Exile - Chapter 5]
【流放之路-第三章】
Talk about possible problems when using transactions (@transactional)
[the road of Exile - Chapter III]
数学建模——自来水管道铺设问题
Network security litigation risk: four issues that chief information security officers are most concerned about
为什么 BI 软件都搞不定关联分析
druid. The performance of IO + tranquility real-time tasks is summarized with the help of 2020 double 11
Comprehensive explanation of "search engine crawl"
Come on, handwritten RPC S2 serialization exploration
TDA75610-I2C-模拟功放I2C地址的确定
Force deduction brush question (1): sum of two numbers
Mathematical modeling - location of police stations
九天后我们一起,聚焦音视频、探秘技术新发展
MPEG音频编码三十年
[10:00 public class]: application exploration of Kwai gpu/fpga/asic heterogeneous platform
Mathematical modeling -- red wine quality classification
How to protect WordPress website from network attack? It is essential to take safety measures
Leetcode 113: path sum II