移动设备的动画切换按钮

时间:2015-06-06 10:25:08

标签: javascript jquery html css

我有一个Wordpress网站,其导航基于Bootstrp 3框架。我希望将“切换”按钮设置为动画(从汉堡包外观到'x'和背面)

NAV HTML - Wordpress代码的基本引导程序。

 <nav class="navbar navbar-custom" role="navigation" data-0="opacity:1;" data-50="opacity:0;">

         <!-- Brand and toggle get grouped for better mobile display -->

            <div class="navbar-header">

                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse"> 
                    <span class="sr-only">Toggle navigation</span> 
                    <span class="icon-bar"></span> 
                    <span class="icon-bar"></span> 
                    <span class="icon-bar"></span> 
                </button> 

                <a class="navbar-brand" href="<?php echo home_url(); ?>"></a>

            </div>           

        <!-- Collect the nav links, forms, and other content for toggling --> 

            <div class="collapse navbar-collapse navbar-right"> 
                <?php /* Primary navigation */
                    wp_nav_menu( array(
                    'menu' => 'top_menu',
                    'depth' => 2,
                    'container' => false,
                    'menu_class' => 'nav navbar-nav',
                    //Process nav menu using our custom nav walker
                    'walker' => new wp_bootstrap_navwalker())
                    );
                ?>
            </div>
    </nav>

头标记中的javascript

<script>
  $(document).ready(function () {
  $(".navbar-toggle").on("click", function () {
    $(this).toggleClass("active");
          });
  });
</script>

CSS

.navbar-toggle .icon-bar:nth-of-type(2) {
  top: 1px;
}

.navbar-toggle .icon-bar:nth-of-type(3) {
top: 2px;
}

.navbar-toggle .icon-bar {
  position: relative;
  transition: all 500ms ease-in-out;
}

.navbar-toggle.active .icon-bar:nth-of-type(1) {
  top: 6px;
  transform: rotate(45deg);
}

.navbar-toggle.active .icon-bar:nth-of-type(2) {
  background-color: transparent;
}

.navbar-toggle.active .icon-bar:nth-of-type(3) {
  top: -6px;
  transform: rotate(-45deg);
}

单击时切换移动,因此可以正确定位。但是并没有形成像它应该

的'x'

1 个答案:

答案 0 :(得分:1)

你需要在活动时让第二个栏完全消失。

.navbar-toggle.active .icon-bar:nth-of-type(2) {
  display:none;
}

如果这不起作用,请尝试使用span替换.icon-bar并使用前缀作为动画:

.navbar-toggle span {
  position: absolute;
  transition: .3s all;
  -moz-transition: .3s all;
}

我会玩这个代码集示例:http://codepen.io/u/pen/jGHch

如果您正在寻找不同的动画,请more examples