html - Bootstrap 4 下拉菜单动画从页面外开始

标签 html css animation bootstrap-4

我有一行包含两个 div,一个是 col-md-11,另一个是 col-md-1。在这个 col-md-1 div 中,我有一个需要设置动画的下拉菜单。对于动画,我使用 animate css但问题是动画 slideInRight 从页面外部开始。我希望动画应该从行边界开始,而不是在下面的页面之外是我的 html

    <div class="row m-0">
<div class="col-md-7">
</div>
<div class="col-md-5 text-right">
<div class="btn-group">
  <button type="button" class="btn btn-secondary dropdown-toggle" data-toggle="dropdown" data-display="static" aria-haspopup="true" aria-expanded="false">
    Right-aligned
  </button>
  <div class="dropdown-menu dropdown-menu-right dropdown-menu-lg-left animated slideInRight" style="width:500px;">
    <button class="dropdown-item" type="button">Action</button>
    <button class="dropdown-item" type="button">Another action</button>
    <button class="dropdown-item" type="button">Something else here</button>
  </div>
</div>
</div>

</div>

在这个动画slideInRight类中提供了滑入的动画。

最佳答案

在这个答案中,我在你的问题中使用了更新的代码,并对 animate.css 代码和 Bootstrap4 drop-down 元素进行了必要的修改,以便最终结果更符合我所理解的规定要求:

CSS

.animated {
  -webkit-animation-duration: 1s;
  animation-duration: 1s;
  -webkit-animation-fill-mode: both;
  animation-fill-mode: both;
}

@-webkit-keyframes slideInRight {
  from {
    -webkit-transform: translate3d(9rem, 2rem, 0);
    transform: translate3d(9rem, 2rem, 0);
    visibility: visible;
  }

  to {
    -webkit-transform: translate3d(0, 2rem, 0);
    transform: translate3d(0, 2rem, 0);
  }
}

@keyframes slideInRight {
  from {
    -webkit-transform: translate3d(9rem, 2rem, 0);
    transform: translate3d(9rem, 2rem, 0);
    visibility: visible;
  }

  to {
    -webkit-transform: translate3d(0, 2rem, 0);
    transform: translate3d(0, 2rem, 0);
  }
}

.slideInRight {
  -webkit-animation-name: slideInRight;
  animation-name: slideInRight;
}

.dropdown-item {
    height: 2rem !important;
    margin-bottom: 1rem !important;
    padding: 1rem !important;
    width: 25rem !important;
}

这个解决方案是完全适应的,如果你想最终确定它,我很乐意与你合作:)

重要细节:

  • Bootstrap 4 类 text-rightdropdown-menu-lg-left 是 不相容。菜单按钮在任何情况下都不会向左移动 如果将 text-right 添加到 col-md-5

  • 我更改了 translate3d 的“y”属性,以便 slideInRight 出现在按钮下方。如果您愿意,则无​​需进行此更改 menu-item 滑入时覆盖按钮。如果你想 更改它然后 所有引用translate3d 应该有 'y' 元素放回 0:

    translate3d(9rem, 2rem, 0); 会变成 translate3d(9rem, 0, 0); 对所有对 translate3d 的引用进行了类似的更改。

  • 我改的dropdown-item属性也演示了 该元素的适应性无需使用内联样式(如 用以前版本的代码编写)。

希望这个回答对 Feroz 有所帮助。如前所述,我将非常乐意与您合作完成满足您要求的解决方案。 :)

        .animated {
          -webkit-animation-duration: 1s;
          animation-duration: 1s;
          -webkit-animation-fill-mode: both;
          animation-fill-mode: both;
        }
        
        @-webkit-keyframes slideInRight {
          from {
            -webkit-transform: translate3d(9rem, 2rem, 0);
            transform: translate3d(9rem, 2rem, 0);
            visibility: visible;
          }

          to {
            -webkit-transform: translate3d(0, 2rem, 0);
            transform: translate3d(0, 2rem, 0);
          }
        }

        @keyframes slideInRight {
          from {
            -webkit-transform: translate3d(9rem, 2rem, 0);
            transform: translate3d(9rem, 2rem, 0);
            visibility: visible;
          }

          to {
            -webkit-transform: translate3d(0, 2rem, 0);
            transform: translate3d(0, 2rem, 0);
          }
        }

        .slideInRight {
          -webkit-animation-name: slideInRight;
          animation-name: slideInRight;
        }
        
        .dropdown-item {
            height: 2rem !important;
			      margin-bottom: 1rem !important;
            padding: 1rem !important;
            width: 25rem !important;
        }
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">    
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

  </head>
  
  <body>

      <!-- <div class="container"> -->
          
          <div class="row m-0">
              <div class="col-md-7"></div>
              <div class="col-md-5">
                  <div class="btn-group">
                      <button type="button" class="btn btn-secondary dropdown-toggle" data-toggle="dropdown" data-display="static" aria-haspopup="true" aria-expanded="false">
                          Right-aligned
                      </button>
                      <div class="dropdown-menu dropdown-menu-right dropdown-menu-lg-left animated slideInRight" style="">
                          <button class="dropdown-item" type="button">Action</button>
                          <button class="dropdown-item" type="button">Another action</button>
                          <button class="dropdown-item" type="button">Something else here</button>
                      </div>
                  </div>
              </div>
          </div>
          
      <!-- </div> -->

	
	<!-- javascript -->
    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
  
  </body>
</html>

关于html - Bootstrap 4 下拉菜单动画从页面外开始,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58603981/

相关文章:

javascript - 第一个 jquery 事件正在发生,但不是第二个

javascript - 如果浏览器中禁用了 JavaScript,如何禁用 HTML/JavaScript 表单

html - 我们可以在每个 <tr> 上方的表格内添加 div 吗?

css - 有许多未使用的 CSS 选择器/规则对性能有何影响?

javascript - 添加绝对定位元素导致换行

javascript - 如何为 SVG 多边形点设置动画?

c# - 如何通过父控件中的事件为子控件设置动画? - 可重复使用的方式

html - 为什么 image.height 尺寸不合适?

javascript - 使 holder.js 与其他 bootstrap 缩略图大小相同?

javascript - CSS 动画不适用于内联显示属性?