不但要返回顶部,还要返回底部,小改变,实用,?scroll?的js代码,支持IE,FF,chrome[摘自布布分享,tech.bubufx.com]
Posted on 2013-04-24 10:21 weekzero 阅读(72) 评论(0) 收藏 举报
返回顶部,top,返回底部,bottom,大于1000的贴近网站内容,纯js,加入网站简单,支持IE,FF,chrome等,好用。
代码放置于页面body标签内,可放于公共页面中,就是在每个页面中都引用的页面中,如布布分享网站,就是放在了bottom用户控件中,这样每个页面就都加上了返回顶部和返回底部。
之前借助了锚点,比如:<a name="bottom" id="bottom"></a>
后来才明白过来,哈哈,其实用scroll就足够了,返回顶部js的scroll(0,0),到达底部用:scroll(0,document.body.scrollHeight),document.body.scrollHeight是正文全文高。
代码如下:
<script type="text/javascript">
var imgurltop = "/img/totop.png";
var imgurlbottom = "/img/tobottom.png";
var w = 45; //bubufx提示:离右侧距离
var h = 110; //bubufx提示:离底部距离
var wW = document.body.clientWidth; //bubufx提示:获得当前页面宽度
if (wW > 1000) //bubufx提示:页面宽度大于1000时贴在内容旁边
w = (wW - 1000) / 2 - 5;
var str = "";
var obj = document.getElementById("divStayTopLeft");
if (obj) str = obj.innerHTML;
if (typeof document.compatMode != ’undefined’ && document.compatMode != ’BackCompat’) {
document.writeln(’<DIV style="z-index:9;right:0;bottom:0;height:’ + h + ’px;width:’ + w + ’px;overflow:hidden;POSITION:fixed;_position:absolute; _margin-top:expression(document.documentElement.clientHeight-this.style.pixelHeight+document.documentElement.scrollTop);">’);
}
else {
document.writeln(’<DIV style="z-index:9;right:0;bottom:0;height:’ + h + ’px;width:’ + w + ’px;overflow:hidden;POSITION:fixed;*position:absolute; *top:expression(eval(document.body.scrollTop)+eval(document.body.clientHeight)-this.style.pixelHeight);">’);
}
document.writeln(’<div style="clear:both;margin:auto;height:120px;font-size:16px;overflow:hidden;font-weight:bold;text-align:left;"><a href="javascript:scroll(0,0)" hidefocus="true"><img src="’ + imgurltop + ’" width="42" height="42" alt="回到布布分享顶部" style="border: 0px;" /></a><div style="height:5px;"></div><a href="javascript:scroll(0,document.body.scrollHeight)" hidefocus="true"><img src="’ + imgurlbottom + ’" width="42" height="42" alt="回到布布分享底部" style="border: 0px;" /></a></div> ’);
document.write(’<div style="clear:both;margin:auto;overflow:hidden;text-align:left;">’ + str + ’</div>’);
document.writeln(’</DIV>’);
</script>
浙公网安备 33010602011771号