2017-02-14 63 views
0

我正在設計一個單頁滾動網站。當我在屏幕寬度小於780px(當出現漢堡包圖標時)點擊導航欄中的頁面鏈接時,漢堡包圖標消失。除非刷新頁面,否則我無法恢復。點擊頁面鏈接一次後,導航欄也會以全屏寬度消失。我希望始終保持漢堡包圖標和頂部導航。我用來摺疊顯示在780px的全屏菜單的JavaScript導致了這個問題,但我需要它,或者當單擊鏈接時菜單不會消失。感謝任何能夠幫助的人!單擊頁面鏈接時導航/漢堡包圖標消失

$(document).ready(function() { 
 
    $('a').click(function() { 
 
     $('#menu').slideToggle(); 
 
    }); 
 
});
@media screen and (max-width: 780px) { 
 

 
    nav { 
 
     width: 100%; 
 
     margin: 0; 
 
     padding: 0; 
 
     position: relative; 
 
     left: 0; 
 
     display: block; 
 
     opacity: 1.0 !important; 
 
     filter: alpha(opacity=100); /* For IE8 and earlier */ 
 
    } 
 

 
    nav ul { 
 
     width: 100%; 
 
     margin: 0 auto; 
 
     padding: 0; 
 
     display: none; 
 
     float: none; 
 
    } 
 

 
    nav ul li { 
 
     font-size: 1.3em; 
 
     font-weight: normal; 
 
     line-height: 40px; 
 
     width: 100% !important; 
 
     margin: 0; 
 
     padding: 0; 
 
    } 
 

 
    nav ul li:nth-of-type(1) { margin-top: 20%; } 
 

 
    nav ul li:hover { background: #565758; } 
 
    
 
    nav ul li a { 
 
     color: white !important; 
 
     font-family: "Lato", sans-serif; 
 
     border-bottom: none !important; 
 
     display: inline-block; 
 
    } 
 

 
    nav ul li a.active-link { 
 
     color: white !important; 
 
     font-size: 1.3em; 
 
    } 
 

 
    nav ul li a:hover { 
 
     color: white; 
 
     width: 100%; 
 
    } 
 
     
 
    /*Display 'show menu' link*/ 
 
    .show-menu { 
 
     margin: 0 !important; 
 
     padding: 1em !important; 
 
     text-align: right; 
 
     display: block; 
 
     float: right; 
 
    } 
 

 
    /*Show menu when invisible checkbox is checked*/ 
 
    input[type=checkbox]:checked ~ #menu { background-color: #747475 !important; display: block; height: 100vh; } 
 
}​
<header> 
 
    <nav> 
 
     <label for="show-menu" class="show-menu"><img src="hamburger.png" alt="Hamburger Menu Icon" style="width: 15%;"></label> 
 
     <input type="checkbox" id="show-menu" role="button"> 
 
     <ul id="menu"> 
 
      <li><a href="#choco">HOME</a> 
 
      <li><a href="#about-page">ABOUT</a></li> 
 
      <li><a href="#portfolio-page">PORTFOLIO</a></li> 
 
      <li><a href="#contact.html">CONTACT</a></li> 
 
     </ul> 
 
    </nav> 
 
    <div id="logo"><img src="logo-grey.png" alt="Logo" style="max-width:100%; height:auto;"></div> 
 
</header>​

回答

0

你需要切換添加到複選框爲好。它是一個jQuery函數,它使用特定的動畫和樣式。

$('#show-menu').click(function() { 
    $('#menu').slideToggle(); 
}); 

EDIT


我添加了一個工作示例。我沒有在這裏使用切換,以獲得更好的設計。現在的菜單也與複選框點擊toggels :-)

$(document).ready(function() { 
 
    $('a').click(function() { 
 
     $('#menu').slideToggle(); 
 
    }); 
 
    $('#show-menu').change(function() { 
 
     if(this.checked) 
 
     $('#menu').slideDown(); 
 
     else 
 
     $('#menu').slideUp(); 
 
    }); 
 
});
@media screen and (max-width: 780px) { 
 

 
    nav { 
 
     width: 100%; 
 
     margin: 0; 
 
     padding: 0; 
 
     position: relative; 
 
     left: 0; 
 
     display: block; 
 
     opacity: 1.0 !important; 
 
     filter: alpha(opacity=100); /* For IE8 and earlier */ 
 
    } 
 

 
    nav ul { 
 
     width: 100%; 
 
     margin: 0 auto; 
 
     padding: 0; 
 
     display: none; 
 
     float: none; 
 
    } 
 

 
    nav ul li { 
 
     font-size: 1.3em; 
 
     font-weight: normal; 
 
     line-height: 40px; 
 
     width: 100% !important; 
 
     margin: 0; 
 
     padding: 0; 
 
    } 
 

 
    nav ul li:nth-of-type(1) { margin-top: 20%; } 
 

 
    nav ul li:hover { background: #565758; } 
 
    
 
    nav ul li a { 
 
     color: white !important; 
 
     font-family: "Lato", sans-serif; 
 
     border-bottom: none !important; 
 
     display: inline-block; 
 
    } 
 

 
    nav ul li a.active-link { 
 
     color: white !important; 
 
     font-size: 1.3em; 
 
    } 
 

 
    nav ul li a:hover { 
 
     color: white; 
 
     width: 100%; 
 
    } 
 
     
 
    /*Display 'show menu' link*/ 
 
    .show-menu { 
 
     margin: 0 !important; 
 
     padding: 1em !important; 
 
     text-align: right; 
 
     display: block; 
 
     float: right; 
 
    } 
 

 
    /*Show menu when invisible checkbox is checked*/ 
 
    input[type=checkbox]:checked ~ #menu { background-color: #747475 !important; display: block; height: 100vh; } 
 
}​
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<header> 
 
    <nav> 
 
     <label for="show-menu" class="show-menu"><img src="hamburger.png" alt="Hamburger Menu Icon" style="width: 15%;"></label> 
 
     <input type="checkbox" id="show-menu" role="button"> 
 
     <ul id="menu"> 
 
      <li><a href="#choco">HOME</a> 
 
      <li><a href="#about-page">ABOUT</a></li> 
 
      <li><a href="#portfolio-page">PORTFOLIO</a></li> 
 
      <li><a href="#contact.html">CONTACT</a></li> 
 
     </ul> 
 
    </nav> 
 
    <div id="logo"><img src="logo-grey.png" alt="Logo" style="max-width:100%; height:auto;"></div> 
 
</header>​

+0

謝謝您的解答。 DomeTune,我想要這個工作,但事實並非如此。我可能會把它設置錯誤。我用#show-menu(整個代碼片段)在我的當前javascript行下面加上了「a」,並在下一行加上了「#menu」。我在下面添加了你的行,並把所有的「});」在他們自己的路線下。不幸的是沒有什麼改變 – Connie

+0

@Connie我添加了一個工作示例。希望它有幫助,如果不問我。 – DomeTune

+0

謝謝,DomeTune!除了兩件事之外,這很有效:爲了讓菜單擴展,我必須點擊漢堡包圖標兩次。此外,當頁面爲全角時,我必須刷新頁面才能顯示頂部導航。但這是迄今爲止最接近完美的! – Connie