我正在尝试创建一个“气泡”,该气泡可以在onmouseover
事件触发时弹出,并且只要鼠标悬停在引发onmouseover
事件的项目上方,或者如果将鼠标移到气泡中,它就会保持打开状态。我的气泡需要具有各种HTML和样式,包括超链接,图像等。
我基本上通过编写约200行丑陋的JavaScript来实现了这一目标,但是我真的很想找到一个jQuery插件或其他一些方法来解决这一问题。
我正在尝试创建一个“气泡”,该气泡可以在onmouseover
事件触发时弹出,并且只要鼠标悬停在引发onmouseover
事件的项目上方,或者如果将鼠标移到气泡中,它就会保持打开状态。我的气泡需要具有各种HTML和样式,包括超链接,图像等。
我基本上通过编写约200行丑陋的JavaScript来实现了这一目标,但是我真的很想找到一个jQuery插件或其他一些方法来解决这一问题。
Answers:
Qtip是我见过的最好的Qtip。它是MIT许可的产品,美观,具有您所需的所有配置。
我最喜欢的轻便选项是小费。也是麻省理工学院许可的。它启发了Bootstrap的工具提示插件。
也可以通过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});
};
'top': tPosY + 'px'
等等。
好的,经过一些工作,我能够在适当的时候弹出气泡并消失。有很多样式需要仍然发生,但这基本上就是我使用的代码。
<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>
我已经编写了一个有用的jQuery插件,只需使用jQuery中的一行代码即可轻松创建智能气泡弹出窗口!
您可以做什么:-将弹出窗口附加到任何DOM元素!-自动管理mouseover / mouseout事件!-设置自定义弹出窗口事件!-创建智能的阴影弹出窗口!(在IE中也是如此!)-在运行时选择弹出窗口的样式模板!-在弹出窗口中插入HTML消息!-将许多选项设置为:距离,速度,延迟,颜色...
Internet Explorer 6 +,Firefox,Opera 9 +,Safari完全支持Popup的阴影和彩色模板
QTip有jQuery 1.4.2的错误。我不得不切换到jQuery Bubble Pop up http://www.vegabit.com/jquery_bubble_popup_v2/#examples,效果很好!
在我看来,您不需要鼠标悬停事件:您需要jQuery hover()事件。
您似乎想要的是“丰富”工具提示,在这种情况下,我建议使用jQuery工具提示。使用bodyHandler选项,您可以放入任意HTML。
我正在尝试创建一个“气泡”,当触发onmouseover事件时,该气泡可以弹出,并且只要鼠标悬停在引发onmouseover事件的项目上,或者如果将鼠标移至气泡中,该气泡就会保持打开状态。我的气泡需要具有各种html和样式,包括超链接,图像等。
所有这些事件均由此插件完全管理...
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/上的实时演示和教程 。
自动调整大小的简单弹出气泡
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);
}
Tiptip也是一个不错的图书馆。
您可以为此使用qTip。但是,您必须编写一些代码才能在mouseover事件上启动它。并且如果您想在文本字段上使用默认水印,则必须使用水印插件...
我意识到这会导致很多重复的代码。因此,我在qTip之上编写了一个插件,它使将信息弹出窗口附加到表单字段确实非常容易。您可以在这里查看:https : //bitbucket.org/gautamtandon/jquery.attachinfo
希望这可以帮助。