简体   繁体   English

第二次单击按钮时切换反向动画

[英]Toggle a reverse animation when a button is clicked the second time

I am looking to animate a menu.The menu button is a div which has an animation as well with a click event. 我正在为菜单设置动画。菜单按钮是一个div,它具有动画以及单击事件。 I succesfully got the menu to beatifully appear on the screen, but i can't get it to leave it as well, preferably with the same, but reversed, animation. 我成功地使菜单漂亮地出现在了屏幕上,但是我不能让它也离开它,最好是使用相同但相反的动画。 This is my html: 这是我的html:

<div class="dropdown-meniu">
    <div class="buton-meniu" onclick="myFunction(this)">
        <div class="bar1"></div>
        <div class="bar2"></div>
        <div class="bar3"></div>
    </div>
    <div id="myDropdown" class="dropdown-meniu-content">
        <p>TEST</p>
    </div>
</div>

I have used for the moment the solution that W3 Schools provides for the dropdown, as well for the animation of the button. 目前,我已经使用了W3 Schools为下拉菜单以及按钮动画提供的解决方案。 This is the styling of the menu and the animation: 这是菜单和动画的样式:

.dropdown-meniu-content {
    display: none;
    position: fixed;
    background-color: #696969;
    width: 35%;
    max-width: 500px;
    height: 100%;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
    z-index: 1;
    overflow: hidden;
    right: 0;
    animation-name: slideIn-meniu;
    -webkit-animation-duration: 0.6s;
    -webkit-animation-name: slideIn-meniu;
    animation-duration: 0.6s;
}

@keyframes slideIn-meniu {
    from { transform: translateX(100%); opacity: 0.5;  }
    to { margin-right: 0px; opacity: 1; } 
}

.show {
    display:block;
}

If it is necessary, I will post the other CSS. 如有必要,我将发布其他CSS。 For the script, I have used jQuery and javascript for the two animations(I am a noob at JavaScript/jQuery so I couldn't get it to work with only JavaScript/jQuery alone and I know that jquery is slow to load but I am using it for other things). 对于脚本,我已经将jQuery和javascript用于这两个动画(我在JavaScript / jQuery上是菜鸟,所以我无法让它仅与JavaScript / jQuery一起使用,并且我知道jquery加载缓慢,但是我将其用于其他用途)。

Thank you in advance! 先感谢您!

EDIT: This is the jsFiddle: https://jsfiddle.net/Lfv7qL7z/3/ . 编辑:这是jsFiddle: https : //jsfiddle.net/Lfv7qL7z/3/。

Your issue is that you are trying to animate between display: none and display: block which doesn't work. 您的问题是您试图在display: nonedisplay: block之间进行动画处理,这是行不通的。 You need to hide the element in some other manner, like a negative offset, and then animate it to be visible. 您需要以其他某种方式(例如,负偏移量)隐藏该元素,然后对其进行动画处理以使其可见。

Modify your .dropdown-meniu-content rule to start offscreen and to have a transition property. 修改您的.dropdown-meniu-content规则以启动屏幕外并具有transition属性。 Then have .show reset the transform back to translateX(0%) : 然后让.showtransform重置回transform translateX(0%)

 function myFunction(x) { x.classList.toggle("change"); document.getElementById("myDropdown").classList.toggle("show"); } 
 html, body { margin: 0px; padding: 0px; max-width: 100%; } .wrapper { height: 110px; width: 100%; max-width: 10000px; background-color: rgb(206, 206, 206, 0.2); } .logo { float: left; margin-top: 10px; margin-left: 5px; width: 200px; height: 100px; } .meniu { float: right; width: auto; } .buton-meniu { display: block; cursor: pointer; width: 35px; margin-right: 30px; margin-top: 40px; } .bar1, .bar2, .bar3 { width: 35px; height: 5px; background-color: #333; margin: 6px 0; transition: 0.4s; } .change .bar1 { -webkit-transform: rotate(-45deg) translate(-9px, 6px); transform: rotate(-45deg) translate(-9px, 6px); } .change .bar2 { opacity: 0; } .change .bar3 { -webkit-transform: rotate(45deg) translate(-8px, -8px); transform: rotate(45deg) translate(-8px, -8px); } .dropdown-meniu { position: relative; display: inline-block; } .dropdown-meniu-content { top: 110px; position: fixed; background-color: #696969; width: 30%; max-width: 10000px; height: 100%; box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2); z-index: 1; overflow: hidden; right: 0; transform: translateX(100%); transition: transform 1s ease; animation-name: slideIn-meniu; -webkit-animation-duration: 0.6s; -webkit-animation-name: slideIn-meniu; animation-duration: 0.75s; } @keyframes slideIn-meniu { from { transform: translateX(100%); opacity: 0.5; } to { margin-right: 0px; opacity: 1; } } .show { transform: translateX(0%); } 
  <div class="meniu"> <div class="dropdown-meniu"> <div class="buton-meniu" onclick="myFunction(this)"> <div class="bar1"></div> <div class="bar2"></div> <div class="bar3"></div> </div> <div id="myDropdown" class="dropdown-meniu-content"> <p>TEST </p> </div> </div> </div> 

Not exactly sure what you need, but maybe something like this? 不完全确定您需要什么,但是也许像这样? I pulled out the display: none and stripped down the animations a bit. 我拉出了display: none并略去了动画。

HTML 的HTML

   <div class="dropdown-meniu">
    <div class="buton-meniu" onclick="myFunction(this)">
      <div class="bar1">Click Me</div>
      <div class="bar2"></div>
      <div class="bar3"></div>
    </div>
    <div id="myDropdown" class="dropdown-meniu-content hide">
      <p>TEST</p>
    </div>
  </div>

CSS 的CSS

    .dropdown-meniu-content {
   /*      display: none; */
        position: fixed;
        background-color: #696969;
        width: 35%;
        max-width: 500px;
        height: 100%;
        box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
        z-index: 1;
        overflow: hidden;
        right: 0;

    }
    @keyframes slideIn-meniu {
        from { opacity: 0.5;  }
        to { opacity: 1; } 
    }
    @keyframes slideOut-meniu {
        from { opacity: 1;  }
        to { opacity: 0.5; } 
    }
    .show {
        display:block;
        right: 0;
        transition: right 0.6s;

        -webkit-animation-duration: 0.6s;
        -webkit-animation-name: slideIn-meniu;
        animation-duration: 0.6s ease;
    }
    .hide {
      right: -35%;
      transition: right 0.6s ease;
      display: block;
       -webkit-animation-duration: 0.6s;
       -webkit-animation-name: slideOut-meniu;
        animation-duration: 0.6s ease;
    }

jQuery jQuery的

$('.bar1').click(function(){
 $('.dropdown-meniu-content').toggleClass('show').toggleClass('hide');
})

https://jsfiddle.net/282vpcm2/15/ https://jsfiddle.net/282vpcm2/15/

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

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