简体   繁体   English

在下拉按钮内旋转字体超赞图标180deg

[英]Rotate Font-Awesome Icon 180deg Inside Dropdown Button

I've created some custom button styles and turned them into dropdowns using bootstraps dropdown classes. 我创建了一些自定义按钮样式,并使用bootstraps下拉菜单将它们转换为下拉菜单。 When I click the buttons, I want the font-awesome icon inside to rotate 180 degrees while the dropdown is open, and to rotate 180 degree the opposite way, back to it's original position, after the button loses active/focus. 当我单击按钮时,我希望在下拉菜单打开时,里面的字体超赞图标旋转180度,并在按钮失去活动/焦点后以相反的方式旋转180度,回到其原始位置。

The code I've been able to right rotates the icon 180deg, pointing it up. 我已经能够正确执行的代码将图标旋转180度,使其朝上。 But I can't get it to rotate back down. 但是我无法使其向下旋转。 Also, I have these 2 css classes. 另外,我有这两个css类。 I've been leaving them commented out because activating them only adds a weird fade effect. 我一直将它们注释掉,因为激活它们只会增加怪异的淡入淡出效果。 Code is below.. 代码如下。

HTML: HTML:

<div class="dropdown">
          <a id="dropdown1" class="hlo-btn-round-dropdown dropdown-toggle " data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" type="button" tabindex="1">Dropdown <i class="fa fa-chevron-down fa-color icon-rotates" aria-hidden="true"></i></a>
          <ul class="dropdown-menu" aria-labelledby="dropdown1">
            <li><a href="#">Action</a></li>
            <li><a href="#">Another action</a></li>
            <li><a href="#">Something else here</a></li>
            <li role="separator" class="divider"></li>
            <li><a href="#">Separated link</a></li>
          </ul>

</div>

CSS: CSS:

.icon-rotates {
  -moz-transition: all 1s linear;
  -webkit-transition: all 1s linear;
  transition: all 1s linear;
}

.icon-rotates.rotate {
  -moz-transition: rotate(180deg);
  -webkit-transition: rotate(180deg);
  transition: rotate(180deg);
}

JS: I've been this with the classes commented out because they make everything weird.. JS:我一直在讲课,因为它们使一切变得怪异。

$('.hlo-btn-round-dropdown').click(function(){

  if($(this).css("transform") == 'none') {
    $(this).children().css('transform', 'rotate(180deg)');
  } else {
    $(this).children().css('transform', 'none');
  }

});

Thanks in advance for any answers! 预先感谢您的任何回答! jQuery always seems to give me issues or maybe I'm combining too many methods of rotating this idk. jQuery似乎总是给我带来问题,或者也许我组合了太多旋转此idk的方法。

Actually, you can do it with bootstrap Class.You don't have to write extra jquery code. 实际上,您可以使用bootstrap Class来实现。您不必编写额外的jquery代码。

Did you notice that when the dropdown is open it has an extra class (open)? 您是否注意到下拉列表打开时有一个额外的类(打开)? So when .dropdown has class .open(like .dropdown.open) then you add extra rule to the .icon-rotates (like .dropdown.open .icon-rotates). 因此,当.dropdown具有.open类(例如.dropdown.open)时,则需要向.icon-rotates(例如.dropdown.open .icon-rotates)添加额外的规则。

 .icon-rotates { -moz-transition: all 1s linear; -webkit-transition: all 1s linear; transition: all 1s linear; } .icon-rotates.rotate { -moz-transition: rotate(180deg); -webkit-transition: rotate(180deg); transition: rotate(180deg); } .dropdown.open .icon-rotates { -webkit-transform: rotate(180deg); transform: rotate(180deg); } 
 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/> <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <div class="dropdown"> <a id="dropdown1" class="hlo-btn-round-dropdown dropdown-toggle " data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" type="button" tabindex="1">Dropdown <i class="fa fa-chevron-down fa-color icon-rotates" aria-hidden="true"></i></a> <ul class="dropdown-menu" aria-labelledby="dropdown1"> <li><a href="#">Action</a></li> <li><a href="#">Another action</a></li> <li><a href="#">Something else here</a></li> <li role="separator" class="divider"></li> <li><a href="#">Separated link</a></li> </ul> </div> 

Since you are using bootstrap, you can capture the dropdown's hiddden.bs.dropdown event to find out when the dropdown got closed and rotate the icon back to normal position- 由于您使用的是引导程序,因此可以捕获下拉菜单的hiddden.bs.dropdown事件,以了解下拉菜单关闭的时间,并将图标旋转回正常位置-

$(".dropdown").on("hidden.bs.dropdown", function(){
    $(this).find('.hlo-btn-round-dropdown').children().css('transform', 'none');
});

Here is a working JSFiddle to see this in action - https://jsfiddle.net/schikara/qre4wpcb/1/ 这里是一个工作的jsfiddle看到这个动作- https://jsfiddle.net/schikara/qre4wpcb/1/

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

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