对于页面内容比较长的网站,博主觉得返回顶部的功能是很需要的。
给大家分享一个教程吧
1. 在style.css中添加如下代码
#gotop{ width:50px; height:50px; position:fixed; bottom:25px; right:10px; top:auto; display:block; cursor:pointer; background: url(images/top.gif) 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))); }
其中,width和height根据自己图标像素大小设置,bottom和right属性可以控制按钮图片在右下角的位置,background属性可以修改返回顶部的图片。
2. 在footer.php中添加如下代码
<!-- 返回顶部 --> <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> <!-- 返回顶部END -->
3. 添加返回顶部按钮图片
如第一步中的background属性,把top.gif放到主题目录中存放图片的目录下。
谢谢了 ,这个不错啊学习受用了啊