简体   繁体   English

Bootstrap 3下拉过渡

[英]Bootstrap 3 dropdown transition

Firstly here's the fiddle 首先,这是小提琴

Just a regular bootstrap dropdown, I made a few changes to css so that the dropdown appears on hover (instead of click) but how do I want a very simple fade animation. 只是一个常规的bootstrap下拉列表,我对css进行了一些更改,以便在悬停时显示下拉列表(而不是单击),但我如何想要一个非常简单的淡入淡出动画。 I tried css transition but it didn't work because the .dropdown-menu element has a 'display: none' applied to it, on hover it changes to 'display: block'. 我尝试过css过渡,但它没有用,因为.dropdown-menu元素有一个'display:none'应用于它,在悬停时它变为'display:block'。 How do I animate an element which changes from 'diplay: none' to 'display: block' or is there any other method to achieve this? 如何为从'diplay:none'更改为'display:block'的元素设置动画,还是有其他方法可以实现此目的?

I've already googled this and found out some answer but they didn't help. 我已经用谷歌搜索了这个并找到了答案,但他们没有帮助。

HTML Code: HTML代码:

<div class="dropdown">
  <button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown">
    Dropdown
    <span class="caret"></span>
  </button>
  <ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu1">
    <li role="presentation"><a role="menuitem" tabindex="-1" href="#">Action</a></li>
    <li role="presentation"><a role="menuitem" tabindex="-1" href="#">Another action</a></li>
    <li role="presentation"><a role="menuitem" tabindex="-1" href="#">Something else here</a></li>
    <li role="presentation" class="divider"></li>
    <li role="presentation"><a role="menuitem" tabindex="-1" href="#">Separated link</a></li>
  </ul>
</div>

CSS Code: CSS代码:

.dropdown .dropdown-menu{
opacity: 0;
transition:         all 400ms ease;
-moz-transition:    all 400ms ease;
-webkit-transition: all 400ms ease;
-o-transition:      all 400ms ease;
-ms-transition:     all 400ms ease;
}
.dropdown:hover .dropdown-menu {
    display: block;
    opacity: 1;
}
.dropdown .dropdown-menu {
  display: block;
  visibility: hidden;
  opacity: 0;
  transition:         all 0.2s  ease;
  -moz-transition:    all 0.2s  ease;
  -webkit-transition: all 0.2s  ease;
  -o-transition:      all 0.2s  ease;
  -ms-transition:     all 0.2s  ease;
}
.dropdown:hover .dropdown-menu {
  visibility: visible;
  opacity: 1;
}
.dropdown {
  display: inline-block;
}

Just add display:block and visibility:hidden; 只需添加display:blockvisibility:hidden; to .dropdown .dropdown-menu { . to .dropdown .dropdown-menu { Then add visibility: visible to .dropdown:hover .dropdown-menu { and you are done. 然后添加visibility: visible .dropdown:hover .dropdown-menu { visibility: visible .dropdown:hover .dropdown-menu {你就完成了。

You need to change visibility since the opacity of the dropdown menu is 0 but it is still there. 您需要更改可见性,因为下拉菜单的不透明度为0但它仍然存在。 You can check this by hovering under your button. 您可以将鼠标悬停在按钮下进行检查。 By changing the visibility your dropdown menu will only be there when your button gets hovered. 通过更改可见性,您的下拉菜单将仅在您的按钮悬停时才会出现。

You can override the default style of display:none with display:block, since you're also using opacity:0 to hide the menu. 您可以使用display:block覆盖display:none的默认样式,因为您还使用opacity:0来隐藏菜单。 Give the following CSS a try and see if that accomplishes what you need. 尝试下面的CSS,看看它是否能满足您的需求。 I've updated the transition speed to make the effect more apparent. 我已经更新了转换速度,使效果更加明显。

.dropdown .dropdown-menu{
    display: block;
    opacity: 0;

    -moz-transition:    all 1000ms ease;
    -webkit-transition: all 1000ms ease;
    -o-transition:      all 1000ms ease;
    -ms-transition:     all 1000ms ease;
    transition:         all 1000ms ease;
}
.dropdown:hover .dropdown-menu {
    display: block;
    opacity: 1;
}

Updated version of your fiddle: http://jsfiddle.net/pjej7o2m/1/ 您的小提琴的更新版本: http//jsfiddle.net/pjej7o2m/1/

Here's a jQuery script that might work as an alternative to hovering over the div (still using the css transition properties for opacity) 这是一个jQuery脚本,可以作为将鼠标悬停在div上的替代方法(仍然使用不透明度的css过渡属性)

$(function(){
  var $menu = $('.dropdown-menu');

  $('.dropdown-toggle').hover(
    function() {
      $menu.css('opacity',1);
    },
    function() {
      $menu.css('opacity',0);
    });
})();

Updated fiddle: http://jsfiddle.net/pjej7o2m/2/ 更新小提琴: http//jsfiddle.net/pjej7o2m/2/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM