如何计算html元素在屏幕上的坐标

$(window).height();//是文档窗口高度 
$("div").offset().top//是标签距离顶部高度(没有到下面的距离,比如$("div").offset().down)
$("div").offset().left//是标签距离左边高度(没有到右面的距离,比如$("div").offset().right)
$(document).scrollTop();//是滚动条的滚动高度
$("div").height();//是标签高度
你要的高度+$("div").height()+[$("div").offset().top-$(document).scrollTop()]=$(window).height();

经过简单的数学变换即可得到你要的值了
获取页面某一元素的绝对X,Y坐标,可以用offset():
var X = $(‘#DivID’).offset().top;
var Y = $(‘#DivID’).offset().left;
获取相对(父元素)位置:
var X = $(‘#DivID’).position().top;
var Y = $(‘#DivID’).position().left;
通过getBoundingClientRect方法获取对象位置,包含: left , top , right , bottom 4个参数值。
 
 
来自:https://zhidao.baidu.com/question/1737598449272803747.html

猜你喜欢

转载自blog.csdn.net/cnm_csdn_wt/article/details/78977763