jQuery弹出气泡/工具提示[关闭]


99

我正在尝试创建一个“气泡”,该气泡可以在onmouseover事件触发时弹出,并且只要鼠标悬停在引发onmouseover事件的项目上方,或者如果将鼠标移到气泡中,它就会保持打开状态。我的气泡需要具有各种HTML和样式,包括超链接,图像等。

我基本上通过编写约200行丑陋的JavaScript来实现了这一目标,但是我真的很想找到一个jQuery插件或其他一些方法来解决这一问题。


1
@bluefeet关闭主题?认真吗 这个人问了一个有关如何使用JQuery缩短200行代码的问题,它已经使用了将近四分之一的次数(刚刚解决了我的问题),您将其标记为主题了吗?与其他任何问题相比,这会带来更多自以为是的答案吗?
克里斯·夏普

@ChrisSharp您是否已阅读关闭它的原因?它专门要求“一个很好的jQuery插件来制作奇特的泡泡”。提出建议的问题不合时宜,但如果您认为可以将其改写成主题,则可以随时提出建议以使其成形。
塔林

Answers:


158

Qtip是我见过的最好的Qtip。它是MIT许可的产品,美观,具有您所需的所有配置。

我最喜欢的轻便选项是小费。也是麻省理工学院许可的。它启发了Bootstrap的工具提示插件


6
到目前为止最好的。一行代码与其他人提供的所有其他巨大解决方案相比。我希望这个回应能被投票通过。
彼得·沃克

2
Qtip与jQuery 1.4+存在兼容性问题。简单的单行修复qTip插件可以修复它。看到这里:craigsworks.com/projects/forums/…–
tchaymore

4
我今天看了一下Qtip,虽然它确实起作用,但有一些缺点:一段时间未更新,丢失或没有记录一些明显的东西(想用提示时调用的函数来构建工具提示文本显示),并且下载量很大(部分原因是它似乎包含很多东西,例如圆角样式)。希望不会被视为消极的-只是想节省一些时间。绝对值得考虑,但也有一些缺点。
Cymen 2010年

4
@Cymen,我无法说说大约在10年9月之前的情况,但是您所说的话不再是真的。它是:活跃的,有据可查的,并且允许jQuery UI的下载大小非常可定制。
Kirk Woll

52

也可以通过mouseover事件轻松完成此操作。我已经做到了,它根本不需要200行。从触发事件开始,然后使用将创建工具提示的功能。

$('span.clickme').mouseover(function(event) {
    createTooltip(event);               
}).mouseout(function(){
    // create a hidefunction on the callback if you want
    //hideTooltip(); 
});

function createTooltip(event){          
    $('<div class="tooltip">test</div>').appendTo('body');
    positionTooltip(event);        
};

然后创建一个函数,该函数将工具提示放置在触发mouseover事件的DOM元素的偏移位置上,这对于css是可行的。

function positionTooltip(event){
    var tPosX = event.pageX - 10;
    var tPosY = event.pageY - 100;
    $('div.tooltip').css({'position': 'absolute', 'top': tPosY, 'left': tPosX});
};

1
简单实用,可以轻松编写时不需要XX ko插件。PS:位置:绝对缺失:)
kheraud 2011年

1
您可能想将“ px”单位添加到整数中。'top': tPosY + 'px'等等。
罗布斯托

1
$('span.klickme')-这很糟糕:)
formatc 2012年

我喜欢你的方法。非常简单的方法,无需使用任何外部库
AMIC MING

12

尽管qTip(公认的答案)很好,但我还是开始使用它,但它缺少我需要的某些功能。

然后我偶然发现了PoshyTip-它非常灵活,而且非常易于使用。(我可以做我所需要的)


4

好的,经过一些工作,我能够在适当的时候弹出气泡并消失。有很多样式需要仍然发生,但这基本上就是我使用的代码。

