它返回一个对象,其中包含了left、right、top、bottom四个属性,分别对应了该元素的左上角和右下角相对于浏览器窗口(viewport)左上角的距离。 所以,网页元素的相对位置就是 var X= this.getBoundingClientRect().left; var Y =this.getBoundingClientRect().top; 再加上滚动距离,就可以得到绝对位置 var X= this.getBoundingClientRect().left+document.documentElement.scrollLeft; var Y =this.getBoundingClientRect().top+document.documentElement.scrollTop; 目前,IE、Firefox 3.0+、Opera 9.5+都支持该方法,而Firefox 2.x、Safari、Chrome、Konqueror不支持。
推荐阅读
extJs 文本框后面加上说明文字+下拉列表选中值后触发事件
复制代码 代码如下:var showForm; function panelShowForm() { showForm=null; showForm = new Ext.FormPanel({ renderTo:"showPanel", region:'north', border:false, bodyBorder:false, frame:true, waitMsgTarg>>>详细阅读
本文标题:javascript 获取元素位置的快速方法 getBoundingClientRect()
地址:http://www.17bianji.com/kaifa2/JS/27621.html
1/2 1