我們用 WordPress 來做站都會遇到頁面內容比較長的時候,上下拖拉頁面實在麻煩,這時候就需要一個 WordPress 返回頂部的功能了。網上相關教程也一大堆,又是 js 腳本啊,又是 JQuery 啊什麼的,太麻煩了,而今天分享的是沒有 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. 添加返回頂部按鈕圖片 可以選擇自己喜歡的

top

top02

a

roll

icon

top01

將圖片保存到主題文件夾下的 images 裏,圖片名叫 gotop.gif