简体   繁体   English

将鼠标悬停在链接旁边时如何创建弹出框?

[英]How to create popup boxes next to the links when mouse over them?

Here is what I want to implement: 这是我要实现的:

I have two hyperlinks that are displayed on the webpage: 我在网页上显示了两个超链接:

<a href="http://foo.com"> foo </a>

<a href="http://bar.com"> bar </a>

and I also have two descriptions to the links as divs: 而且我对div的链接也有两个描述:

<div id="foo">foo means foo</div>

<div id="bar">bar means bar</div>

Now, when I mouse over the link of foo, the corresponding description div should pop up, and it should pop up right next to where my cursor is. 现在,当我将鼠标悬停在foo的链接上时,应该弹出相应的描述div,并且应该在光标所在的位置旁边弹出它。

So if I mouse over "foo", a pop-up box containing "foo means foo" should appear right next to the mouse cursor. 因此,如果我将鼠标悬停在“ foo”上,则包含“ foo意味着foo”的弹出框应出现在鼠标光标旁边。 Same thing applies to "bar". 同样的情况适用于“酒吧”。

Please show a quick and simple way to implement this, with javascript/jquery, CSS, or combination of these. 请使用javascript / jquery,CSS或它们的组合,展示一种实现此目的的快速简单的方法。

PS I apologize for didn't explain clearly earlier, I actually want to add further links or images into the description div instead of pure text so a regular tool-tip perhaps would not do. PS抱歉,我之前并没有明确解释,实际上我想在description div中添加更多链接或图像,而不是纯文本,因此常规的工具提示可能不会。

Thanks. 谢谢。

Here is the simpliest solution. 这是最简单的解决方案。

HTML: HTML:

<a href="http://foo.com" data-tooltip="#foo">foo</a>
<a href="http://bar.com" data-tooltip="#bar">bar</a>

<div id="foo">foo means foo</div>
<div id="bar">bar means bar</div>

CSS: CSS:

div {
    position: absolute;
    display: none;
    ...
}​

JavaScript: JavaScript:

$("a").hover(function(e) {
    $($(this).data("tooltip")).css({
        left: e.pageX + 1,
        top: e.pageY + 1
    }).stop().show(100);
}, function() {
    $($(this).data("tooltip")).hide();
});

 $("a").hover(function(e) { $($(this).data("tooltip")).css({ left: e.pageX + 1, top: e.pageY + 1 }).stop().show(100); }, function() { $($(this).data("tooltip")).hide(); }); 
 div { position: absolute; display: none; border: 1px solid green; padding:5px 15px; border-radius: 15px; background-color: lavender; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script> <a href="http://foo.com" data-tooltip="#foo">foo</a> <a href="http://bar.com" data-tooltip="#bar">bar</a> <div id="foo">foo means foo</div> <div id="bar">bar means bar</div> 

​DEMO: http://jsfiddle.net/8UkHn/ 演示: http : //jsfiddle.net/8UkHn/

Have you considered using a "title" attribute in this case? 您是否考虑过在这种情况下使用“标题”属性?

<a href="http://foo.com" title="foo means foo"> foo </a>

See if this fits your need. 看看这是否符合您的需求。

What it does is, when you move mouse over the link "foo", a small box containing "foo means foo" will appear next to the mouse pointer. 它的作用是,当您将鼠标移到链接“ foo”上时,鼠标指针旁边将出现一个包含“ foo意味着foo”的小框。

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

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