html:
<span>^</span>
css
body{
height: 3000px;
}
span{
display: block;
position: fixed;
bottom: 20px;
right: 10px;
font-size: 60px;
}
js:
<script>
var ospan=document.getElementsByTagName('span')[0];
var sp=0;
function scrollup(){
if(sp>0){
sp-=100;
window.scrollTo(0,sp);
setTimeout(scrollup,20);//用setTimeout模式setInterval的效果
}else{
window.scrollTo(0,0);
}
}
ospan.onclick=function(){
sp=document.documentElement.scrollTop || window.pageYOffset || document.body.scrollTop;//兼容浏览器,获得到顶部的距离
setTimeout(scrollup,20);
}
</script>
jquery:
<script>
$('.back-to-top').click(function(){
$('html, body').animate({scrollTop:0},500);
})
</script>