源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

js控制的回到页面顶端goTop的代码实现

  • 时间:2021-02-08 13:24 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:js控制的回到页面顶端goTop的代码实现
有没有见过很多在页面的右下角有个【回到顶端】的悬浮东东,并且在开始时没有,一移动滚动条就出现,回到了顶端又消失的样子。 像: [img]http://files.jb51.net/file_images/article/201303/2013320152203720.jpg?2013220152326[/img] 控制的js代码如下:
[u]复制代码[/u] 代码如下:
function goTop(){ var _btn = document.getElementById("goTop"); if (document.documentElement && document.documentElement.scrollTop) { var _con = document.documentElement; } else if (document.body) { var _con = document.body; } window.onscroll = set; _btn.style.left = document.documentElement.scrollWidth/2 + 500 + "px"; _btn.onclick = function (){ _btn.style.display = "none"; window.onscroll = null; this.timer = setInterval(function() { _con.scrollTop -= Math.ceil(_con.scrollTop * 0.3); if (_con.scrollTop == 0) clearInterval(_btn.timer, window.onscroll = set); },10); }; function set() { _btn.style.display = _con.scrollTop ? 'block': "none"; } }; document.write("<div id=\"goTop\" style=\"font-size:12px; width:20px; height:55px; background:#0950A2; color:#fff; line-height:14px; position:fixed; bottom:30px; display:none; cursor:pointer; text-align:center; padding:5px 0; z-index:1; _position:absolute; _top:expression(eval(document.compatMode && document.compatMode == \'CSS1Compat\') ? documentElement.scrollTop + (documentElement.clientHeight - this.clientHeight) - 30 : document.body.scrollTop + (document.body.clientHeight - this.clientHeight) - 30);\">返<br />回<br />顶<br />部</div>"); window.onscroll = goTop;
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部