我试图得到一个元素在窗口中的位置,如下所示:

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;

然而,底部和右边的人面前有-个……这是为什么呢?因为这些数字是正确的,所以它们不应该是负的.

推荐答案

而不是

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

Why aren't you doing

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

Edit: Your mistake is that you're doing

bottom = offset.top - bottom;

instead of

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

Jquery相关问答推荐

当一个很长的活动增加标题时,满历js每周都会出现.这是一种让标题只出现一次的方法吗?

在 div 中找到第一次出现的类

jQuery获取DOM node ?

如何在 jquery 中从这个日期减go 一周?

jQuery.parseJSON 单引号与双引号

如何在数据表中显示空数据消息

jQuery/JavaScript 碰撞检测

Zepto 和 jQuery 2 有什么区别?

datatable jquery - 表头宽度与正文宽度不对齐

使用 jQuery 将宽度设置为百分比

如何使用jquery获取点击链接的href?

测试两个元素是否相同

使用 JavaScript 获取用户代理

禁用 jquery Select 的下拉菜单

Bootstrap 4 文件输入

如何使用 jQuery 或纯 JS 重置所有复选框?

检测页面是否已完成加载

默认情况下如何将tinymce粘贴为纯文本

使用 TypeScript 设置 window.location

jQuery:通过 .attr() 添加两个属性;方法