huangwei的gravatar头像
huangwei 2015-01-28 15:05:50

javascript跨浏览器获取滚动条的坐标位置

javascript如何获取浏览器滚动条的位置呢?

// Cross browser gets the position of scroll
com.whuang.hsj.getScroll=function(){
    return {
        top:document.documentElement.scrollTop || document.body.scrollTop,
        left:document.documentElement.scrollLeft || document.body.scrollLeft,
        height:document.documentElement.scrollHeight ||document.body.scrollHeight
    }

}

如下图:

javascript跨浏览器获取滚动条的坐标位置

应用:

弹框时需要获取竖直滚动条的位置,使弹出框的高度正好在可视位置,例如:

javascript跨浏览器获取滚动条的坐标位置

项目中使用的代码:

 var ajaxDetail=function(id_product){
		 	// $("#subPagePanel").css("background-image", "url(\"<%=path%>/static/images/loading/progress.gif\")");
		 	showLoadPanel("<%=path%>/static/images/loading/progress.gif");
		 	$('#subPagePanel').css("top", com.whuang.hsj.getScroll().top+"px");//弹出panel兼容滚动条
		 	$('#subPagePanel').show('normal');
		 	ajaxHtml("<%=path%>/${label }/detail?id="+id_product+"&fsdf=${currentTime}&targetView=product/detail_ajax",$('#content'));
		 	
		 }

 

代码common_util.js见附件


打赏

文件名:common_util.zip,文件大小:117.046K 下载
  • /
    • /Ajax.js
    • /common_util.js
    • /index.js
    • /jquery-1.11.1.js
    • /jquery.form.js
    • /md5.js
    • /mobile_index.js
    • /page.js
    • /test.html
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友