我一直在寻找一个可以解决以下问题的脚本:
- 点击div外部时关闭它。
- 当另一个div被点击时,关闭一个div。
- 再次点击“更多信息”时关闭div。
我想知道为什么减号图标在div处于活动状态时不会停留在原位,以及如何在上述所有情况下恢复加号图标。
我想知道为什么减号图标在div处于活动状态时不会停留在原位,以及如何在上述所有情况下恢复加号图标。
a.trigger.isshown{
background:#fff url(http://icons.iconarchive.com/icons/deleket/sleek-xp-basic/24/Close-icon.png) 95% 65% no-repeat;
}
我将:active
选择器作为.trigger
的子类,正如@Christopher所评论的那样,并修复了.trigger
X按钮的行为,以相应地切换类:
$('.trigger').click(function(event) {
event.preventDefault();
var panel = $(this).next('.panel');
$('.panel').not(panel).hide();
panel.toggle("fast");
$('.trigger').not(this).removeClass('active'); //remove active class from other X buttons
$(this).toggleClass('active'); //toggle the clicked button's active class
});
这样做会从其他 X 按钮中删除 active
类,并根据要求切换当前按钮。
在 DOM 就绪处理程序中添加以下代码可在单击 .panel
和 .trigger
之外关闭框:
$(document).click('click', function(e) {
if (!$('.panel').is(':visible')) return;
var targ = $(e.target);
//doesn't close the boxes if target is .panel/.trigger or their descendant
if (targ.closest('.panel').length || targ.is('.panel')
|| targ.closest('.trigger').length || targ.is('.trigger')) return;
$('.panel').hide('fast');
$('.trigger').removeClass('active');
});
http://jsfiddle.net/dwZnG/ 试试这个大小。
a.trigger{
position: absolute;
text-decoration: none;
bottom:0;
right: 0;
font-size: 17px;
letter-spacing:-1px;
font-family: verdana, helvetica, arial, sans-serif;
color:#333;
padding: 20px 30px 10px 15px;
font-weight: 600;
background:#fff url(http://icons.iconarchive.com/icons/deleket/sleek-xp-basic/24/Add-icon.png) 95% 65% no-repeat;
display: block;
}
/* Change active to a class name*/
a.trigger.active {
background:#fff url(http://icons.iconarchive.com/icons/deleket/sleek-xp-basic/24/Close-icon.png) 95% 65% no-repeat;
}
然后是你的JS代码:
$(document).ready(function() {
$('.trigger').click(function(event) {
event.preventDefault();
var me = $(this);
var panel = me.next('.panel');
//If active is there, remove it
if (me.hasClass("active")) {
me.removeClass("active");
}
//If it ain't...add it
else {
me.addClass("active");
}
$('.panel').not(panel).hide();
panel.toggle("fast");
$(document).ready(function() {
$('.panel').hover(function() {
mouse_inside_div = true;
}, function() {
mouse_inside_div = false;
});
$('body').mouseup(function() {
if (!mouse_inside_div) $('.panel').hide();
});
});
});
});
基本上和Abody说的一样。一旦你掌握了这个,你应该能够弄清楚其余的功能。
:P
- Fabrício Mattémouse_inside_div
是干什么用的,你只想在按钮上点击时打开/关闭/切换吧? - Fabrício Mattéa.active
不会像那样工作。将 active 设置为 trigger 的子类类(a.trigger.active
)。 - SomeShinyObject