jQuery鼠标位置相对窗口 [英] jquery mouse position relative window

查看:130
本文介绍了jQuery鼠标位置相对窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取相对于窗口的确切鼠标位置.

I'm trying to get the exact mouse position relative to the window.

这是我的问题...

  • document.height = 1600(实际文档大小)
  • window.height = 400(可见)
  • document.height = 1600 (actual document size)
  • window.height = 400 (viewable)

我需要找出鼠标相对于窗口的位置,而不是pageY属性提供的文档的位置.

I need to figure out the mouse position relative to the window, not to the document which the pageY attribute provides.

这是一个大的工具提示,它会在表项目的moesover上弹出.如果屏幕底部没有足够的空间(窗口已满),则工具提示将显示在指针上方,否则显示在指针下方.在文档大小大于页面大小(长表)之前,此方法可以正常工作.

It's for a large tooltip, which gets popped in on mouesover for a table item. If there is not enough room at the bottom of the screen (window is maxed), then the tooltip get displayed above the pointer, otherwise, below the pointer. This works fine until the document size is greater than pagesize (long table).

谢谢, 卢克

推荐答案

您可以减去 .scrollTop() window中的window中的>,以获取窗口中的位置,如下所示:

You can subtract .scrollTop() of the window from pageY to get the position in the window, like this:

var top = e.pageY - $(window).scrollTop();

您可以在这里尝试一下,看看控制台.

这篇关于jQuery鼠标位置相对窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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