第一、通用CSS文件
#gotop{
width:38px;
height:36px;
position:fixed;
bottom:25px;
right:10px;
top:auto;
display:block;
cursor:pointer;
background: url(images/topav.jpg) no-repeat
}
*html #gotop{
position:absolute;
bottom:auto; top:expression(eval(document.documentElement.scrollTop+document.documentElement.clientHeight-this.offsetHeight-(parseInt(this.currentStyle.marginTop,10)||0)-(parseInt(this.currentStyle.marginBottom,10)||0)));
}通用css文件,我们可以修改一下对应的图片"
images/topav.jpg",我们准备准备一个箭头上的图片。第二、添加页面脚本
<div style="display: none;" id="gotop"></div>
<script type='text/javascript'>
backTop=function (btnId){
var btn=document.getElementById(btnId);
var d=document.documentElement;
var b=document.body;
window.onscroll=set;
btn.onclick=function (){
btn.style.display="none";
window.onscroll=null;
this.timer=setInterval(function(){
d.scrollTop-=Math.ceil((d.scrollTop+b.scrollTop)*0.1);
b.scrollTop-=Math.ceil((d.scrollTop+b.scrollTop)*0.1);
if((d.scrollTop+b.scrollTop)==0) clearInterval(btn.timer,window.onscroll=set);
},10);
};
function set(){btn.style.display=(d.scrollTop+b.scrollTop>100)?'block':"none"}
};
backTop('gotop');
</script>这里,在我们的网站底部页面模板中添加代码,一般在footer.php模板中。
我们再实例中测试看看效果。