汉堡菜单

scss
阅读 71 收藏 0 点赞 0 评论 0

functions.php
<?php
  // Custom walker for menu toggles
  class Mobile_Walker extends Walker_Nav_Menu {
      public function start_lvl( &$output, $depth = 0, $args = array() ) {
          $indent = str_repeat("\t", $depth);
          $output .= '<span class="menu-item-sub-menu js-toggle-subnav"><i></i></span>';
          $output .= "\n$indent<ul class=\"sub-menu\">\n";
         }
  }
    
  // Register navigation
	function setup_navs() {
	    register_nav_menus( array(
	        'primary_menu' => 'Primary navigation',
	    ) );
	}
	add_action( 'after_setup_theme', 'setup_navs' );
script.js
    function setupMobile() {
        $('.menu-button').on('click', function(e){
            e.preventDefault();
            $this = $(this);

            if( $this.hasClass('active') ) {
                $this.removeClass('active').addClass('temp-ani');
                setTimeout( function() {
                    $this.addClass('animation').addClass('temp-ani');
                }, 5 );
            }else{
                $this.removeClass('animation');
                setTimeout( function() {
                    $this.addClass('active');
                }, 5);
            }
            $this.next().toggleClass('active');
        });

        $('.menu a').on('click', function(){
            $this = $('.menu-button');
            $this.removeClass('active').addClass('temp-ani');
            setTimeout( function() {
                $this.addClass('animation').addClass('temp-ani');
            }, 5 );
            $this.next().toggleClass('active');
        });
        
        // Toggle submenu
        $('.js-toggle-subnav').on('click', function() {
            $(this).parent('li').toggleClass('active');
        });   

    }
header.php

        <button href="#" class="menu-button hide-desktop">
            <span class="menu-button__before"></span>
            <span class="menu-button__middle"></span>
            <span class="menu-button__after"></span>
        </button>
        <nav class="menu">
            <?php wp_nav_menu( array('theme_location' => 'primary_menu', 'container' => '', 'menu_class' => '', 'walker' => new Mobile_Walker() ) ); ?>
        </nav>
_nav.scss
@media ($belowtablet) {
  .menu {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    right: 0;
    background: rgba( 255, 255, 255, .9 );
    padding-top: 100px;
    opacity: 0;
    pointer-events: none;
    transition: 300ms;
    z-index: 10;

    &.active {
      pointer-events: auto;
      opacity: 1;

      li {
        opacity: 1;
        left: 0;
      }

    }

    li {
      display: block;
      text-align: center;
      margin: 0 0 50px;
      opacity: 0;
      left: -10%;
      transition: 300ms;
      position: relative;

      @for $i from 1 through 20 {
        &:nth-child( #{$i} ) {
          transition-delay: #{$i*100}ms;
        }
      }

    }

    &-button {
      border: 3px solid black;
      background: none;
      border-radius: 5px;
      display: block;
      position: absolute;
      right: 20px;
      top: 20px;
      height: 40px;
      width: 40px;
      z-index: 11;

      &__before {
        position: absolute;
        top: 7px;
        left: 10%;
        right: 10%;
        height: 4px;
        background: black;
      }

      &__middle {
        top: 15px;
        left: 10%;
        right: 10%;
        background: black;
        position: absolute;
        height: 4px;
      }

      &__after {
        bottom: 7px;
        left: 10%;
        right: 10%;
        background: black;
        height: 4px;
        position: absolute;
      }

    }

  }

  .active {

    .menu-button__before {
      animation-name: menuButtonTop;
      animation-duration: .5s;
      animation-direction: normal;
      top: 15px;
      transform: rotate( 45deg );
    }

    .menu-button__middle {
      animation-name: menuButtonMiddle;
      animation-duration: .5s;
      animation-direction: normal;
      opacity: 0;
    }

    .menu-button__after {
      animation-name: menuButtonBottom;
      animation-duration: .5s;
      animation-direction: normal;
      bottom: 15px;
      transform: rotate( -45deg );
    }

  }

  .tmp-ani {

    .menu-button__before {
      top: 15px;
      transform: rotate( 45deg );
    }

    .menu-button__middle {
      opacity: 0;
    }

    .menu-button__after {
      bottom: 15px;
      transform: rotate( -45deg );
    }

  }

  .animation {

    .menu-button__before {
      animation-name: menuButtonTop;
      animation-duration: .5s;
      animation-direction: reverse;
    }

    .menu-button__middle {
      animation-name: menuButtonMiddle;
      animation-duration: .5s;
      animation-direction: reverse;
    }

    .menu-button__after {
      animation-name: menuButtonBottom;
      animation-duration: .5s;
      animation-direction: reverse;
    }

  }
}

@keyframes menuButtonTop {
  0% {
    top: 7px;
    transform: rotate( 0deg );
  }
  50% {
    top: 15px;
    transform: rotate( 0deg );
  }
  100% {
    top: 15px;
    transform: rotate( 45deg );
  }
}

@keyframes menuButtonMiddle {
  0% {
    opacity: 1;
  }
  50% {
    opacity: 1;
  }
  51% {
    opacity: 0;
  }
  100% {
    opacity: 0;
  }
}

@keyframes menuButtonBottom {
  0% {
    bottom: 7px;
    transform: rotate( 0deg );
  }
  50% {
    bottom: 15px;
    transform: rotate( 0deg );
  }
  100% {
    bottom: 15px;
    transform: rotate( -45deg );
  }
}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号