<script type="text/javascript"> $(function(){ showScroll(); function showScroll(){ $(window).scroll( function() { var scrollValue=$(window).scrollTop(); scrollValue > 100 ? $('div[class=scroll]').fadeIn():$('div[class=scroll]').fadeOut(); } ); $('#gotop').click(function(){ $("html,body").animate({scrollTop:0},200); }); } }); </script>
原生js实现:
兼容火狐和webkit内核浏览器:document.documentElement.scrollTop||document.body.scrollTop;
<script type="text/javascript"> function goTopEx(){ var obj=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()>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); } } } goTopEx(); </script>