展开/折叠jQuery菜单 - 选择器

时间:2015-07-22 22:54:40

标签: jquery menu selector collapse expand

我正在尝试构建扩展/折叠菜单。

我有一个主导航栏,里面有3个子菜单。默认情况下,子菜单的高度为50px - 但一旦点击,此高度将变为200px。再次单击它以使其折叠回原始的50px。

让我烦恼的是当我扩展subMenu1时,我继续扩展subMenu2 - subMenu1保持扩展,我希望它在第二个子菜单被选中时崩溃。

这是我的代码;

<!DOCTYPE html>
<html>
<head>
    <title>projectFive</title>
    <link rel="stylesheet" href="css/style.css" type="text/css">
</head>
<body>




    </div>
</div>
<div class="sideMenu">

    <div class="sideButton">&#9776;</div>
    <div class="profilePic"></div>

    <div class="menuButtonHolder">


        <div class="menuButton">
            <a class="menuButtonIcon" href="#"><img src="./img/rightArrow.png" id="arrow" alt=""><span>link</span></a>
            <div class="subMenu"></div>
        </div><!--  ending toggle button -->


    <div class="menuButton">
            <a class="menuButtonIcon" href="#"><img src="./img/rightArrow.png" id="arrow" alt=""><span>link</span></a>
            <div class="subMenu"></div>
        </div><!--  ending toggle button -->


    <div class="menuButton">
            <a class="menuButtonIcon" href="#"><img src="./img/rightArrow.png" id="arrow" alt=""><span>link</span></a>
            <div class="subMenu"></div>
        </div><!--  ending toggle button -->


    </div> <!-- ending menu button holder -->

</div>
<!-- ending side menu
 -->


<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js" type="text/javascript"></script>
<script src="my.js" type="text/javascript"></script>
</body>
</html>

和jQuery:

$(document).ready(function(){


    var $menuButton = $(".sideButton");
    var $menuBar = $(".sideMenu");
    var $profilePic = $(".profilePic");

    $menuButton.click(function(){
        if ($(this).hasClass("active")){

            $profilePic.fadeOut(200);
            $profilePic.css({"width": "25px", "height": "25px", "transition": "width 0.3s linear, height 0.3s linear"}, 500);

            $(".menuButtonHolder").css({
                "top": "1000px", 
                "transition": "top .4s ease-out"});

            $menuBar.animate({width: "60px"}, 500);
            $menuBar.css({"background-color": "rgba(0,0,0,0.9)"});
            $menuButton.removeClass("active");



        } else {

            $profilePic.fadeIn(500);
            $profilePic.css({"width": "150px", "height": "150px", "transition": "width 0.7s linear, height 0.7s linear"}, 500);

            $(".menuButtonHolder").css({
                "top": "0px", 
                "transition": "top .8s ease-out"});

            $menuBar.animate({width: "300px"}, 800);
            $menuBar.css({"background-color": "rgba(0,0,0,0.5)", "transition": "background-color .5s linear"});
            $menuButton.addClass("active");
        }


});


$(".menuButtonIcon").click(function(){


        if ($(this).hasClass("filter")){

            $(this).find("#arrow").css({"-webkit-transform": "rotate(90deg)", "transition": "transform 0.5s ease-out"});
            $(this).css({"height": "50px", "transition": "height 0.5s ease-out"});
            $(this).removeClass("filter");





        } else {
            $(this).find("#arrow").css({"-webkit-transform": "rotate(180deg)", "transition": "transform 0.5s ease-out"});
            $(this).css({"height": "200px", "transition": "height 0.5s ease-out"});
            $(".menuButtonIcon").addClass("filter");
        }


});


});

我想知道什么功能或用什么来确保一旦点击任何子菜单 - 如果有一个子菜单被扩展,兄弟姐妹会崩溃。

我希望我的代码有足够的信息给你们一个想法。

提前致谢!

1 个答案:

答案 0 :(得分:1)

尝试将子菜单折叠逻辑移动到单独的函数中,并使用click事件函数中的相关元素调用它,如下所示:

function collapse($elements) {
  $elements.removeClass("filter");
  $elements.find("#arrow").css({"-webkit-transform": "rotate(90deg)", "transition": "transform 0.5s ease-out"});
  $elements.css({"height": "50px", "transition": "height 0.5s ease-out"});
}

$(".menuButtonIcon").click(function(){
  if ($(this).hasClass("filter")){
    collapse($(this));
  } else {
    $(this).find("#arrow").css({"-webkit-transform": "rotate(180deg)", "transition": "transform 0.5s ease-out"});
    $(this).css({"height": "200px", "transition": "height 0.5s ease-out"});
    $(this).addClass("filter");
    collapse($(".menuButtonIcon").not($(this)));
  }
});

如果它具有“过滤器”类,则会折叠菜单项,并且如果不包含单击的菜单项,则会折叠所有 其他项目。

以下是其实际应用的粗略示例:http://codepen.io/nrbernard/pen/doqwLr