Jquery多个div切换

时间:2015-11-09 06:06:13

标签: javascript jquery

行。我不会花很长时间才能知道我在这里学习jQuery,并且可能以最笨拙的方式解决这个问题。这就是我在这里的原因。

我一直在创建一个基于“面板”的菜单系统,它提供了许多不同的功能(菜单,过滤器,搜索,购物篮和帐户)。我有99%在我想要的地方。实际上,如果单击菜单图标(作为示例),您将看到确切的效果。再次点击它,一切都很完美。

我的问题出现在用户点击其初始“面板”打开的另一个图标时。现在你可以看到我所知的差距。

请注意效果是面板的不同div和每次(主)的同一div。当然,最好是: a)当单击一个新图标而不关闭面板时,jQuery关闭前一个面板,移除close-btn,向后滑动主面板,然后打开触发新面板。 要么 b)关闭前一个面板,移除close-btn但保持主要打开(我认为这已经过时了)。

HTML

<div id="mainpanel">
<div class="menunav">
    <div class="toggle menu-btn"></div>
    <div class="toggle filter-btn"></div>
    <div class="toggle search-btn"></div>
    <div class="toggle basket-btn"></div>
    <div class="toggle account-btn"></div>
</div>
</div>
<div class="togglepanel mainmenu">
menu here
</div>
<div class="togglepanel filter">
filter here
</div>
<div class="togglepanel search">
search here
</div>
<div class="togglepanel basket">
basket here
</div>
<div class="togglepanel account">
account here
</div>
<main>
content will be here
</main>

CSS

#mainpanel {
position: fixed;
display: table;
top: 0;
left: 0;
width: 125px;
height: 100%;
background: #206ba4;
vertical-align: middle;
z-index: 9999;}

main {
position: relative;
top: 0;
margin-left: 125px;
transform: translateX(0);
transform: translateY(0);
transition: transform .5s;}

.move {
transform: translateX(300px) !important;}

.menunav {
display: table-cell;
color: #fff;
z-index: 1001;
margin: 20px 0 0;
text-align: center;
vertical-align: middle;}

.menunav div {
display: block;
width: 100%;
margin: 0 0 30px;
text-align: center;}

.menu-btn:after, .filter-btn:after, .search-btn:after, .basket-btn:after, .account-btn:after, .close-btn:after {
font-family: FontAwesome;
content: "menu";
font-size: 1.8em;
font-weight: 200;
color: #fff;
display: block;
height: 1em;
width: 1em;
margin: 0 0 0 30%;
cursor: pointer;}

.filter-btn:after {
content: "filter";}

.search-btn:after {
content: "search";}

.basket-btn:after {
content: "basket";}

.account-btn:after {
content: "account";}

.close-btn:after {
content: "close";}

.mainmenu, .filter, .search, .basket, .account {
position: fixed;
width: 300px;
top: 0;
left: 125px;
height: 100%;
background: #54a4de;
transform: translateX(-100%);
transition: transform .5s;
z-index: -1;}

.expand {
transform: translateX(0px);}

的jQuery

jQuery(function($){
    $('.menu-btn').click(function(){
    $('.mainmenu').toggleClass('expand')
    $('main').toggleClass('move')
    $('.menu-btn').toggleClass('close-btn')
     })
})
jQuery(function($){
    $( '.filter-btn' ).click(function(){
    $('.filter').toggleClass('expand')
    $('main').toggleClass('move')
    $('.filter-btn').toggleClass('close-btn')
     })
})
jQuery(function($){
    $( '.search-btn' ).click(function(){
    $('.search').toggleClass('expand')
    $('main').toggleClass('move')
    $('.search-btn').toggleClass('close-btn')
 })
})  
jQuery(function($){
    $( '.basket-btn' ).click(function(){
    $('.basket').toggleClass('expand')
    $('main').toggleClass('move')
    $('.basket-btn').toggleClass('close-btn')
     })
})  
jQuery(function($){
    $( '.account-btn' ).click(function(){
    $('.account').toggleClass('expand')
    $('main').toggleClass('move')
    $('.account-btn').toggleClass('close-btn')
     })
})

Here is the jsfiddle

非常感谢,提前,任何指示....我的头疼!

2 个答案:

答案 0 :(得分:4)

<强> DEMO HERE

你的尝试中有许多冗余的code,但仍然是实现这一目标的一个很好的尝试。以下是我的建议。

