jquery javascript查找离元素有多远的位置:relative(jquery javascript find how far left an element is with positio

编程入门 行业动态 更新时间:2024-10-19 11:57:27
jquery javascript查找离元素有多远的位置:relative(jquery javascript find how far left an element is with position:relative)

在jquery / javascript中我试图找到一个元素与位置相距多远:相对。 屏幕左侧和屏幕左侧有几个元素。 因为它的位置是:相对和浮动:左边使用:

$(this).css('left');

每次给我'0px'。 那么我怎么才能发现元素离屏幕左侧有多远呢?

in jquery/javascript I'm trying to find how far left an element is with position:relative. There are several elements between this one and the left of the screen. since it is positioned:relative and float:left using:

$(this).css('left');

gives me '0px' every time. So how do I found how far the element is from the left of the screen?

最满意答案

你可以使用jQuery的.offset()方法。

这是一个例子: http //jsfiddle.net/PgbmV/

$(this).offset().left; http://api.jquery.com/offset/

这将给出与文档相关的左侧位置。

如果您想要相对于其容器的位置,您将以类似的方式使用.position() 。

$(this).position().left; http://api.jquery.com/position/

You can use jQuery's .offset() method.

Here's an example: http://jsfiddle.net/PgbmV/

$(this).offset().left; http://api.jquery.com/offset/

This will give the left position relative to the document.

If you want the position relative to its container, you would use .position() in a similar manner.

$(this).position().left; http://api.jquery.com/position/

更多推荐

本文发布于:2023-07-23 02:34:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1226499.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:有多远   元素   位置   javascript   jquery

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!