2017-04-16 121 views
0

好日子大家,引導漢堡圖標動畫導航欄頭

如何使菜單按鈕漢堡動畫到X在引導navbar-fixed-top

<nav class="navbar navbar-default navbar-fixed-top"> 
      <div class="container"> 
      <div class="navbar-header"> 
       <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar"> 
       <span class="sr-only">Toggle navigation</span> 
       <span class="icon-bar"></span> 
       <span class="icon-bar"></span> 
       <span class="icon-bar"></span> 
       </button> 
       <a class="navbar-brand" href="index.html">Logo</a> 
      </div> 
      <div id="navbar" class="navbar-collapse collapse"> 
       <ul class="nav navbar-nav"> 
       <li><a href="#">Menu1</a></li> 
       <li><a href="#">Menu2</a></li> 
       <li><a href="#">Menu3</a></li> 
       </ul> 

      </div><!--/.nav-collapse --> 

      </div> 
</nav> 

我試圖在所有可能的例子堵塞,但我錯過了點。有任何想法嗎?

非常感謝。

回答

0

首先,你需要關閉按鈕創建額外的標記

<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar"> 
      <span class="sr-only">Toggle navigation</span> 
      <div class="burger> 
      <span class="icon-bar"></span> 
      <span class="icon-bar"></span> 
      <span class="icon-bar"></span> 
      </div> 
      <div class="close"> 
       x 
      </div> 
      </button> 

所以,在那之後,你需要更新你的CSS:

.navbar-toggle .burger{ 
    display: none; 
} 
.navbar-toggle .close{ 
    display: block; 
} 

.navbar-toggle.collapsed .burger{ 
    display: block; 
} 
.navbar-toggle.collapsed .close{ 
    display: none; 
} 

附:你也可以很容易地默認之間添加動畫和.collapsed

JSFiddle例如

1

你問如何設置過渡動畫效果,我覺得jQuery的是爲這個偉大的。

$(".navbar-toggle").click(function() { 
 
    $(".open").toggle(500).siblings().toggle(500); 
 
});
.close { 
 
display:none; 
 
}
<!DOCTYPE html> 
 
    <html lang="en"> 
 
     <head> 
 
     <meta charset="utf-8"> 
 
     <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
 
     <meta name="viewport" content="width=device-width, initial-scale=1"> 
 
     <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags --> 
 
     <meta name="description" content=""> 
 
     <meta name="author" content=""> 
 
     <link rel="icon" href="../../favicon.ico"> 
 

 
     <title>Test</title> 
 

 
     <!-- Latest compiled and minified CSS --> 
 
     <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> 
 

 
     <!-- IE10 viewport hack for Surface/desktop Windows 8 bug --> 
 
     <link href="../../assets/css/ie10-viewport-bug-workaround.css" rel="stylesheet"> 
 

 
     <!-- Custom styles for this template --> 
 
     <link href="style.css" rel="stylesheet"> 
 

 
     <!-- Just for debugging purposes. Don't actually copy these 2 lines! --> 
 
     <!--[if lt IE 9]><script src="../../assets/js/ie8-responsive-file-warning.js"></script><![endif]--> 
 
     <script src="../../assets/js/ie-emulation-modes-warning.js"></script> 
 

 
     <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries --> 
 
     <!--[if lt IE 9]> 
 
      <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script> 
 
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script> 
 
     <![endif]--> 
 
     </head> 
 

 
     <body> 
 

 
     <!-- Fixed navbar --> 
 
      
 
      
 
     <nav class="navbar navbar-default navbar-fixed-top"> 
 
      <div class="container"> 
 
      <div class="navbar-header"> 
 
       <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar"> 
 
       <div class="open"> 
 
        <span class="sr-only">Toggle navigation</span> 
 
        <span class="icon-bar"></span> 
 
        <span class="icon-bar"></span> 
 
        <span class="icon-bar"></span> 
 
       </div> 
 
       <div class="close"> 
 
        X 
 
       </div> 
 
       </button> 
 
       <a class="navbar-brand" href="index.html">Logo</a> 
 
      </div> 
 
      <div id="navbar" class="navbar-collapse collapse"> 
 
       <ul class="nav navbar-nav"> 
 
       <li><a href="#">Menu1</a></li> 
 
       <li><a href="#">Menu2</a></li> 
 
       <li><a href="#">Menu3</a></li> 
 
       </ul> 
 
      </div><!--/.nav-collapse --> 
 
       
 
      </div> 
 
     </nav> 
 

 
     
 

 

 
     <!-- Bootstrap core JavaScript 
 
     ================================================== --> 
 
     <!-- Placed at the end of the document so the pages load faster --> 
 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> 
 
     <script>window.jQuery || document.write('<script src="../../assets/js/vendor/jquery.min.js"><\/script>')</script> 
 
     <script src="main.js"></script> 
 
     <!-- Latest compiled and minified JavaScript --> 
 
     <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script> 
 
     <!-- IE10 viewport hack for Surface/desktop Windows 8 bug --> 
 
     <script src="../../assets/js/ie10-viewport-bug-workaround.js"></script> 
 
     </body> 
 
    </html>

+0

據工作@nfm!多謝!是否有可能使用類似於這些[示例](https://codepen.io/designcouch/pen/Atyop)的jQuery獲取動畫? –