<script type="text/javascript">
    //--indicates the mouse is currently over a div
    var onDiv = false;
    //--indicates the mouse is currently over a link
    var onLink = false;
    //--indicates that the bubble currently exists
    var bubbleExists = false;
    //--this is the ID of the timeout that will close the window if the user mouseouts the link
    var timeoutID;

    function addBubbleMouseovers(mouseoverClass) {
        $("."+mouseoverClass).mouseover(function(event) {
            if (onDiv || onLink) {
                return false;
            }

            onLink = true;

            showBubble.call(this, event);
        });

        $("." + mouseoverClass).mouseout(function() {
            onLink = false;
            timeoutID = setTimeout(hideBubble, 150);
        });
    }

    function hideBubble() {
        clearTimeout(timeoutID);
        //--if the mouse isn't on the div then hide the bubble
        if (bubbleExists && !onDiv) {
             $("#bubbleID").remove();

             bubbleExists = false;
        }
    }

    function showBubble(event) {
        if (bubbleExists) {
            hideBubble();
        }

        var tPosX = event.pageX + 15;
        var tPosY = event.pageY - 60;
        $('<div ID="bubbleID" style="top:' + tPosY + '; left:' + tPosX + '; position: absolute; display: inline; border: 2px; width: 200px; height: 150px; background-color: Red;">TESTING!!!!!!!!!!!!</div>').mouseover(keepBubbleOpen).mouseout(letBubbleClose).appendTo('body');

        bubbleExists = true;
    }

    function keepBubbleOpen() {
        onDiv = true;
    }

    function letBubbleClose() {
        onDiv = false;

        hideBubble();
    }


    //--TESTING!!!!!
    $("document").ready(function() {
        addBubbleMouseovers("temp1");
    });
</script>

这是附带的html的一个片段:

<a href="" class="temp1">Mouseover this for a terribly ugly red bubble!</a>

4

我已经编写了一个有用的jQuery插件,只需使用jQuery中的一行代码即可轻松创建智能气泡弹出窗口!

您可以做什么:-将弹出窗口附加到任何DOM元素!-自动管理mouseover / mouseout事件!-设置自定义弹出窗口事件!-创建智能的阴影弹出窗口!(在IE中也是如此!)-在运行时选择弹出窗口的样式模板!-在弹出窗口中插入HTML消息!-将许多选项设置为:距离,速度,延迟,颜色...

Internet Explorer 6 +,Firefox,Opera 9 +,Safari完全支持Popup的阴影和彩色模板

您可以从http://plugins.jquery.com/project/jqBubblePopup下载源



3

在我看来,您不需要鼠标悬停事件:您需要jQuery hover()事件。

您似乎想要的是“丰富”工具提示,在这种情况下,我建议使用jQuery工具提示。使用bodyHandler选项,您可以放入任意HTML。


嘿,谢谢您的超快速反应!我只是浏览了文档,但不确定是否可以将“工具提示”保持在固定位置,以便您可以将鼠标移动到该位置并单击链接。你以前用过吗?同时,我将下载并开始播放
jakejgordon

2

我正在尝试创建一个“气泡”,当触发onmouseover事件时,该气泡可以弹出,并且只要鼠标悬停在引发onmouseover事件的项目上,或者如果将鼠标移至气泡中,该气泡就会保持打开状态。我的气泡需要具有各种html和样式,包括超链接,图像等。

所有这些事件均由此插件完全管理...

http://plugins.jquery.com/project/jqBubblePopup


此链接不再是很好的链接
Prescott Chartier


2

jQuery Bubble Popup插件的新版本3.0支持jQuery v.1.7.2,目前是最著名的javascript库的最新稳定版本。

3.0版最有趣的功能是,您可以将jQuery&Bubble Popup插件与其他任何库和javascript框架(如Script.aculo.us,Mootols或Prototype)一起使用,因为该插件已完全封装以防止不兼容问题;

jQuery Bubble Popup经过了测试,并支持许多已知和“未知”的浏览器。请参阅文档以获取完整列表。

像以前的版本一样,jQuery Bubble Popup插件仍在MIT许可下发布。只要版权标头保持不变,您就可以在商业或个人项目中自由使用jQuery Bubble Popup。

下载最新版本,或访问http://www.maxvergelli.com/jquery-bubble-popup/上的实时演示和教程


1

自动调整大小的简单弹出气泡

