如何在活动的 Bootstrap 4 下拉菜单中旋转箭头图标?
Posted
技术标签:
【中文标题】如何在活动的 Bootstrap 4 下拉菜单中旋转箭头图标?【英文标题】:How can I rotate the arrow icon on active Bootstrap 4 dropdown? 【发布时间】:2021-12-07 08:28:24 【问题描述】:我有来自 Bootstrap 4 的下拉菜单。当下拉菜单打开时,我需要让箭头向上旋转。 有谁知道怎么做?
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<div class="dropdown">
<button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Dropdown button
</button>
<div class="dropdown-menu" 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>
【问题讨论】:
凯特,你试过什么?请参阅How to Ask。我们希望您做出努力并提出一个具体的问题。 【参考方案1】:document.querySelectorAll(".btn").forEach(function(btn)
btn.parentNode.addEventListener("click", function()
btn.classList.toggle("rotated");
);
);
body
padding: 30px; /* demo only */
.rotated::after
transform: rotate(180deg);
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<div class="dropdown">
<button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Dropdown button
</button>
<div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
<span class="dropdown-item">Dropdown content</span>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/js/bootstrap.bundle.min.js" integrity="sha384-Piv4xVNRyMGpqkS2by6br4gNJ7DXjqk09RmUpJ8jgGtD7zP9yug3goQfGII0yAns" crossorigin="anonymous"></script>
【讨论】:
很好的解决方案,但我可能会更具体地说明您将事件处理程序和类添加到哪个类。页面上可能有很多按钮。 :)【参考方案2】:您可以利用Bootstrap's events for Dropdown。
body
padding: 30px; /* demo only */
.rotated::after
transform: rotate(180deg);
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css" integrity="sha384-B0vP5xmATw1+K9KRQjQERJvTumQW0nPEzvF6L/Z6nronJ3oUOFUFpCjEUQouq2+l" crossorigin="anonymous">
<div class="dropdown" id="dropdownMenu">
<button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Dropdown button
</button>
<div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
<span class="dropdown-item">Dropdown content</span>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/js/bootstrap.bundle.min.js" integrity="sha384-Piv4xVNRyMGpqkS2by6br4gNJ7DXjqk09RmUpJ8jgGtD7zP9yug3goQfGII0yAns" crossorigin="anonymous"></script>
<script>
$('#dropdownMenu').on('show.bs.dropdown', function()
$(this).find('.dropdown-toggle').addClass('rotated');
).on('hide.bs.dropdown', function()
$(this).find('.dropdown-toggle').removeClass('rotated');
);
</script>
【讨论】:
以上是关于如何在活动的 Bootstrap 4 下拉菜单中旋转箭头图标?的主要内容,如果未能解决你的问题,请参考以下文章
引导、导航选项卡、下拉菜单;如何根据 URI 设置活动选项卡