//以下代码放到一个adapt.js文件当中 (function (doc, win) { var docEl = doc.documentElement, resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize', recalc = function () { var clientWidth = docEl.clientWidth; if (!clientWidth) return; docEl.style.fontSize = 20 * (clientWidth /750) + 'px'; }; if (!doc.addEventListener) return; win.addEventListener(resizeEvt, recalc, false); doc.addEventListener('DOMContentLoaded', recalc, false); })(document, window); function goTopEx() { var obj = document.getElementById("goTopBtn"); function getScrollTop() { return document.documentElement.scrollTop + document.body.scrollTop; } function setScrollTop(value) { if (document.documentElement.scrollTop) { document.documentElement.scrollTop = value; } else { document.body.scrollTop = value; } } window.onscroll = function() { getScrollTop() > 0 ? obj.style.display = "": obj.style.display = "none"; } obj.onclick = function() { var goTop = setInterval(scrollMove, 10); function scrollMove() { setScrollTop(getScrollTop() / 1.1); if (getScrollTop() < 1) clearInterval(goTop); } } }
html文档代码:
<script src="js/adapt.js"></script>
<-- 以下是超过高度显示回到顶部代码 -->
<div id="goTopBtn"><img src="img/index_03.png"/></div>
<script type=text/javascript>goTopEx();</script>
转载于:https://blog.51cto.com/xuyran/1789210