单独给网站添加访问网页顶部的代码实现方法

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

第一、通用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\”,我们准备准备一个箭头上的图片。

第二、添加页面脚本

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\');

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

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

本文出处:本站部落 » 单独给网站添加访问网页顶部的代码实现方法 | 欢迎分享( 公众号:本站玩运营 )

推荐站内搜索:cn域名、腾讯云服务器、美国vps主机、免备案虚拟主机、vps虚拟主机、美国虚拟主机购买、域名注册网站查询、注册 域名、韩国高防服务器服务器、