Js返回顶部的方法
1 if (window.addEventListener) { 2 window.addEventListener('load', goTopEx); 3 } else if (window.attachEvent) { 4 window.attachEvent('onload', goTopEx); 5 } else { 6 window.onload = goTopEx; 7 } 8 function goTopEx() { 9 var obj = document.getElementById("goTopBtn"); 10 getScrollTop() > 0 ? obj.style.display = "": obj.style.display = "none"; 11 function getScrollTop() { 12 return document.documentElement.scrollTop + document.body.scrollTop; 13 } 14 function setScrollTop(value) { 15 if (document.documentElement.scrollTop) { 16 document.documentElement.scrollTop = value; 17 } else { 18 document.body.scrollTop = value; 19 } 20 } 21 window.onscroll = function() { 22 getScrollTop() > 0 ? obj.style.display = "": obj.style.display = "none"; 23 } 24 obj.onclick = function() { 25 var goTop = setInterval(scrollMove, 10); 26 function scrollMove() { 27 setScrollTop(getScrollTop() / 1.1); 28 if (getScrollTop() < 1) clearInterval(goTop); 29 } 30 } 31 }
返回顶部的代码