也想出现在这里?联系我们

分享一款我常用到的wordpress返回顶部的功能

2013.06.05 wordpress教程
  • 文章介绍
  • 升级版本
  • 评价&建议

我们用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属性可以修改返回顶部的图片。

  1. 在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 -->
  1. 添加返回顶部按钮图片 可以选择自己喜欢的

toptop02arollicontop01

将图片保存到主题文件夹下的images里,图片名叫gotop.gif

有用4
  • 2013.06.05初次和大家见面了!

等待您对该主题的建议

发表评论

还能输入240个字

Hi, 欢迎加入Wordpress技术交流群,带你装逼带你飞!

我要入群
也想出现在这里?联系我们
wordpress加速

我来推荐一个更牛逼的给你看看?

  • 猛戳我吧