[英]How do I get the vertical position of an image in Javascript? (in relation to the document)
I am trying to dynamically get the Y position of an image in JavaScript in relation to the screen/document. 我试图动态获取JavaScript中与屏幕/文档相关的图像的Y位置。 How can I do that? 我怎样才能做到这一点? is there anything like in AC3 where you can do: getProperty(obj, _x)
? 在AC3中有什么可以做的: getProperty(obj, _x)
?
Replace your image id
to get y
or top
position of image: 替换您的图片id
以获取图像的y
或top
位置:
JS: JS:
var offsets = document.getElementById('image_id').getBoundingClientRect();
//HERE^
var top = offsets.top;
Jquery: If you use jquery library, do: Jquery:如果你使用jquery库,请执行:
$(document).ready(function(){
var position = $("#image_id").position();
var top = position.top;
});
element.getBoundingClientRect()
will return an object with values for the top, bottom, left, and right sides of the element. element.getBoundingClientRect()
将返回一个对象,其中包含element.getBoundingClientRect()
的顶部,底部,左侧和右侧的值。
To get the y position of an element, just do element.getBoundingClientRect().top
. 要获取元素的y位置,只需执行element.getBoundingClientRect().top
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.