JQuery 手风琴:恢复为“封闭”之前的样式;状态

发布于 2024-10-28 02:02:43 字数 3176 浏览 4 评论 0原文

我正在使用一个简单的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

请叫√我孤独 2024-11-04 02:02:43

尝试使用 .toggleClass()

    $(".menu_active").not(this).removeClass('menu_active');
    $(this).toggleClass('menu_active');

更新了jsfiddle

Try using .toggleClass()

    $(".menu_active").not(this).removeClass('menu_active');
    $(this).toggleClass('menu_active');

Updated jsfiddle.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文