gpt4 book ai didi

html - 下拉菜单在点击时打开,在鼠标移开时关闭

转载 作者:太空宇宙 更新时间:2023-11-04 01:51:18 26 4
gpt4 key购买 nike

我只设法使用 css 在悬停时打开它,但我不知道如何将两者结合起来,并在单击时打开它,在鼠标移开时关闭它。如果这个问题以前出现过,我也很抱歉,但我到处搜索都找不到答案。

到目前为止,这是我的代码:

<div class="col-md-3"><div class="dropdown">
<button class="btn btn-secondary dropdown-toggle text-left border-0 rounded-0 py-2" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Dropdown button
<i class="fa fa-plus float-right" aria-hidden="true"></i>
<i class="fa fa-minus float-right" aria-hidden="true"></i>

</button>

<div class="dropdown-menu rounded-0 border-0" aria-labelledby="dropdownMenuButton">
<a class="dropdown-item" href="#">Action</a>
<a class="dropdown-item" href="#">Another action</a>
<a class="dropdown-item" href="#">Something else here</a>
</div>
</div></div>

body {
background: #eeeeee;
}

.btn {
width: 100%;
}
.dropdown-menu {
width: 100%;
height: 100vw;
}

.dropdown:active .dropdown-menu {
display: block;
margin-top: 0;
}

.dropdown-toggle:after {
display:none;
}
.btn:active .fa-plus {
display:none
}

目前,它在点击时打开/关闭。那些令人惊叹的字体图标在那里,因为我也试图让它们发生变化。

谢谢!

最佳答案

编辑

.dropdown-menu上注册mouseleave事件,取消事件冒泡。另外,刚才提到了.dropdown-toggle,应该是data-toggle,不好意思。顺便说一句,将 .dropdown-toggle 类更改为 .data-toggle 这应该无关紧要,但在您剪切和粘贴时很重要。

$('.dropdown-menu').on('mouseleave', function(e) {
$('.data-toggle').dropdown("toggle");
e.stopPropagation();
});


使用 data-toggle 属性定位 .dropdown-toggle 元素,它是 BS 的主要组件,涉及所有涉及dropdown component .使用以下内容:

$('.dropdown').on('mouseleave', function() {
$(".dropdown-toggle").dropdown("toggle");
});

片段

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1, user-scalable=no">
<title></title>
<link href='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css' rel='stylesheet'>
<link href='https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css' rel='stylesheet'>
<style>
body { background: #eeeeee; }
.btn { width: 100%; }
.dropdown-menu { width: 100%; height: 100vw; }
.dropdown:active .dropdown-menu { display: block; margin-top: 0; }
.dropdown-toggle:after { display: none; }
.btn:active .fa-plus { display: none }
</style>
</head>
<body>
<header> </header>
<section id="testA">
<div class="col-md-3">

<div class="dropdown closed">

<button class="data-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Dropdown trigger

Dropdown button
<i class="fa fa-minus float-right" aria-hidden="true"></i>
<i class="fa fa-plus float-right" aria-hidden="true"></i>

</button>

<div class="dropdown-menu rounded-0 border-0" aria-labelledby="dropdownMenuButton">
<a class="dropdown-item" href="#">Action</a>
<a class="dropdown-item" href="#">Another action</a>
<a class="dropdown-item" href="#">Something else here</a> </div>
</div>

</div>

</section>
<footer> </footer>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
<script src='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js'></script>
<script>


$('.dropdown-menu').on('mouseleave', function(e) {
$('.data-toggle').dropdown("toggle");
e.stopPropagation();
});

</script>
</body>
</html>

关于html - 下拉菜单在点击时打开,在鼠标移开时关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43440514/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com