获取元素的右下角位置


82

我试图像这样在窗口中获取元素的位置:

var link = $(element);

var offset = link.offset();
var top = offset.top;
var left = offset.left;
var bottom = $(window).height() - link.height();
bottom = offset.top - bottom;
var right = $(window).width() - link.width();
right = offset.left - right;

但是底部和右侧-在它们前面...这是为什么?因为数字是正确的,只是它们不应为负。

Answers:


94

代替

var bottom = $(window).height() - link.height();
bottom = offset.top - bottom;

你为什么不做

var bottom = $(window).height() - top - link.height();

编辑:你的错误是你在做

bottom = offset.top - bottom;

代替

bottom = bottom - offset.top; // or bottom -= offset.top;

36
var link = $(element);
var offset = link.offset();

var top = offset.top;
var left = offset.left;

var bottom = top + link.outerHeight();
var右=左+ link.outerWidth();

6
// Returns bottom offset value + or - from viewport top
function offsetBottom(el, i) { i = i || 0; return $(el)[i].getBoundingClientRect().bottom }

// Returns right offset value
function offsetRight(el, i) { i = i || 0; return $(el)[i].getBoundingClientRect().right }

var bottom = offsetBottom('#logo');
var right = offsetRight('#logo');

这将找到从视口的顶部和左侧到元素的确切边缘的距离,而没有其他距离。因此,假设您的徽标为350px,左边距为50px,变量“ right”将保持值为400,因为这是到达元素边缘所需的实际距离(以像素为单位),无论是否有更多填充或右边的边距。

如果将Box-Sizing CSS属性设置为border-box,它将继续工作,就像设置为默认的content-box一样。


给我最好的答案!
geoyws

3

您可以使用.POSITION()

var link = $(element);
var position = link.position(); //cache the position
var right = $(window).width() - position.left - link.width();
var bottom = $(window).height() - position.top - link.height();

4
在jQuery doc中,没有在position()的结果对象中引用“ right”属性。Doc here
Mordhak 2012年

4
是的,但应使用偏移量而不是位置,偏移量取决于文档的顶部/左侧,位置是指父级。
Mordhak 2012年

@Mordhak,是的,但是正如您所看到的,我也正在使用windows.width()和类似的方法进行进一步的计算。
Starx 2012年

1
小心位置。如果存在
则不

1

我认为

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>

<div>Testing</div>
<div id="result" style="margin:1em 4em; background:rgb(200,200,255); height:500px"></div>
<div style="background:rgb(200,255,200); height:3000px; width:5000px;"></div>

<script>
(function(){
    var link=$("#result");

    var top = link.offset().top; // position from $(document).offset().top
    var bottom = top + link.height(); // position from $(document).offset().top

    var left = link.offset().left; // position from $(document).offset().left
    var right = left + link.width(); // position from $(document).offset().left

    var bottomFromBottom = $(document).height() - bottom;
    // distance from document's bottom
    var rightFromRight = $(document).width() - right;
    // distance from document's right

    var str="";
    str+="top: "+top+"<br>";
    str+="bottom: "+bottom+"<br>";
    str+="left: "+left+"<br>";
    str+="right: "+right+"<br>";
    str+="bottomFromBottom: "+bottomFromBottom+"<br>";
    str+="rightFromRight: "+rightFromRight+"<br>";
    link.html(str);
})();
</script>

结果是

top: 44
bottom: 544
left: 72
right: 1277
bottomFromBottom: 3068
rightFromRight: 3731

在我的Chrome浏览器中。

当文档可滚动时,$(window).height()返回浏览器视口的高度,而不是滚动中隐藏了某些部分的文档的宽度。请参阅http://api.jquery.com/height/


1

这是一个jquery函数,它返回页面上任何类或id的对象

var elementPosition = function(idClass) {
            var element = $(idClass);
            var offset = element.offset();

            return {
                'top': offset.top,
                'right': offset.left + element.outerWidth(),
                'bottom': offset.top + element.outerHeight(),
                'left': offset.left,
            };
        };


        console.log(elementPosition('#my-class-or-id'));

0

香草Javascript答案

var c = document.getElementById("myElement").getBoundingClientRect();
var bot = c.bottom;
var rgt = c.right;

为了清楚起见,只要您为其分配了ID<img> <div> <p>等,该元素就可以是任何东西。

例如

<img
    id='myElement'
    src='/img/logout.png'
    className='logoutImg img-button'
    alt='Logout'
/>
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.