好的......所以基本上你想要做的是<div>在你的每个选项卡中都有一个元素。当用户单击其中一个选项卡时,jQuery 将确保没有其他“弹出”的 div 元素可见,然后显示相应的 div。
我将以 3 个标签为例 -
<div class="tab" id="pink">
<img src="http://placehold.it/160x160" width="160" height="160" />
<span class="tab_text">TITLE 01</span>
<div class="pop-out">Some pink information</div>
</div>
<div class="tab" id="turquoise">
<img src="http://placehold.it/160x160" width="160" height="160" />
<span class="tab_text">TITLE 02</span>
<div class="pop-out">Some turquoise information</div>
</div>
<div class="tab" id="yellow">
<img src="http://placehold.it/160x160" width="160" height="160" />
<span class="tab_text">TITLE 03</span>
<div class="pop-out">Some yellow information</div>
</div>
请注意,每次我使用id属性时,它们都是唯一的。该id属性应始终具有唯一值,而不是class您可以看到的属性可以具有相同的值。class="pop-out"
pop-out将是我们识别所有弹出 div 元素的方式
- 当用户单击选项卡时,我们将添加一个附加
sel类(selected 的缩写)。这样我们就可以跟踪上次打开的 div(如果有的话),并确保在打开新的之前关闭它。
现在,假设我们希望触发器是整个选项卡。用户点击它的任何地方都会触发弹出窗口。
$(".tab").on('click',function(){
$("div.pop-out.sel").fadeOut().removeClass('sel'); // this will hide the last selected div and remove its selected class
$(this).find("div.pop-out").fadeIn().addClass('sel'); // this will display the div of the element the user clicked and add the selected class.
}
显然,您可以替换任何您想要的动画。
现在我们还可以使用以下方法跟踪打开的选项卡 -
$("div.pop-out.sel").closest("div.tab");
这将返回当前正在显示的弹出 div 的父级。记得先测试一个选定的 div 是否 真的存在 :)