今天一个朋友发邮件给我说博客没有返回顶部功能他看着特别不爽,其实我原来是有这个的,用了一个wp-top插件,可能是我后期优化的时候把这个插件给关闭了,这个朋友说的也对,没这个返回顶部也不爽,现在给大家分享下一个简单的解决办法,只用改两个文件,不调用JS。
1. 在style.css中添加如下代码:
#gotop{ width:38px; height:36px; position:fixed; bottom:25px; right:10px; top:auto; display:block; cursor:pointer; background: url(images/gotop.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)));}
其中,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. 添加返回顶部按钮图片 可以选择自己喜欢的
自己去网上找,有很多,或者扒走我的http://aywps.cn/top.png(后缀改成.gif)上传到你的主题images文件夹里面