获取元素的右下角位置 [英] Get bottom and right position of an element

查看:90
本文介绍了获取元素的右下角位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to get the position of an element within the window like so:

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;

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

However the bottom and right have - in front of them... Why is this? as the numbers are correct just they should NOT be minus.

推荐答案

代替

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;

这篇关于获取元素的右下角位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