2016-12-09 2 views
2

저는 아코디언 타입 메뉴를위한 간단한 슬라이드 토글을했습니다.jQuery 슬라이드 토글, 아코디언 유형 어디서든 닫으시겠습니까?

li는 그 li 내에있는 .sub-menu 열린 미끄러 클릭 .menu-item-has-children 클래스가있는 경우 : menu-item-has-children와 다른 클래스가 클릭하면, 그것은 하나의 I를 닫 것을

$(".menu-item-has-children").unbind('click').click(function(){  
     $(".menu-item-has-children > a").toggleClass("sub-open"); 
     $(this).children(".sub-menu").slideToggle(); 
    }); 

가 어떻게 그것을해야합니까를 이전에 열어 본 것은 내가 방금 클릭 한 것을 엽니 다.

기본적으로 아코디언은 새 아코디언을 열기 전에 이미 닫혀 있거나 열렸던 아코디언을 닫습니다. 그러나 이들은 예를 들어 같은 목록이 아니라 페이지 내의 다른 곳에 나타날 수 있습니다.

+0

지금까지 해보신 것은 무엇입니까? 귀하의 작업 바이올린을 추가 할 수 있습니다 – ScanQR

답변

2

다른 menu-item 요소의 slideUp() 하위 메뉴를 사용할 수 있습니다.

$(".menu-item-has-children").unbind('click').click(function(){ 
    //Find other menu items 
    var otherMenuItems = $(".menu-item-has-children").not($(this)); 

    //Find children and perform Slideup sub-menus and remove sub-open class 
    otherMenuItems.children(".sub-menu").slideUp(); 
    otherMenuItems.children("a").removeClass("sub-open"); 

    $(this).children("a").toggleClass("sub-open"); 
    $(this).children(".sub-menu").slideToggle(); 
}); 
+0

브릴리언트, 여기 테스트 .. 감사합니다 https://jsfiddle.net/ps70fo4c/ 그들은 모두 시작 열어, 또는 CSS를 닫은 그들을 숨기려면 어떻게해야합니까? – user4630

+0

@ user4630, https://jsfiddle.net/ps70fo4c/1/ 참조하십시오 – Satpal

+0

도움 주셔서 감사합니다 :) – user4630

관련 문제