我正在使用materialize css并在导航栏上有一个下拉列表,鼠标悬停在图标更多的顶部(在右侧示例上)。
当我点击下拉列表中的链接时,目标会正确触发,但下拉列表不会关闭。
工作example here。我希望下拉菜单在点击后自动关闭。在我的HTML代码下面:
<ul id="dropdown1" class="dropdown-content collection">
<li><a href="#/" onclick="openGFS(event, '1', '1'); return false;" id="defaultOpenGFS">Item 1</a></li>
<li><a href="#/" onclick="openGFS(event, '2', '2'); return false;" id="defaultOpenGFS">Item 2</a></li>
</ul>
<nav class="blue darken-3">
<div class="nav-wrapper">
<ul id="tabs" class="left">
<li><a href="#" onclick="openGFS(event, '1', '1'); return false;" id="defaultOpenGFS" class="yellow-text text-accent-3"><b>Item 1</b></a></li>
<li><a href="#" onclick="openGFS(event, '2', '2'); return false;" class="yellow-text text-accent-3"><b>Item 2</b></a></li>
</ul>
<ul class="right">
<li><a class="dropdown-button" href="#!" data-activates="dropdown1"><i class="material-icons tiny">more_vert</i></a></li>
</ul>
</div>
</nav>
<div id="1" class="tabcontent_gfs">
content 1 here
</div>
<div id="2" class="tabcontent_gfs">
content 2 here
</div>
这里是javascript
(function($) {
$(function() {
$('.dropdown-button').dropdown({
inDuration: 300,
outDuration: 225,
hover: true, // Activate on hover
belowOrigin: true, // Displays dropdown below the button
alignment: 'right' // Displays dropdown with edge aligned to the left of button
}
);
}); // End Document Ready
})(jQuery); // End of jQuery name space
var currentTabGFS = null;
function openGFS(evt, tabName, ThirdValue) {
var i, tabcontent, tablinks;
tabcontent = document.getElementsByClassName("tabcontent_gfs");
for (i = 0; i < tabcontent.length; i++) {
tabcontent[i].style.display = "none";
}
tablinks = document.getElementsByClassName("tablinks");
for (i = 0; i < tablinks.length; i++) {
tablinks[i].className = tablinks[i].className.replace(" active", "");
}
document.getElementById(tabName).style.display = "block";
evt.currentTarget.className += " active";
var text = document.getElementById(tabName);
text.innerHTML = text.innerHTML.replace('{var}',tabName);
currentTabGFS = tabName;
}
document.getElementById("defaultOpenGFS").click();
function dismissGFS(){
document.getElementById(currentTabGFS).style.display='none';
};
和css的一部分:
<style class="cp-pen-styles">
.dropdown-content {
background-color: #FFFFFF;
margin: 0;
display: none;
min-width: 350px;
height: 120px;
max-height: auto;
margin-left: -1px;
overflow: auto;
opacity: 0;
position: absolute;
white-space: nowrap;
z-index: 1;
will-change: width, height;
}
</style>
答案 0 :(得分:1)
每the materialize dropdown docs,您可以在点击处理程序中为这些链接使用$('.dropdown-button').dropdown('close');
,但您需要升级materialize,因为您当前的版本不支持它。
(function($) {
$(function() {
$('.dropdown-button').dropdown({
inDuration: 300,
outDuration: 225,
hover: true, // Activate on hover
belowOrigin: true, // Displays dropdown below the button
alignment: 'right' // Displays dropdown with edge aligned to the left of button
});
}); // End Document Ready
})(jQuery); // End of jQuery name space
var currentTabGFS = null;
function openGFS(evt, tabName, ThirdValue) {
var i, tabcontent, tablinks;
tabcontent = document.getElementsByClassName("tabcontent_gfs");
for (i = 0; i < tabcontent.length; i++) {
tabcontent[i].style.display = "none";
}
tablinks = document.getElementsByClassName("tablinks");
for (i = 0; i < tablinks.length; i++) {
tablinks[i].className = tablinks[i].className.replace(" active", "");
}
document.getElementById(tabName).style.display = "block";
evt.currentTarget.className += " active";
var text = document.getElementById(tabName);
text.innerHTML = text.innerHTML.replace('{var}', tabName);
currentTabGFS = tabName;
$('.dropdown-button').dropdown('close');
}
document.getElementById("defaultOpenGFS").click();
function dismissGFS() {
document.getElementById(currentTabGFS).style.display = 'none';
};
body {
font-family: "Lato", sans-serif;
}
/* Style the tab */
div.tab {
overflow: hidden;
border: 1px solid #ccc;
background-color: #f1f1f1;
}
/* Style the buttons inside the tab */
div.tab button {
background-color: inherit;
float: left;
border: none;
outline: none;
cursor: pointer;
padding: 14px 16px;
transition: 0.3s;
font-size: 17px;
}
/* Change background color of buttons on hover */
div.tab button:hover {
background-color: #ddd;
}
/* Create an active/current tablink class */
div.tab button.active {
background-color: #ccc;
}
/* Style the tab content */
.tabcontent {
display: none;
padding: 0px 0px;
border: 0px solid #ccc;
border-top: none;
}
/* Style the close button */
.topright {
float: right;
cursor: pointer;
font-size: 20px;
}
.topright:hover {
color: red;
}
nav .nav-wrapper i.material-icons {
height: 48px;
line-height: 48px;
}
nav ul a {
font-size: 2.1rem;
color: #FFF;
}
nav,
nav .nav-wrapper i,
nav a.button-collapse,
nav a.button-collapse i {
height: 75px;
line-height: 75px;
}
</style> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.98.2/css/materialize.min.css"> <style class="cp-pen-styles"> .dropdown-content {
background-color: #FFFFFF;
margin: 0;
display: none;
min-width: 350px;
height: 120px;
max-height: auto;
margin-left: -1px;
/* Add this to keep dropdown in line with edge of navbar */
/* overflow: hidden;*/
/* Changed this from overflow-y:auto; to overflow:hidden; */
overflow: auto;
opacity: 0;
position: absolute;
white-space: nowrap;
z-index: 1;
will-change: width, height;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.98.2/js/materialize.min.js"></script>
<ul id="dropdown1" class="dropdown-content collection">
<li><a href="#/" onclick="openGFS(event, '1', '1'); return false;" id="defaultOpenGFS">Item 1</a>
<label>
<input type="checkbox"><span class="lever"></span></label>
</li>
<li><a href="#/" onclick="openGFS(event, '2', '2'); return false;" id="defaultOpenGFS">Item 2</a>
<label>
<input type="checkbox"><span class="lever"></span></label>
</li>
</ul>
<nav class="blue darken-3">
<div class="nav-wrapper">
<ul id="tabs" class="left">
<li><a href="#" onclick="openGFS(event, '1', '1'); return false;" id="defaultOpenGFS" class="yellow-text text-accent-3"><b>Item 1</b></a></li>
<li><a href="#" onclick="openGFS(event, '2', '2'); return false;" class="yellow-text text-accent-3"><b>Item 2</b></a></li>
</ul>
<ul class="right">
<li><a class="dropdown-button" href="#!" data-activates="dropdown1"><i class="material-icons tiny">more_vert</i></a></li>
</ul>
</div>
</nav>
<div id="1" class="tabcontent_gfs">
content 1 here
</div>
<div id="2" class="tabcontent_gfs">
content 2 here
</div>