<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
#returnTop {
position: fixed;
right: 50px;
bottom: 100px;
}
</style>
</head>
<body style="height: 5000px;">
<div id="returnTop">返回顶部</div>
<script>
const returnTop = document.querySelector('#returnTop');
// 点击返回顶部
returnTop.onclick = function () {
// 获取当前的距离
let top = window.pageYOffset;
let time = setInterval(function () {
top -= 30;
window.scroll(0, top);
// 当前位置小于0时就当前位置等于0
if (top < 0) {
fn()
}
})
// 调用
function fn() {
window.scroll(0, 0);
clearInterval(time)
}
}
</script>
</body>
</html>
实现网站返回顶部效果
- 27阅读
- 2022年08月05日
- 0评论
梦想家阳阳
半山腰的风景很美,然而,我还是想到山顶去看看!文章 | 评论 | 标签 |
---|---|---|
391 | 5 | 40 |
最新回复
随便看看
版权属于:梦想家阳阳
本文链接:http://liuyuyang.net/index.php/archives/1713/
作品采用
知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可
评论区
暂无评论