2017-01-23 60 views
0

當我點擊一個按鈕時,我製作了一個菜單,其中的滑動條向下打開。然而,我的菜單的內容只顯示了50%的時間......我真的不知道如何解決這個問題。 因此,基本上你點擊打開的菜單,它顯示正常,但當我關閉並重新打開它不顯示我的內容...當點擊打開菜單時,菜單內容只顯示50%的時間

我使用html,css和jquery獲得此效果。

我做了一個codepen:Codepen

$(document).ready(function() { 
 

 
    var clicks = 0; 
 
    $('.menubutton-tablet').click(function() { 
 

 
    if (clicks % 2 == 0) { 
 

 
     $('.menu-collapse-tablet').animate({ 
 
     'height': '375px' 
 
     }, 500); 
 
     $('.navbar-ul-tablet').toggleClass('slideopen'); 
 
    } else { 
 

 
     $('.menu-collapse-tablet').animate({ 
 
     'height': '0px' 
 
     }, 500) 
 
     $('.navbar-ul-tablet').toggleClass('menutoggle'); 
 
    } 
 
    ++clicks 
 

 

 
    }); 
 
});
nav { 
 
    width: 100%; 
 
    display: -webkit-flex; 
 
    display: flex; 
 
    -webkit-flex-direction: column; 
 
    flex-direction: column; 
 
    background: black; 
 
    background-position: center; 
 
    display: flex; 
 
} 
 
.navbar-styles { 
 
    border-radius: 0px; 
 
    border-style: none; 
 
    border-color: none; 
 
    min-height: 100px; 
 
    margin-bottom: 0; 
 
    background-color: transparent; 
 
    width: 100%; 
 
    display: flex; 
 
    justify-content: space-between; 
 
} 
 
.headerlinks { 
 
    position: relative; 
 
    text-decoration: none; 
 
    list-style-type: none; 
 
    color: white; 
 
} 
 
.headerlogo { 
 
    width: 225px; 
 
    margin-left: 10%; 
 
    float: left; 
 
    position: relative; 
 
    display: inline; 
 
} 
 
.menubutton-tablet { 
 
    width: 100px; 
 
    height: 100px; 
 
    margin-top: 1%; 
 
    cursor: pointer; 
 
    color: white; 
 
} 
 
.menubutton-tablet:hover { 
 
    opacity: 0.8; 
 
} 
 
.menu-collapse-tablet { 
 
    display: flex; 
 
    width: 100%; 
 
    height: 0px; 
 
    justify-content: center; 
 
    background: transparent; 
 
} 
 
.navbar-ul-tablet { 
 
    display: none; 
 
    -webkit-align-items: center; 
 
    align-items: center; 
 
    -webkit-justify-content: center; 
 
    justify-content: center; 
 
    -webkit-flex-direction: column; 
 
    flex-direction: column; 
 
} 
 
.slideopen { 
 
    display: flex; 
 
} 
 
.menutoggle { 
 
    display: none; 
 
} 
 
.navbar-ul-tablet li { 
 
    text-decoration: none; 
 
    list-style-type: none; 
 
    font-size: 18px; 
 
    padding-top: 50px; 
 
    text-shadow: 3px 2px 4px rgba(0, 0, 0, 0.50); 
 
} 
 
.navbar-ul-tablet li:first-child { 
 
    padding-top: 10px; 
 
} 
 
.navbar-ul-tablet li:last-child { 
 
    padding-bottom: 20px; 
 
} 
 
.headerlinks { 
 
    color: white; 
 
    font-weight: 100; 
 
} 
 
.headerlinks:hover { 
 
    color: #d0021b; 
 
} 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script> 
 
<nav> 
 
    <div class="navbar-styles"> 
 

 
    <a href="index.php"> 
 
     <img class="headerlogo" src="images/mobilelogo2.png" /> 
 
    </a> 
 
    <p class="menubutton-tablet">Open Menu</p> 
 

 
    </div> 
 

 
    <div class="menu-collapse-tablet"> 
 
    <ul class="navbar-ul-tablet"> 
 
     <li><a class="headerlinks" href="assortiment.php">ASSORTIMENT</a> 
 
     </li> 
 
     <li><a class="headerlinks" href="overons.php">OVER ONS</a> 
 
     </li> 
 
     <li><a class="headerlinks" href="webshop.php">WEBSHOP</a> 
 
     </li> 
 
     <li><a class="headerlinks" href="#">LOGIN</a> 
 
     </li> 
 
     <li><a class="headerlinks" href="contact.php">CONTACT</a> 
 
     </li> 
 

 

 
    </ul> 
 
    </div> 
 
</nav>

回答

2

你需要改變你的代碼:

$('.menu-collapse-tablet').animate({'height': '0px'}, 500) 
$('.navbar-ul-tablet').toggleClass('menutoggle'); 

這一個:

$('.menu-collapse-tablet').animate({'height': '0px'}, 500) 
$('.navbar-ul-tablet').toggleClass('slideopen'); 
+0

Thx,像一個魅力一樣工作猜測它沒有必要隱藏容器內的內容... – Pieter

0

你有因爲低於行代碼的這個問題。

if(clicks % 2 == 0){ 
$('.menu-collapse-tablet').animate({'height': '375px'}, 500); 
$('.navbar-ul-tablet').toggleClass('slideopen'); 
} 

你跟蹤的點擊而當點擊甚至你是顯示菜單否則你設置height:0px

+0

我怎樣才能讓這個它只是切換?我以前試過toggleClass,但後來我沒有設法給我的下拉做動畫... – Pieter

+0

感謝@Mukesh Ram讓它變得很容易:) –

相關問題