Animate Bootstrap 4下拉列表

时间:2018-10-10 10:06:37

标签: javascript css twitter-bootstrap bootstrap-4

我正在为Bootstrap 4下拉列表设置动画,但是我有点挣扎。我已经运行了当前代码:

.navbar-nav .dropdown-menu
{
  border:0px;
  transform: translate3d(0px, -100%, 0px);
  transition: opacity 300ms ease 0s, transform 300ms ease 0s;
  margin: 5px 0px;
}

.navbar-nav .dropdown-menu.show
{
  transform: translate3d(0px, 0px, 0px) !important;
  opacity:1 !important;
}

和JS:

$('#navDrop').on('show.bs.dropdown', function () {
  $(".overlay").fadeIn(150);
})
$('#navDrop').on('hide.bs.dropdown', function () {
  $(".overlay").fadeOut(150);
})

但是CSS似乎根本没有触发?有没有人找到解决此问题的方法?

1 个答案:

答案 0 :(得分:3)

您可以使用CSS来简化此操作...请注意,引导程序在下拉列表中插入了很多内联CSS,这比jQuery fadeIn()优先

.dropdown .dropdown-menu{animation-name: example; animation-duration: 4s;}
@keyframes example {
    from {opacity:.2}
    to {opacity:1;}
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
<div class="container">
  <h2>Dropdown with just CSS </h2>
  <p>change the duration as you want </p>
  <div class="dropdown">
    <button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown">
      Dropdown button
    </button>
    <div class="dropdown-menu">
      <a class="dropdown-item" href="#">Link 1</a>
      <a class="dropdown-item" href="#">Link 2</a>
      <a class="dropdown-item" href="#">Link 3</a>
    </div>
  </div>
</div>