点击按钮回到顶部(H5+JS)

编程入门 行业动态 更新时间:2024-10-19 11:40:16

点击<a href=https://www.elefans.com/category/jswz/34/1771318.html style=按钮回到顶部(H5+JS)"/>

点击按钮回到顶部(H5+JS)

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>回到顶部</title>
</head>
<style>/*鼠标滑动20px后显示定值按钮*/#btnTop {display: none;position: fixed;bottom: 20px;right: 30px;z-index: 99;border: none;outline: none;background-color: #89cff0;color: white;cursor: pointer;padding: 15px;border-radius: 10px;}#btnTop:hover {background-color: #1E90FF;}.a {margin-top: 100px;background-color: wheat;width: 1000px;height: 200px;}.b {background-color: silver;width: 1000px;height: 600px;}.c {background-color: firebrick;width: 1000px;height: 600px;}div {margin: 5px auto;}img {margin: 40px 100px;}/*鼠标悬停放大延时秒*/div img {cursor: pointer;transition: all 0.7s;}/*鼠标悬停放大倍数*/div img:hover {transform: scale(1.5);}
</style>
<body><div class="a"><img src="../PublicImg/Number/2.jpg" height="150"/>
</div>
<div class="b"><img src="../PublicImg/Number/3.jpg" height="313"/>
</div>
<div class="c"><img src="../PublicImg/Number/4.jpg" height="313"/>
</div>
<button onclick="returnTop()" id="btnTop" title="返回顶部">返回顶部</button>
<script>//回到顶部滑动效果window.onscroll = function () {scrollFunction()};var timer = null;btnTop.onclick = function () {cancelAnimationFrame(timer);timer = requestAnimationFrame(function fn() {var oTop = document.body.scrollTop || document.documentElement.scrollTop;if (oTop > 0) {document.body.scrollTop = document.documentElement.scrollTop = oTop - 50;timer = requestAnimationFrame(fn);} else {cancelAnimationFrame(timer);}});}// 当网页向下滑动 20px 出现"返回顶部" 按钮window.onscroll = function () {scrollFunction()};function scrollFunction() {console.log(121);if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) {document.getElementById("btnTop").style.display = "block";} else {document.getElementById("btnTop").style.display = "none";}}// 点击按钮,返回顶部function returnTop() {document.body.scrollTop = 0;document.documentElement.scrollTop = 0;}</script>
</body>
</html>

更多推荐

点击按钮回到顶部(H5+JS)

本文发布于:2023-07-28 21:54:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1330313.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:按钮   JS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!