JQuery 手风琴:恢复为“封闭”之前的样式;状态
我正在使用一个简单的 JQuery 手风琴,它使用 DIV 而不是典型的 UL 结构。
- 当您单击菜单 DIV 时,将打开特定的相应面板 DIV。
- 菜单 DIV 的样式发生变化(变为灰色)以表明它处于活动状态。
- 当您再次单击它时,面板 DIV 将关闭。
- 发生这种情况时,我希望菜单 DIV 恢复到以前的样式。
- 目前它只是保持灰色。
有人可以帮忙吗?
这是 JSFiddle 上的代码: http://jsfiddle.net/Gaelen/aTR2b/6/
JavaScript:
$(document).ready(function() {
$("#menu1").data("panelId", "#collapse_photography");
$("#menu2").data("panelId", "#collapse_fineart");
$("#menu3").data("panelId", "#collapse_architecture");
$("#menu4").data("panelId", "#collapse_graphdesign");
$("#menu5").data("panelId", "#collapse_infographics");
$("#menu6").data("panelId", "#collapse_3dviz");
$("#menu1, #menu2, #menu3, #menu4, #menu5, #menu6").click(function() {
$(".menu_active").removeClass('menu_active');
$(this).addClass('menu_active');
var first = true;
var panelId = $(this).data("panelId");
$(".class1").not(panelId).slideUp(function() {
if (first) {
first = false;
$(panelId).slideToggle(400);
}
});
});
});
CSS:
.class1 {width:200px; height:200px; border:1px solid black; display:none;}
.menu_active{ background: grey;}
#collapse_photography {background:red; margin-bottom:10px;}
#collapse_fineart {background:red; margin-bottom:10px;}
#collapse_architecture {background:red; margin-bottom:10px;}
#collapse_graphdesign {background:red; margin-bottom:10px;}
#collapse_infographics {background:red; margin-bottom:10px;}
#collapse_3dviz {background:red; margin-bottom:10px;}
#menu1 {cursor:pointer; display:block; width:200px; border:1px solid black; margin-bottom:10px;}
#menu2 {cursor:pointer; display:block; width:200px; border:1px solid black; margin-bottom:10px;}
#menu3 {cursor:pointer; display:block; width:200px; border:1px solid black; margin-bottom:10px;}
#menu4 {cursor:pointer; display:block; width:200px; border:1px solid black; margin-bottom:10px;}
#menu5 {cursor:pointer; display:block; width:200px; border:1px solid black; margin-bottom:10px;}
#menu6 {cursor:pointer; display:block; width:200px; border:1px solid black; margin-bottom:10px;}
HTML:
<div id="menu1"><a href="#">Photography</a></div>
<div id="collapse_photography" class="class1">Content 1</div>
<div id="menu2">Fine Art</div>
<div id="collapse_fineart" class="class1">Content 2</div>
<div id="menu3">Architecture</div>
<div id="collapse_architecture" class="class1">Content 3</div>
<div id="menu4">Graphic + Web Design</div>
<div id="collapse_graphdesign" class="class1">Content 4</div>
<div id="menu5">Information Graphics</div>
<div id="collapse_infographics" class="class1">Content 5</div>
<div id="menu6">3D Visualisation</div>
<div id="collapse_3dviz" class="class1">Content 6</div>
I'm using a simple JQuery accordion that uses DIV's rather than the typical UL structure.
- When you click on a menu DIV, a specific corresponding panel DIV slides open.
- The menu DIV's style changes (goes grey) to show that it's active.
- When you click on it again, the panel DIV closes.
- When this happens I want the menu DIV to revert back to it's previous style.
- At the moment it just stays grey.
Can anyone help?
Here is the code on JSFiddle: http://jsfiddle.net/Gaelen/aTR2b/6/
JavaScript:
$(document).ready(function() {
$("#menu1").data("panelId", "#collapse_photography");
$("#menu2").data("panelId", "#collapse_fineart");
$("#menu3").data("panelId", "#collapse_architecture");
$("#menu4").data("panelId", "#collapse_graphdesign");
$("#menu5").data("panelId", "#collapse_infographics");
$("#menu6").data("panelId", "#collapse_3dviz");
$("#menu1, #menu2, #menu3, #menu4, #menu5, #menu6").click(function() {
$(".menu_active").removeClass('menu_active');
$(this).addClass('menu_active');
var first = true;
var panelId = $(this).data("panelId");
$(".class1").not(panelId).slideUp(function() {
if (first) {
first = false;
$(panelId).slideToggle(400);
}
});
});
});
CSS:
.class1 {width:200px; height:200px; border:1px solid black; display:none;}
.menu_active{ background: grey;}
#collapse_photography {background:red; margin-bottom:10px;}
#collapse_fineart {background:red; margin-bottom:10px;}
#collapse_architecture {background:red; margin-bottom:10px;}
#collapse_graphdesign {background:red; margin-bottom:10px;}
#collapse_infographics {background:red; margin-bottom:10px;}
#collapse_3dviz {background:red; margin-bottom:10px;}
#menu1 {cursor:pointer; display:block; width:200px; border:1px solid black; margin-bottom:10px;}
#menu2 {cursor:pointer; display:block; width:200px; border:1px solid black; margin-bottom:10px;}
#menu3 {cursor:pointer; display:block; width:200px; border:1px solid black; margin-bottom:10px;}
#menu4 {cursor:pointer; display:block; width:200px; border:1px solid black; margin-bottom:10px;}
#menu5 {cursor:pointer; display:block; width:200px; border:1px solid black; margin-bottom:10px;}
#menu6 {cursor:pointer; display:block; width:200px; border:1px solid black; margin-bottom:10px;}
HTML:
<div id="menu1"><a href="#">Photography</a></div>
<div id="collapse_photography" class="class1">Content 1</div>
<div id="menu2">Fine Art</div>
<div id="collapse_fineart" class="class1">Content 2</div>
<div id="menu3">Architecture</div>
<div id="collapse_architecture" class="class1">Content 3</div>
<div id="menu4">Graphic + Web Design</div>
<div id="collapse_graphdesign" class="class1">Content 4</div>
<div id="menu5">Information Graphics</div>
<div id="collapse_infographics" class="class1">Content 5</div>
<div id="menu6">3D Visualisation</div>
<div id="collapse_3dviz" class="class1">Content 6</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用
.toggleClass()
更新了jsfiddle。
Try using
.toggleClass()
Updated jsfiddle.