javascript获取元素离文档各边距离的方法
apollokk 人气:0本文实例讲述了javascript获取元素离文档各边距离的方法。分享给大家供大家参考。具体实现方法如下:
复制代码 代码如下:
function getDistance(obj) {
if (!obj instanceof jQuery) {
obj = $(obj);
}
var distance = {};
distance.top = (obj.offset().top - $(document).scrollTop());
distance.bottom = ($(window).height() - distance.top - obj.outerHeight());
distance.left = (obj.offset().left - $(document).scrollLeft());
distance.right = ($(window).width() - distance.left - obj.outerWidth());
return distance;
}
if (!obj instanceof jQuery) {
obj = $(obj);
}
var distance = {};
distance.top = (obj.offset().top - $(document).scrollTop());
distance.bottom = ($(window).height() - distance.top - obj.outerHeight());
distance.left = (obj.offset().left - $(document).scrollLeft());
distance.right = ($(window).width() - distance.left - obj.outerWidth());
return distance;
}
希望本文所述对大家的javascript程序设计有所帮助。
加载全部内容