首页 > 代码库 > 原生态js回顶部
原生态js回顶部
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" /> <title>Document</title> <style type="text/css"> * { margin: 0; padding: 0; } .public_gotop { background-color: #858585; position: fixed; right: 2rem; bottom: 6rem; width: 3rem; height: 3rem; border-radius: 50%; background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAABN0lEQVRYhe3Uuy4FURjFcZEjiMu76FwLiWuLhJJ4FU+gFwqNa1SuHQmtzhNoJRxB3H4Kc+LLGJdzDI1Z3cxe3/qvvfdk6uoKFSpUqEahFetYRtNfw9tx4k2HaPkreBuOvdcBmn8b3pyAKrrCTXjeQsNvwVtxFGAX6EAPyuH9bu7fRMbOL9EZ1ntTJbbRmBe8CfupY+/K8A2krmPzx9eRwPdS8O5P/IO4Df4NlGqFN2InhJXR+425oVSJtapLJPDtEHKNvirmR3AX5le+XQINydFVdIOBqnbwmjOaKrGE+q+GSl5/rxXdYqhaeMgbw33IW/ywRAJfTcGHa4WH3HE8hNyFzBKYD6YHTPwUHrKn8RTy57JM58niIybzgof8WTwnjLMsQz9OMZU3PDBmEkbn1+5ChQr9F70Ae6lHrD/JbEkAAAAASUVORK5CYII=); background-repeat: no-repeat; background-position: center; background-size: 60%; display: none; } </style> </head> <body> <div style="height: 500px; background: #E1A0F3"></div> <div style="height: 500px; background: #CE77B5"></div> <div class="public_gotop" id="gotop"></div> <script type="text/javascript"> window.onload = function() { // 返回顶部 var goTopEx = function() { var gtObj = document.getElementById("gotop"); function getScrollTop() { return document.documentElement.scrollTop || document.body.scrollTop; } function setScrollTop(value) { document.documentElement.scrollTop = value; document.body.scrollTop = value; } window.onscroll = function() { getScrollTop() > 80 ? gtObj.style.display = "block" : gtObj.style.display = "none"; } gtObj.onclick = function() { var goTop = setInterval(scrollMove, 10); function scrollMove() { setScrollTop(getScrollTop() / 1.1); if (getScrollTop() < 1) clearInterval(goTop); } } }() } </script> </body> </html>
效果如图:
原生态js回顶部
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。