如何测试slideToggle后DIV是打开还是关闭
我有一个 Jquery Ui 按钮 (#toggleIcons),单击该按钮时,会切换 div (.icons) 以显示一些图标。我还使用 Jquery Isotope 和 Infinitescroll 动态添加新图像。我想做的是有一种方法可以在添加和更新新图像时保留幻灯片切换状态。 ifinitescroll 有一个回调函数,这样我就可以更新页面和图标的状态。
//My button click function
$("#styleSwitcher #toggleIcons" ).click(function() {
$('.icons').slideToggle('slow');
//for Isotope to update the layout
$('#container').isotope('reLayout')
return false;
});
//code I am working on to put in callback to test if div is open or closed
if($(".icons").is(":hidden"))
{
$('.icons').hide();
}
else
{
$('.icons').show();
}
不工作。任何帮助或指导将不胜感激。谢谢
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
你的情况倒退了:
You have your condition backwards:
将检查状态脚本放入函数中 - 让它保留一点..
Put check state script in a function - let it hold a bit..
为什么不只是 切换 或 slideToggle 吗?
why not just toggle or slideToggle it?
我会使用
:visible
但如果你想坚持你的代码
可能应该阅读
I would use
:visible
but if you want to stick to your code
should probably read
我做了同样的事情,但是对于基于 id 的设置,然后我发现当我使用它时它有效
i was doing the same but for an id based setup and then i found it worked when i used this instead
我想我明白你想要什么。通过某些与添加图像无关的按钮,用户可以隐藏和显示图标。添加了新图像,图标“显示”,并且您不知道回调是否应该显示图标,或隐藏它们,以便它们与图库的其余部分匹配。
i think i understand what you want. from some button, that has nothing to do with adding images, users can hide and show icons. New images are added, with icons 'showing', and you have no idea on callback whether you should be showing the icons, or hiding them so they match the rest of the gallery.
您可以使用另一种更强大且更短的方法。
您可以向元素添加一个属性,例如“open”。
当您第一次阅读时,它不在那里,因此它假设它是初始位置。
如果它是“0”,那么您将其反转,并在条件块中执行slidedown()或slideup()
这有很多好处:
- 您可以通过浏览器调试器读取该值并查看它的变化
- 最有用的是您可以使用 .each 迭代器读取该值并检查打开的值
You use another method which is more robust and shorter.
You can add an attribute,eg 'open' to the element.
First time you read it is not there so it assumes it is the initial position.
If it is '0' then you reverse it and in the condition block you do slidedown() or slideup()
This has many benefits:
- you can read the value by browser debugger and see it changed
- most useful is you can read that value using .each iterator and check for the ones that are open
SlideToggle 不会立即更改可见性。你需要破解这个
希望这个迟了10年的答案对你有用我
从https://forum.jquery.com/topic/state-of-slidetoggle
slideToggle does not change visibility immediately. You need to hack around this
hope this 10 years late answer works for you
I found this from a 7 years old post from https://forum.jquery.com/topic/state-of-slidetoggle