一般我们遇到比较长页面的网站,会在右侧方加上访问顶部的小浮动按钮,我们可以点击后直接回到网页的首屏中,于是这类问题经常出现的时候老蒋会收藏整理一个通用的代码脚本,在需要用到的时候直接贴进来使用。这里把我整理过来的方法一并记录到网站中,有需要的朋友可以选择。同时也方便我以后的再次复制。

第一、通用CSS文件

#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)));
}

通用css文件,我们可以修改一下对应的图片"images/topav.jpg",我们准备准备一个箭头上的图片。

第二、添加页面脚本

<div 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>

这里,在我们的网站底部页面模板中添加代码,一般在footer.php模板中。

我们再实例中测试看看效果。

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。