index.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  <link href="bubble.css" type="text/css" rel="stylesheet" />
  <script language="javascript" type="text/javascript" src="jquery.js"></script>
  <script language="javascript" type="text/javascript" src="bubble.js"></script>
</head>
<body>
  <br/><br/>
  <div class="bubbleInfo">
      <div class="bubble" title="Text 1">Set cursor</div>
  </div>
  <br/><br/><br/><br/>
  <div class="bubbleInfo">
      <div class="bubble" title="Text 2">Set cursor</div>
  </div>
</body>
</html>

bubble.js

$(function () {     
  var i = 0;
  var z=1;
  do{
    title = $('.bubble:eq('+i+')').attr('title');
    if(!title){
      z=0;
    } else {
       $('.bubble:eq('+i+')').after('<table style="opacity: 0; top: -50px; left: -33px; display: none;" id="dpop" class="popup"><tbody><tr><td id="topleft" class="corner"></td><td class="top"></td><td id="topright" class="corner"></td></tr><tr><td class="left"></td><td>'+title+'</td><td class="right"></td></tr><tr><td class="corner" id="bottomleft"></td><td class="bottom"><img src="bubble/bubble-tail.png" height="25px" width="30px" /></td><td id="bottomright" class="corner"></td></tr></tbody></table>');
       $('.bubble:eq('+i+')').removeAttr('title');
    }
    i++;
  }while(z>0)

  $('.bubbleInfo').each(function () {
    var distance = 10;
    var time = 250;
    var hideDelay = 500;        
    var hideDelayTimer = null;       
    var beingShown = false;
    var shown = false;
    var trigger = $('.bubble', this);
    var info = $('.popup', this).css('opacity', 0);        

    $([trigger.get(0), info.get(0)]).mouseover(function () {
      if (hideDelayTimer) clearTimeout(hideDelayTimer);
      if (beingShown || shown) {
        // don't trigger the animation again
        return;
      } else {
        // reset position of info box
        beingShown = true;

        info.css({
        top: -40,
        left: 10,
        display: 'block'
        }).animate({
        top: '-=' + distance + 'px',
        opacity: 1
        }, time, 'swing', function() {
          beingShown = false;
          shown = true;
        });
      }          
      return false;
    }).mouseout(function () {
      if (hideDelayTimer) clearTimeout(hideDelayTimer);
      hideDelayTimer = setTimeout(function () {
        hideDelayTimer = null;
        info.animate({
          top: '-=' + distance + 'px',
          opacity: 0
        }, time, 'swing', function () {
          shown = false;
          info.css('display', 'none');
        });
      }, hideDelay);
      return false;
    });
  }); 
});

bubble.css

/* Booble */
.bubbleInfo {
    position: relative;
    width: 500px;
}
.bubble {       
}
.popup {
    position: absolute;
    display: none;
    z-index: 50;
    border-collapse: collapse;
    font-size: .8em;
}
.popup td.corner {
    height: 13px;
    width: 15px;
}
.popup td#topleft { 
    background-image: url(bubble/bubble-1.png); 
} 
.popup td.top { 
    background-image: url(bubble/bubble-2.png); 
}
.popup td#topright { 
    background-image: url(bubble/bubble-3.png); 
}
.popup td.left { 
    background-image: url(bubble/bubble-4.png); 
}
.popup td.right { 
    background-image: url(bubble/bubble-5.png); 
}
.popup td#bottomleft { 
    background-image: url(bubble/bubble-6.png); 
}
.popup td.bottom { 
    background-image: url(bubble/bubble-7.png); 
    text-align: center;
}
.popup td.bottom img { 
    display: block; 
    margin: 0 auto; 
}
.popup td#bottomright { 
    background-image: url(bubble/bubble-8.png); 
}


1

您可以为此使用qTip。但是,您必须编写一些代码才能在mouseover事件上启动它。并且如果您想在文本字段上使用默认水印,则必须使用水印插件...

我意识到这会导致很多重复的代码。因此,我在qTip之上编写了一个插件,它使将信息弹出窗口附加到表单字段确实非常容易。您可以在这里查看:https : //bitbucket.org/gautamtandon/jquery.attachinfo

希望这可以帮助。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.