深圳网站托管宁波seo教程行业推广
jquery获取元素的坐标
jQuery函数获取元素的坐标。
jQuery方法
.position()方法允许我们检索元素相对于偏移父级的当前位置
var pos = $('#wrapper').position();
console.dir(pos);
//output: left: 0, top: 20
.offset(),它检索相对于文档的当前位置。
var offset = $('#wrapper').offset();
console.dir(offset);
//output: left: 70, top: 40
变成坐标:
var elem = $("#wrapper");
var x = $("#wrapper").offset().left;
var y = $("#wrapper").offset().top;
console.log('x: ' + x + ' y: ' + y);
//output: x: 70 y: 40
jQuery getCoord()函数
jQuery.fn.getCoord = function()
{var elem = $(this);var x = elem.offset().left;var y = elem.offset().top;console.log('x: ' + x + ' y: ' + y);//output: x: 70 y: 40return {"x" : x,"y" : y};//note that it is not efficient as it breaks the jQuery chain//return elem;
};$('#wrapper').getCoord();
//output: Object { x=70, y=40 }
翻译自: https://www.sitepoint.com/jquery-coordinates-element/
jquery获取元素的坐标