网站怎么添加代码(单独给网站添加访问网页顶部的代码实现方法)
导读:一般我们遇到比较长页面的网站,会在右侧方加上访问顶部的小浮动按钮,我们可以点击后直接回到网页的首屏中,于是这类问题经常出现的时候老蒋会收藏整理一个通用的代码脚本,在需要用到的时候直接贴进来使用。这里把我整理过来的方法一并记录到网站中,有需要的朋友可以选择。同时也方便我以后的再次复制。...
一般我们遇到比较长页面的网站 ,会在右侧方加上访问顶部的小浮动按钮 ,我们可以点击后直接回到网页的首屏中 ,于是这类问题经常出现的时候老蒋会收藏整理一个通用的代码脚本 ,在需要用到的时候直接贴进来使用 。这里把我整理过来的方法一并记录到网站中 ,有需要的朋友可以选择 。同时也方便我以后的再次复制 。
第一 、通用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模板中 。
我们再实例中测试看看效果 。
创心域SEO版权声明:以上内容作者已申请原创保护,未经允许不得转载,侵权必究!授权事宜、对本内容有异议或投诉,敬请联系网站管理员,我们将尽快回复您,谢谢合作!