继续分享wordpress建站教程。如果你的网站内容比较长,可以考虑给网站添加一个返回顶部的按钮,这样当用户浏览到页面底部又想返回顶部时,就可以一键返回了,这也算是提升用户体验的之法之一吧。现在有很多主题其实都集成了这个功能,如果没有则可以参考使用下面的代码来添加。
我们先使用下面的CSS代码设置好返回顶部按钮的样式,其中的【images/topav.jpg】可以自己设置为你自己的图标样式。
#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))); }
接下来把下面的代码添加到网站的footer页面就可以了。
<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>
© Copyright 2020~2026. 悦然网络工作室/悦然wordpress建站 专注中小企业wordpress建站 All Rights Reserved.网站地图本站图片来源为Pexels、Pixabay、Freepik、Unsplash等图片库的免费许可,CC0协议;还有部分为自己手绘或AI生成,版权碰瓷请自重!法律服务:law@yueranseo.com蜀ICP备20016391号-1 川公网安备 51011502000367号