确定鼠标点击位置的位置
我正在制作一个工具,允许人们使用 jQuery 双击 HTML 页面上的任何位置并留下消息。它在消息定位方面遇到了障碍。
我目前正在测试的代码:
$('body').bind('dblclick', function(e) {
var message = $('<div class="message">Form here</div>').css({
'left' : e.pageX,
'top' : e.pageY
});
$(this).append(message);
});
这会在鼠标单击的位置创建一条新的“消息”。虽然这在一定程度上有效,但一旦调整浏览器窗口的大小,“消息”就会移出位置。由于我最终希望保存消息(并检索它们),因此将它们加载到每个浏览器上的每个用户的不同位置并不理想。我希望消息附加到页面布局,即使该页面使用媒体查询。
我对这个问题的理解是,我需要相对于用户单击的最近的元素来定位消息。问题是。我该怎么办?
任何提示或提示将不胜感激。
更新 04/07/11
我在下面得到的大多数答案并不是我真正想要的。我已将我的页面示例上传到 JSfiddle 来向您展示我的设置。这里的问题是使消息附加在相对位置,以便在重新调整浏览器窗口大小时消息与内容一起移动。
例如。如果我将一条消息附加到页面上的第二段,如何让该消息与该段落保持一致,无论浏览器/设备宽度如何。据我所知,我需要建立最近的块级元素(或具有 ID 或类的元素),可用于相对定位消息。
希望能解决这个问题&感谢您的所有建议。
I am putting together a tool that allows people to double click and leave messages anywhere on a HTML page using jQuery. It have hit a stumbling block to do with positioning of the messages.
The code I am testing at the moment:
$('body').bind('dblclick', function(e) {
var message = $('<div class="message">Form here</div>').css({
'left' : e.pageX,
'top' : e.pageY
});
$(this).append(message);
});
This creates a new 'message' at the position of the mouse click. While this works to an extent, as soon as the browser window is resized the 'message' moves out of position. As I am eventually hoping to save the messages (and retrieve them) it is not ideal to have them load in a different locations for every user on every browser. I want the messages to attach to the page layout even if that page uses media queries.
My understanding of the problem is that I need to position the messages relative to the nearest element where the user has clicked. The question is. How do I this?
Any tips or hints would be greatly appreciated.
Update 04/07/11
Most of the answers I have had below are not really what I am after. I have uploaded an example of my page to JSfiddle to show you my setup. The problem here is making the messages attach in a relative position, so that as the browser window is re-sized the messages move along with the content.
For example. If I attach a message to the second paragraph on a page how do I get that message to stick with that paragraph, regardless of browser/device width. As far as I can tell I need to establish the nearest block level element (or element with an ID or class) that can be used to position the message relative to.
Hope that clears up the question & thanks for all your suggestions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
据我所知,可能存在几个问题:我看到的第一个问题是您需要确保“body”元素覆盖整个页面,因为它只会扩展到其框允许的范围。 (我通常意识到,简单地设置div的背景通常可以让你看到它的范围,但body标签是一个特例。它的背景延伸到浏览器的整个可见区域,但它的盒子可能不会)
其次,它是将 css“position”属性设置为“absolute”很重要,否则绝对定位将不起作用:此代码有效:
As far as I can tell, there are potentially several problems: the first that I can see is you need to make sure that the "body" element covers the whole page, because it will only extend as far as its box allows it to. (i realise normally, simply setting the background of a div usually lets you see its extent, but the body tag is a special case. Its background extends to the entire visible area of the browser, but it's box may not)
Secondly, it is important to set the css "position" property to "absolute" or absolute positioning won't work: This code works:
这应该显示如何跟踪鼠标移动 http://docs.jquery.com/Tutorials:Mouse_Position #How_do_I_find_the_mouse_position.3F
除此之外,您可以使用
resize()
来改变div的位置This should shows how to track mouse movements http://docs.jquery.com/Tutorials:Mouse_Position#How_do_I_find_the_mouse_position.3F
along with this you may use
resize()
to change position of div我建议在页面上放置一个具有固定宽度和高度的
div
。然后将dblclick
函数应用于该div
而不是body
。您可能还想绝对定位动态
div
元素,以便它们出现在正确的位置。这是一个小提琴示例。I would suggest placing a single
div
on your page, with fixed width and height. Then apply yourdblclick
function to thatdiv
rather than thebody
.You may also want to position the dynamic
div
elements absolutely, so they appear in the correct positions. Here is an example fiddle.您需要定位消息div:
http://jsfiddle.net/PvFTN/
You need to position the message div:
http://jsfiddle.net/PvFTN/