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
最代码最近下载分享源代码列表最近下载
kkname12345  LV11 2016年6月9日
zyl  LV34 2016年1月7日
twingo0204  LV24 2016年1月7日
bxs1259  LV16 2015年2月8日
最代码官方  LV167 2015年1月28日
最代码最近浏览分享源代码列表最近浏览
zz-xxzly  LV2 2020年12月21日
gaoy103 2020年11月24日
暂无贡献等级
jpy8500  LV6 2020年5月23日
少年阿宾  LV3 2019年9月18日
李杰强  LV18 2019年4月11日
ahviplc  LV5 2018年11月13日
稀饭囡  LV9 2018年10月22日
lw19900921  LV25 2018年7月5日
bianyuning  LV14 2018年5月2日
sorace  LV3 2017年12月4日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友