我想通過CSS爲我的下拉菜單設置動畫,以顯示淡入和淡出動畫。我使用bootstrap,它管理我的導航欄,並在移動設備上淡入和淡出。通過CSS,我可以爲導航欄內的下拉菜單設置淡入淡出動畫效果。但是,當摺疊這些菜單時,根本沒有動畫。 我還做了此行爲的視頻: https://youtu.be/YcWzsH5aKbwBootstrap CSS下拉菜單動畫
的jsfiddle:https://jsfiddle.net/mvhvcuo9/2/ 你必須降低輸出視圖,直到導航欄崩潰,因爲我有錯誤僅僅是存在於倒塌的模式。摺疊按鈕目前是黑色的,但這不會改變任何行爲。
我的CSS & HTML:
.dropdown .dropdown-menu {
-webkit-transition: max-height 0.3s, opacity 0.2s 0.1s, visibility 0s 0s;
-moz-transition: max-height 0.3s, opacity 0.2s 0.1s, visibility 0s 0s;
-ms-transition: max-height 0.3s, opacity 0.2s 0.1s, visibility 0s 0s;
-o-transition: max-height 0.3s, opacity 0.2s 0.1s, visibility 0s 0s;
transition: max-height 0.3s, opacity 0.2s 0.1s, visibility 0s 0s;
max-height: 0;
display: block;
overflow: hidden;
opacity: 0;
visibility: hidden;
}
.dropdown.open .dropdown-menu {
-webkit-transition: max-height 0.3s, opacity 0.2s 0.1s, visibility 0s 0s;
-moz-transition: max-height 0.3s, opacity 0.2s 0.1s, visibility 0s 0s;
-ms-transition: max-height 0.3s, opacity 0.2s 0.1s, visibility 0s 0s;
-o-transition: max-height 0.3s, opacity 0.2s 0.1s, visibility 0s 0s;
transition: max-height 0.3s, opacity 0.2s 0.1s, visibility 0s 0s;
max-height: 190px;
display: block;
opacity: 1;
visibility: visible;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" rel="stylesheet" />
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<body>
<div class="container">
<nav class="navbar navbar-custom" "="">
<div class="container-fluid">
<div class="navbar-header">
<button style="background: url(https://cdn.pixabay.com/photo/2016/04/27/16/16/generic-button-1357003_960_720.png)" 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>
<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="/">censor</a> </div>
<div id="navbar" class="collapse navbar-collapse"> <a href="http://censor.censor.censor-censor.de/index.htm" target="_blank"><button type="button" class="btn btn-warning navbar-right"> censor</button></a> <ul class="nav navbar-nav"><li class="dropdown" <!!="=:">
<a href="http://censor.censor-censor.net/censor/" title="censor" class="dropdown-toggle" data-toggle="dropdown">test <b class="caret"></b></a><ul class="dropdown-menu" role="menu" aria-labelledby="dLable"><li><a href="http://censor.censor-censor.net/test/test/">test</a></li><li><a href="http://censor.censor-censor.net/test/">test</a></li><li><a href="http://censor.censor-censor.net/test/">test</a></li><li><a href="http://censor.censor-censor.net/test/test/">test</a></li><li><a href="http://censor.censor-censor.net/testtest/">TEst</a></li><li><a href="http://censor.censor-censor.net/test/">Test</a></li></ul></li>
<li class="dropdown" <!!="=:">
<a href="http://censor.censor-censor.net/censor/" title="censor" class="dropdown-toggle" data-toggle="dropdown">test <b class="caret"></b></a><ul class="dropdown-menu" role="menu" aria-labelledby="dLable"><li><a href="http://censor.censor-censor.net/test/test/">test</a></li><li><a href="http://censor.censor-censor.net/test/">test</a></li><li><a href="http://censor.censor-censor.net/test/">test</a></li><li><a href="http://censor.censor-censor.net/test/test/">test</a></li><li><a href="http://censor.censor-censor.net/testtest/">TEst</a></li><li><a href="http://censor.censor-censor.net/test/">Test</a></li></ul></li>
<li class="dropdown" <!!="=:">
<a href="http://censor.censor-censor.net/censor/" title="censor" class="dropdown-toggle" data-toggle="dropdown">test <b class="caret"></b></a><ul class="dropdown-menu" role="menu" aria-labelledby="dLable"><li><a href="http://censor.censor-censor.net/test/test/">test</a></li><li><a href="http://censor.censor-censor.net/test/">test</a></li><li><a href="http://censor.censor-censor.net/test/">test</a></li><li><a href="http://censor.censor-censor.net/test/test/">test</a></li><li><a href="http://censor.censor-censor.net/testtest/">TEst</a></li><li><a href="http://censor.censor-censor.net/test/">Test</a></li></ul></li>
<li class="dropdown" <!!="=:">
<a href="http://censor.censor-censor.net/censor/" title="censor" class="dropdown-toggle" data-toggle="dropdown">test <b class="caret"></b></a><ul class="dropdown-menu" role="menu" aria-labelledby="dLable"><li><a href="http://censor.censor-censor.net/test/test/">test</a></li><li><a href="http://censor.censor-censor.net/test/">test</a></li><li><a href="http://censor.censor-censor.net/test/">test</a></li><li><a href="http://censor.censor-censor.net/test/test/">test</a></li><li><a href="http://censor.censor-censor.net/testtest/">TEst</a></li><li><a href="http://censor.censor-censor.net/test/">Test</a></li></ul></li>
<ul> </ul></ul></div> </div> </nav>
</nav>
</div>
</div>
</body>
</nav>
編輯:@冷水機的答案是偉大的,但它錯過了一個小東西。爲了使您的下拉菜單中選擇正確關閉壓在它之外或摺疊導航欄的時候,你必須添加下面的JavaScript代碼(jQuery的):
$(document).click(function(){
$(".dropdown-menu").slideUp(300);
});
$(".dropdown-menu").click(function(e){
e.stopPropagation();
});
@Chiller:你改變的東西 - 他們沒有做任何exept語法修正,對吧?仍然有我的問題:( – Scunkaneli
我剛剛爲我和其他人看了你到目前爲止的片段..但它不顯示像你放的視頻的問題..我建議你做一個片段,清楚地顯示你有問題 – Chiller
好的,謝謝!我可以用JSFiddle重現我的問題,這裏是:https://jsfiddle.net/mvhvcuo9/ – Scunkaneli