简体   繁体   English

相对于鼠标滚动位置定位div

[英]Positioning a div relative to mouse scroll position

On some mouse over, div1 is displayed.How to display the div next to the mouse pointer so that even mouseover at the end of the context the div should show up next to the mouse pointer. 在鼠标悬停时,将显示div1。如何在鼠标指针旁边显示div,以便即使鼠标悬停在上下文末尾,div也应显示在鼠标指针旁边。

<style type="text/css">
#div1 { width: 200px; height: 30px; background-color: #a9a9a9; color: #fff; position: absolute; }
</style>
<script src="jquery.js" type="text/javascript"></script>
<script type="text/javascript">
$(function(){
$(window).mouseover(function(event){
$("#div1").css({'top': event.pageY, 'left': event.pageX});  
});
});
</script>
<div id="div1">mouseover me</div>

The usual term for what you want to do is "tooltip." 您想要做什么的常用术语是“工具提示”。

Your best bet is probably to use a JavaScript framework, like Prototip , which provides this functionality (as opposed to trying to code it yourself). 最好的选择是使用JavaScript框架(如Prototip) ,该框架提供此功能(而不是尝试自己编写代码)。 (Search that link for the "Hooking" section to see how you would go about creating such a tooltip with Prototip .) (在该链接的“挂钩”部分中搜索,以了解如何使用Prototip创建这样的工具提示。)

This might help: http://webdevpad.blogspot.com/2010/07/cross-browser-mouse-coordinates.html 这可能会有所帮助: http : //webdevpad.blogspot.com/2010/07/cross-browser-mouse-coordinates.html

Once you get the mouse position, give it a small offset, pass the coordinates to your tooltip div, and remember to give your tooltip position:fixed. 一旦获得鼠标位置,请给它一个小的偏移量,将坐标传递到您的工具提示div,并记住给您的工具提示位置:固定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM