[英]bootstrap toggle dropdown from within javascript
I have a bootstrap 4 dropdown menu that expands (toggles) on hover instead of a click. 我有一个bootstrap 4下拉菜单,该菜单在悬停时展开(切换),而不是单击。 On a click a link is followed.
单击后,将链接。 (I know, this is bad practice).
(我知道,这是不好的做法)。 Now I am trying to achieve that on touch-enabled devices, the link is never followed and on a click or touch the dropdown is toggled.
现在,我正在尝试在启用触摸的设备上实现该功能,从不跟踪链接,并且单击或触摸下拉菜单都可以切换。
I have managed to disable the link, but I can't seem to toggle the menu. 我设法禁用了链接,但似乎无法切换菜单。 This is what I tried:
这是我尝试的:
JS JS
var button = jQuery('.btn-dropdown-link');
if ('ontouchstart' in window) {
button.click(function (e) {
e.preventDefault();
console.log('clicked / touched');
jQuery('#dropdownMenuButton').dropdown('toggle'); // this does not work?
});
}
HTML 的HTML
<ul>
<li class="btn dropdown" id="dropdownMenuButton" aria-haspopup="true" aria-expanded="false">
<a class="btn btn-dropdown-link dropdown-toggle" href="/com">some link</a>
<ul class="dropdown-menu" id="dropdownDonateMenu" aria-labelledby="dropdownMenuButton">
<li><a href="#">example</a></li>
<li><a href="#">example</a></li>
<li><a href="#">example</a></li>
</ul>
</li>
</ul>
CSS 的CSS
.dropdown:hover > .dropdown-menu {
max-height: 500px;
opacity: 1;
}
.dropdown .dropdown-menu {
transition: all 0.3s;
max-height: 0;
display: block;
overflow: hidden;
opacity: 0;
min-width: 100%;
}
Or a live example on codeply . 或关于codeply的现场示例 。
How can I toggle the dropdown when I use e.preventDefault();
使用
e.preventDefault();
时如何切换下拉菜单e.preventDefault();
to block the link? 阻止链接?
Edit : I have to use JS 编辑 :我必须使用JS
You can add the :active on css on the same place you have the :hover and on the HTML you can simply remove the href 您可以在:hover的同一位置在CSS上添加:active,并且在HTML上只需删除href
something like this on the CSS: 在CSS上是这样的:
.dropdown::active > .dropdown-menu, .dropdown::active > .dropdown-menu {
max-height: 500px;
opacity: 1;
}
and on the HTML you can simply remove the 在HTML上,您只需删除
You can do that using d-**-none
and d-**-block
on the break point which you wanna show 您可以在要显示的断点上使用
d-**-none
和d-**-block
来执行此操作
.dropdown:hover>.dropdown-menu { display: block; width: fit-content; }
<meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script> <ul class="navbar-nav ml-auto"> <li class="nav-item dropdown" style="cursor:pointer"> <a class="nav-link d-none d-sm-none d-md-none d-lg-block dropdown-toggle" id="navbarDropdown3" data-hover="dropdown" aria-haspopup="true" aria-expanded="false" href="/com">dropdown</a> <a class="nav-link d-lg-none dropdown-toggle" id="navbarDropdown3" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" href="/com">dropdown</a> <div class="dropdown-menu" aria-labelledby="navbarDropdown3"> <a class="dropdown-item" href="#">Action</a> <a class="dropdown-item" href="#">Another action</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="#">Something else here</a> </div> </li> </ul>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.