Bootstrap 4 打开菜单后插入符方向改变

Bootstrap 4 caret direction change after opening menu

我是新手 Bootstrap 4. 我有一个导航菜单,里面有一个下拉项。它有标准的 Bootstrap 插入符号指向下方,这是自动添加的。

现在,我想在打开导航项的下拉菜单后将方向更改为 'up'。我已经在整个互联网及其他地方进行了搜索,但在不使用 font-awesome 或变通方法的情况下找不到 Bootstrap 4 的有效解决方案。

这是我的 HTML 菜单代码(工作正常),包括下拉项(第 2 项):

<nav class="navbar navbar-expand-sm navbar-light">
  <a class="navbar-brand" href="#"><img src="img/logo.svg" width="80" height="80" alt="Logo"></a>
  <button type="button" class="navbar-toggler" data-toggle="collapse" data-target=".navbar-collapse">
    <span class="sr-only"></span>
    <span class="icon-bar"></span>
    <span class="icon-bar"></span>
    <span class="icon-bar"></span>
  </button>
  <div class="collapse navbar-collapse">
    <div class="navbar-nav">
      <a class="nav-item nav-link active" href="#">Item 1<span class="sr-only">(current)</span></a>
      <li class="nav-item dropdown">
        <a class="nav-link dropdown-toggle" href="" data-toggle="dropdown">Item 2</a>
        <div class="dropdown-menu">
          <a class="dropdown-item" href="#">Item 2.1</a>
          <a class="dropdown-item" href="#">Item 2.2</a>
          <a class="dropdown-item" href="#">Item 2.3</a>
        <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="#">Item 2.4</a>
        </div>
      </li>
      <a class="nav-item nav-link" href="#">Item 3</a>
      <a class="nav-item nav-link" href="#">Item 4</a>
    </div>
  </div>
</nav>

CSS 或 jQuery 解决方案就可以了,只要它不是解决方法而是一个不错的、干净的 Bootstrap 解决方案。应该可以吧?

那么,谁能帮我把打开下拉项时插入符号的方向从下改成上?最终有了漂亮的动画。提前致谢!

您可以像这样简单地旋转它:

.dropdown-toggle[aria-expanded="true"]:after {
  transform: rotate(180deg);
}


/*for the animation*/
.dropdown-toggle:after {
  transition: 0.5s;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
<nav class="navbar navbar-expand-sm navbar-light">
  <a class="navbar-brand" href="#"><img src="img/logo.svg" width="80" height="80" alt="Logo"></a>
  <button type="button" class="navbar-toggler" data-toggle="collapse" data-target=".navbar-collapse">
    <span class="sr-only"></span>
    <span class="icon-bar"></span>
    <span class="icon-bar"></span>
    <span class="icon-bar"></span>
  </button>
  <div class="collapse navbar-collapse">
    <div class="navbar-nav">
      <a class="nav-item nav-link active" href="#">Item 1<span class="sr-only">(current)</span></a>
      <li class="nav-item dropdown">
        <a class="nav-link dropdown-toggle" href="" data-toggle="dropdown">Item 2</a>
        <div class="dropdown-menu">
          <a class="dropdown-item" href="#">Item 2.1</a>
          <a class="dropdown-item" href="#">Item 2.2</a>
          <a class="dropdown-item" href="#">Item 2.3</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="#">Item 2.4</a>
        </div>
      </li>
      <a class="nav-item nav-link" href="#">Item 3</a>
      <a class="nav-item nav-link" href="#">Item 4</a>
    </div>
  </div>
</nav>