[英]Get JavaScript slideToggle to close active div when opening another one?
Here's a demo of what I have so far: http://www.betafreshmedia.com/nathan/coffee.html 这是到目前为止我所拥有的演示: http : //www.betafreshmedia.com/nathan/coffee.html
I know there are a lot of other questions like this, however, I cannot adapt their syntax to mine as everyone's is pretty different. 我知道还有很多其他这样的问题,但是,由于每个人的语法都大不相同,因此我无法适应他们的语法。 Here's my jQuery: 这是我的jQuery:
$(".trigger").click(function() {
$(this).find('ul.coffeetype').slideToggle();
});
I want a minimal, straight-forward way to continue using the same class for each div and only allow one to be opened at a time. 我想要一种简单的直接方法,继续为每个div使用相同的类,并且一次只能打开一个。 When you click on a div that's closed, I want the open one to slide out of sight and the new one to drop down in its respective position. 当您单击关闭的div时,我希望打开的div滑出视线,而新的div则下降到其各自的位置。
HTML: HTML:
<div class="trigger">
<img src="images/coffeetype1.png" />
<ul class="coffeetype">
<li>International</li>
<li>Regional</li>
<li>International</li>
<li>Regional</li>
<li>International</li>
<li>Regional</li>
<li>International</li>
<li>Regional</li>
<li>International</li>
<li>Regional</li>
<li>International</li>
<li>Regional</li>
</ul>
</div>
<div class="trigger">
<img src="images/coffeetype1.png" />
<ul class="coffeetype">
<li>International</li>
<li>Regional</li>
<li>International</li>
<li>Regional</li>
<li>International</li>
<li>Regional</li>
<li>International</li>
<li>Regional</li>
<li>International</li>
<li>Regional</li>
<li>International</li>
<li>Regional</li>
</ul>
</div>
<div class="trigger">
<img src="images/coffeetype1.png" />
<ul class="coffeetype">
<li>International</li>
<li>Regional</li>
<li>International</li>
<li>Regional</li>
<li>International</li>
<li>Regional</li>
<li>International</li>
<li>Regional</li>
<li>International</li>
<li>Regional</li>
<li>International</li>
<li>Regional</li>
</ul>
</div>
It's just the same thing three times in a row right now for demo purposes. 出于演示目的,现在连续三遍都是同一件事。
$(document).ready(function() {
$(".trigger").click(function() {
var $el = $(this).find('ul.coffeetype');
var $opened = $('.toggledDown').not($el);
$opened.toggleClass('toggledDown');
$opened.slideToggle();
$el.toggleClass('toggledDown');
$el.slideToggle();
});
});
Track the ones that are open and close them before you open the active one. 在打开活动的主机之前,请先跟踪已打开的主机并关闭它们。
EDIT Added the ".not($el)" so you can still toggle the same menu up and down. 编辑添加了“ .not($ el)”,因此您仍然可以上下切换相同的菜单。
Change your code to the following: 将您的代码更改为以下内容:
$(".trigger").click(function() {
$('ul.coffeetype').slideUp();
$(this).find('ul.coffeetype').slideToggle();
});
This slides all open lists closed first. 这将滑动所有先关闭的打开列表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.