RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 810817
Accepted
Иван Антонов
Иван Антонов
Asked:2020-04-07 17:29:41 +0000 UTC2020-04-07 17:29:41 +0000 UTC 2020-04-07 17:29:41 +0000 UTC

删除类时反转动画

  • 772

我正在制作一个动画菜单栏。我做了一个打开的动画,但我不能做一个关闭的反向动画。告诉我它是怎么做的?

var collapse = document.getElementById('collapse-menu');
collapse.addEventListener('click', function(e) {
  this.classList.toggle('l-header__bar_close');
});
@keyframes barToClose_one {
  0% {
    transform: translate(0, 0);
  }
  50% {
    transform: translate(0, 9px);
  }
  100% {
    transform: translate(0, 9px) rotate(45deg);
  }
}

@keyframes barToClose_two {
  0% {
    transform: translate(0, 0);
  }
  50% {
    transform: translate(0, 0);
  }
  100% {
    transform: translate(0, 0) rotate(-45deg);
  }
}

@keyframes barToClose_three {
  0% {
    transform: translate(0, 0);
  }
  50% {
    transform: translate(0, -9px);
  }
  100% {
    transform: translate(0, -9px) rotate(-45deg);
  }
}

.l-header {
  height: 45px;
  background-color: #10151c;
}

.l-header__bar {
  width: 50px;
  height: 100%;
  padding: 6px 10px;
  box-sizing: border-box;
}

.l-header__icon-bar {
  background-color: #fff;
  height: 3px;
  margin-top: 6px;
  border-radius: 2px;
}


/* Animate bar to close */

.l-header__bar_close .l-header__icon-bar:nth-child(1) {
  animation: barToClose_one 500ms cubic-bezier(0, .61, 1, .55) forwards;
}

.l-header__bar_close .l-header__icon-bar:nth-child(2) {
  animation: barToClose_two 500ms cubic-bezier(0, .61, 1, .55) forwards;
}

.l-header__bar_close .l-header__icon-bar:nth-child(3) {
  animation: barToClose_three 500ms cubic-bezier(0, .61, 1, .55) forwards;
}
<div class="l-header">
  <div class="l-header__bar" id="collapse-menu">
    <div class="l-header__icon-bar"></div>
    <div class="l-header__icon-bar"></div>
    <div class="l-header__icon-bar"></div>
  </div>
</div>

javascript
  • 2 2 个回答
  • 10 Views

2 个回答

  • Voted
  1. Best Answer
    MobiDevices
    2020-04-07T19:08:32Z2020-04-07T19:08:32Z

    他们把动画搞得太乱了。将其替换为普通transform的,并使用以下命令对其进行动画处理transition:

    var collapse = document.getElementById('collapse-menu');
    collapse.addEventListener('click', function(e) {
      this.classList.toggle('active');
    });
    .l-header {
       height: 45px;
       background-color: #10151c;
    }
    
    .l-header__bar {
       width: 50px;
       height: 100%;
       padding: 17px 10px;
       box-sizing: border-box;
    }
    
    .l-header__icon-bar {
       background-color: #fff;
       height: 3px;
       border-radius: 2px;
    }
    
    
    /* Animate bar to close */
    
    .l-header__icon-bar{
    transition: transform .3s, opacity .3s;
    }
    
    
    .active .l-header__icon-bar:nth-child(1) {
    transform: translate(0, 3px) rotate(45deg);
    }
    .l-header__icon-bar:nth-child(1) {
    transform: translate(0, -6px)
    }
    
    .active .l-header__icon-bar:nth-child(2) {
    opacity: 0;
    }
    
    .active .l-header__icon-bar:nth-child(3) {
    transform: translate(0, -3px) rotate(-45deg)
    }
    .l-header__icon-bar:nth-child(3) {
    transform: translate(0, 6px)
    }
    <div class="l-header">
      <div class="l-header__bar" id="collapse-menu">
        <div class="l-header__icon-bar"></div>
        <div class="l-header__icon-bar"></div>
        <div class="l-header__icon-bar"></div>
      </div>
    </div>

    • 4
  2. user33274
    2020-04-07T18:02:37Z2020-04-07T18:02:37Z

    在 jQuery 上我知道怎么做它没有应用你的标记,但类似......见

    $(document).ready(function() {
      $(".trigger").click(function(e) {
        $(this).toggleClass("active_trigger");
      });
    });
    *{
      margin:0;
      padding:0;
    }
    span.trigger{
      display:inline-block;
      width:50px;
      height:50px;
      margin:40px;
    
    }
    span.trigger i{
      display:block;
      width:40px;
      height:10px;
      background:#000;
      margin:4px auto;
      transition:all .3s linear;
    }
    span.active_trigger i:nth-child(2){
      opacity:0;
    }
    span.active_trigger i:nth-child(1){
      transform:rotate(-45deg)translate(-12px,10px);
    }
    span.active_trigger i:nth-child(3){
      transform:rotate(45deg)translate(-10px,-7px);  
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    
    <div class="header">
      <div class="menu">
        <span class="trigger">
          <i></i>
          <i></i>
          <i></i>
        </span>
      </div>
    </div>

    • 3

相关问题

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    是否可以在 C++ 中继承类 <---> 结构?

    • 2 个回答
  • Marko Smith

    这种神经网络架构适合文本分类吗?

    • 1 个回答
  • Marko Smith

    为什么分配的工作方式不同?

    • 3 个回答
  • Marko Smith

    控制台中的光标坐标

    • 1 个回答
  • Marko Smith

    如何在 C++ 中删除类的实例?

    • 4 个回答
  • Marko Smith

    点是否属于线段的问题

    • 2 个回答
  • Marko Smith

    json结构错误

    • 1 个回答
  • Marko Smith

    ServiceWorker 中的“获取”事件

    • 1 个回答
  • Marko Smith

    c ++控制台应用程序exe文件[重复]

    • 1 个回答
  • Marko Smith

    按多列从sql表中选择

    • 1 个回答
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Suvitruf - Andrei Apanasik 什么是空? 2020-08-21 01:48:09 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5