你可以用下一个方法:
为你所有的滑动<div>s 添加一些类。以class="slickbox" 为例。
每个.click() 都以$('.slickbox').hide(); 开头,首先隐藏所有框,然后展开您需要的框。
例如:
<div style="clear:both;">
<div style="float:left;">
<a href="#" id="slick-slidetoggle2">Slide toggle the box 2</a>
</div>
<div id="slickbox2" class="slickbox">
2: This is the box that will be shown and hidden and toggled at your whim. :)
</div>
</div>
clicks 之一将如下所示。
$('#slick-slidetoggle2').click(function() {
$('.slickbox').hide(); // hide all boxes (doesn't matter if they were opened or not)
$('#slickbox2').show('slide', 400); // and open one that was clicked
return false;
});
这样您就可以扩展您的Like Us 轻松添加新项目,而无需为每个打开/关闭的项目编写.hide()。
更新:如果您需要在用户再次点击时关闭项目,您可以将按钮.click() 处理程序的show/hide 部分更改为以下.
$('#slick-slidetoggle2').click(function() {
if ( $('#slickbox2').is(':hidden') ) { // check if current box is hidden
$('.slickbox').hide(); // hide all boxes (doesn't matter if they were opened or not)
$('#slickbox2').show('slide', 400); // and open this one
} else // if it was already opened
$('#slickbox2').hide('slide', 400); // hiding the box
return false;
});
:hidden 选择器将跟踪对象的状态可见性,并据此决定如何处理该框。