<强>提示:

  
      
  • 不要包含多个jQuery(function,因为这相当于$(document).ready功能,如果你只有一个,那就很好   js档案
  •   
  • 将一个公共事件写入所有按钮,并针对发生的每次点击基于$(this)进行区分

  •   
  • 在您的.toggle元素中添加额外的data- *属性,例如data-target,以定位其对应的panel-body

  •   

以下是我对您的代码所做的一些更改..

<强> HTML

<div class="menunav">
    <!--data-target to each of its corresponding body class-->
    <div class="toggle menu-btn" data-target=".mainmenu"></div>
    <div class="toggle filter-btn" data-target=".filter"></div>
    <div class="toggle search-btn" data-target=".search"></div>
    <div class="toggle basket-btn" data-target=".basket"></div>
    <div class="toggle account-btn" data-target=".account"></div>
</div>

<强> JS

jQuery(function($){
    //click event to one common class i.e toggle
    $('.toggle').click(function(){
        var target=$(this).data('target'); //get the clicked element's target property
        $('.togglepanel').not($(target)).removeClass('expand');
        //remove class from all the togglepanel elements except the current element's target
        $('.toggle').removeClass('close-btn');
        //general action in any scenario to remove close-btn
        if($(target).hasClass('expand'))
        {
            //if target has expand class then remove it and do necessary changes
            $(target).removeClass('expand')
            $('main').removeClass('move')
            $(this).removeClass('close-btn')
        }
        else
        {
            //else add necessary classes
            $(target).addClass('expand')
            $('main').addClass('move')
            $(this).addClass('close-btn')
        }
     })
})

答案 1 :(得分:3)

jQuery(function($){
        $(document).on('click', '.toggle', function (){
            
            // to close all open contents
            $('.togglepanel').removeClass('expand');
            $('main').removeClass('move');
            
           var target = $(this).data("target");
            
            if( $(this).hasClass('close-btn') ){
              $('.toggle').toggleClass('close-btn', false);
               $('main').toggleClass('move', false)
               $(target).toggleClass('expand', false);
            }else{
              $('.toggle').toggleClass('close-btn', false);
              $(this).toggleClass('close-btn', true);
              $('main').toggleClass('move', true)
               $(target).toggleClass('expand', true);
            }
            
        });
    
	})
#mainpanel {
	position: fixed;
	display: table;
	top: 0;
	left: 0;
	width: 125px;
	height: 100%;
	background: #206ba4;
	vertical-align: middle;
	z-index: 9999;
}

main {
    position: relative;
	top: 0;
	margin-left: 125px;
	transform: translateX(0);
	transform: translateY(0);
	transition: transform .5s;
}

.move {
	transform: translateX(300px) !important;
}

.menunav {
	display: table-cell;
	color: #fff;
	z-index: 1001;
	margin: 20px 0 0;
	text-align: center;
	vertical-align: middle;
}

.menunav div {
	display: block;
	width: 100%;
	margin: 0 0 30px;
	text-align: center;
}

.menu-btn:after, .filter-btn:after, .search-btn:after, .basket-btn:after, .account-btn:after, .close-btn:after {
	font-family: FontAwesome;
	content: "menu";
	font-size: 1.8em;
	font-weight: 200;
	color: #fff;
	display: block;
	height: 1em;
	width: 1em;
	margin: 0 0 0 30%;
	cursor: pointer;
}

.filter-btn:after {
	content: "filter";
}

.search-btn:after {
	content: "search";
}

.basket-btn:after {
	content: "basket";
}

.account-btn:after {
	content: "account";
}

.close-btn:after {
    content: "close";
}
}

.close-btn:after {
	content: "\f00d";
}

.mainmenu, .filter, .search, .basket, .account {
	position: fixed;
	width: 300px;
	top: 0;
	left: 125px;
	height: 100%;
	background: #54a4de;
	transform: translateX(-100%);
	transition: transform .5s;
	z-index: -1;
}

.expand {
	transform: translateX(0px);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="mainpanel">
    <div class="menunav">
        <div class="toggle menu-btn" data-target=".mainmenu"></div>
        <div class="toggle filter-btn" data-target=".filter"></div>
        <div class="toggle search-btn" data-target=".search"></div>
        <div class="toggle basket-btn" data-target=".basket"></div>
        <div class="toggle account-btn" data-target=".account"></div>
    </div>
</div>
<div class="togglepanel mainmenu">
    menu here
</div>
<div class="togglepanel filter">
    filter here
</div>
<div class="togglepanel search">
    search here
</div>
<div class="togglepanel basket">
    basket here
</div>
<div class="togglepanel account">
    account here
</div>
<main>
    content will be here
</main>

您实际上在做很多可以转换为单行的代码。 不要在每个按钮上添加click事件,请尝试以下方法:

首先,在按钮中添加data-target属性,例如:

<div class="toggle menu-btn" data-target=".mainmenu"></div>
<div class="toggle filter-btn" data-target=".filter"></div>
<div class="toggle search-btn" data-target=".search"></div>
<div class="toggle basket-btn" data-target=".basket"></div>
<div class="toggle account-btn" data-target=".account"></div>

关于你的jQuery:

jQuery(function($){
    $(document).on('click', '.toggle', function (){

        // to close all open contents
        $('.togglepanel').removeClass('expand');
        $('main').removeClass('move');

       var target = $(this).data("target");

        if( $(this).hasClass('close-btn') ){
           $('.toggle').toggleClass('close-btn', false);
           $('main').toggleClass('move', false)
           $(target).toggleClass('expand', false);
        }else{
           $('.toggle').toggleClass('close-btn', false);
           $(this).toggleClass('close-btn', true);
           $('main').toggleClass('move', true)
           $(target).toggleClass('expand', true);
        }

    });
});