jquery 显示/隐藏 div 和计数器
我有随机数量的 DIV(最少 1 个,最多 10 个),
<div id="container">
<div class="foo">Content</div> <!-- div 1 -->
<div class="foo">Content</div> <!-- div 2 -->
<div class="foo">Content</div> <!-- div 3 -->
<div class="foo">Content</div> <!-- div 4 -->
<div class="foo">Content</div> <!-- div 5 -->
<div class="foo">Content</div> <!-- i need this one hidden -->
<div class="foo">Content</div> <!-- and this one -->
</div>
我希望前 5 个 div 可见(使用 .show() 或使用类,都没关系)。任何额外的 DIV 都应该隐藏。
然后,我使用以下命令模拟 div 的“关闭”:
$('.foo').click(function(){
$(this).fadeOut('slow');
});
它将删除单击的 div,导致下面的所有 div 上移一位。这就是我想要的效果。
但是,我在这里需要一些逻辑。
如果我的 DIVS 少于 5 个,则应禁用关闭设施。如果我有超过 5 个 DIV,那么当一个 div “关闭”时,我希望下一个“隐藏”div 变得可见。
如果需要,我可以向每个 DIV 添加 ID,例如“foo1”、“foo2”等 ID。
I have a random amount of DIVs (minimum 1, max of 10)
<div id="container">
<div class="foo">Content</div> <!-- div 1 -->
<div class="foo">Content</div> <!-- div 2 -->
<div class="foo">Content</div> <!-- div 3 -->
<div class="foo">Content</div> <!-- div 4 -->
<div class="foo">Content</div> <!-- div 5 -->
<div class="foo">Content</div> <!-- i need this one hidden -->
<div class="foo">Content</div> <!-- and this one -->
</div>
I want the first 5 divs to be visible (either with .show() or with a class, doesn't matter). Any additional DIVs should be hidden.
I then simulate the "closing" of a div with:
$('.foo').click(function(){
$(this).fadeOut('slow');
});
which removes the clicked div, causing all the divs below to move up one. This is my desired effect.
However, I need some logic here.
If I have less than 5 DIVS, the close facility should be disabled. If I have more than 5 DIVs, then when a div is "closed", i want the next "hidden" div to become visible.
I can add IDs to each DIV if required with IDs like "foo1" "foo2" etc.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
像这样的东西应该可以工作:
您可以在这里测试。其作用是使用
:gt(4)
(从 0 开始)选择器。然后我们使用.delegate()
来提高效率(尽管 < a href="http://api.jquery.com/click/" rel="nofollow">.click()
也可以)。如果没有更多的隐藏,就没有效果。如果有更多隐藏的内容,请淡出我们单击的内容,在淡入淡出的末尾显示:first
:hiddden< /code>
一,以及
.remove()
我们完全消失了。Something like this should work:
You can test it out here. What this does is hides all past 5 using the
:gt(4)
(0-based) selector. Then we're using.delegate()
for efficiency (though a.click()
would work too). If there aren't any more hidden, there's no effect. If there are more hidden, fade out the one we clicked, at at the end of the fade show the:first
:hiddden
one, and.remove()
the one we faded out completely.