Jquery CSS fadeOut 动画在关闭菜单时不起作用。

huangapple go评论82阅读模式
英文:

Jquery CSS fadeOut animation not working when menu close

问题

这是我的简单下拉菜单的方法。当我单击打开菜单时,它会以动画效果打开。但是我想在关闭时添加另一个动画。我尝试了很多方法,但没有效果。为什么?有人可以帮助我吗?当关闭时,它会直接消失,没有任何动画效果。为什么?

body {
    padding: 15px;
}

.dropdown {
    position: relative;
    display: inline-block;
}

.dropdown-btn {
    width: 40px;
    height: 40px;
    border-radius: 50px;
    border: none;
    background-color: #f4f4f4;
    left: 10px;
    font-size: 20px;
}

.dropdown-btn__icon {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    transition: all 0.3s ease;
}

.dropdown-menu {
    position: absolute;
    top: 50px;
    right: 0;
    background-color: #fff;
    border-radius: 4px;
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.26);
    display: none;
    min-width: 150px;
    overflow: hidden;
    transform-origin: top right;
    animation: dropdown-menu-open 0.3s forwards;
}

.dropdown-menu__list {
    list-style: none;
    margin: 0;
    padding: 0;
}

.dropdown-menu__list li {
    padding: 0px;
}

.dropdown-menu__list a {
    text-decoration: none;
    color: #000;
    font-size: 16px;
    display: block;
    padding: 8px 16px;
}

@keyframes dropdown-menu-open {
    from {
        opacity: 0;
        transform: scale(0, 0);
    }
    to {
        opacity: 1;
        transform: scale(1, 1);
    }
}

.dropdown--open .dropdown-menu {
    display: block;
}

.dropdown-menu__list {
    opacity: 1;
    transition: opacity 0.3s ease;
}

.dropdown-menu__list--hidden {
    opacity: 0;
    transition: opacity 0.3s ease;
}

#asma {
    float: right !important;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>

<span id="asma">
    <div class="dropdown">
        <button class="dropdown-btn mi-ripple mi-ripple-dark">
            <div class="dropdown-btn__icon"> ≡ </div>
        </button>
        <div class="dropdown-menu">
            <ul class="dropdown-menu__list">
                <li class="mi-ripple mi-ripple-dark"><a href="#">Home</a></li>
                <li class="mi-ripple mi-ripple-dark"><a href="#">Dropdown</a></li>
                <li class="mi-ripple mi-ripple-dark"><a href="#">Createanewbjsbsjshsbsticket</a></li>
            </ul>
        </div>
    </div>
</span>
$(document).ready(function() {
  $('.dropdown-btn').on('click', function() {
    $('.dropdown').toggleClass('dropdown--open');
    if ($('.dropdown').hasClass('dropdown--open')) {
      $('.dropdown-menu').stop(true, true).fadeIn(300);
    } else {
      $('.dropdown-menu').stop(true, true).fadeOut(300, function() {
        $(this).removeClass('dropdown-menu__list--hidden');
      });
    }
  });

  $(document).on('touchstart click', function(event) {
    if (!$('.dropdown').is(event.target) && $('.dropdown').has(event.target).length === 0) {
      $('.dropdown').removeClass('dropdown--open');
      $('.dropdown-menu').stop(true, true).fadeOut(300, function() {
        $(this).addClass('dropdown-menu__list--hidden');
      });
    }
  });
});

jQuery的fadeOut效果不起作用,你可以尝试检查以下事项:

  1. 确保jQuery库已正确引入,你可以检查浏览器控制台是否有任何错误消息。
  2. 确保点击事件绑定正确,确保选择器(.dropdown-btn)匹配你的按钮元素。
  3. 确保CSS样式和类名正确,特别是dropdown-menu__list--hidden类。
  4. 你可以尝试调整fadeOut的持续时间,例如fadeOut(300)中的数字来更改动画的速度。

如果上述检查都正常,但仍然无法解决问题,可能需要进一步检查你的代码以排除潜在的问题。

英文:

This is my simple approach for a dropdown menu. When I click to open a menu, it opens with an amination. But I want to add another animation when It will be closed. I tried in many ways, But not working. Why? Somebody, please assist me. When It closes, it directly disappears. No animation is working. Why?

    body {
padding: 15px;
}
.dropdown {
position: relative;
display: inline-block;
}
.dropdown-btn {
width: 40px;
height: 40px;
border-radius: 50px;
border: none;
background-color: #f4f4f4;
left: 10px;
font-size: 20px;
}
.dropdown-btn__icon {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
transition: all 0.3s ease;
}
.dropdown-menu {
position: absolute;
top: 50px;
right: 0;
background-color: #fff;
border-radius: 4px;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.26);
display: none;
min-width: 150px;
overflow: hidden;
transform-origin: top right;
animation: dropdown-menu-open 0.3s forwards;
}
.dropdown-menu__list {
list-style: none;
margin: 0;
padding: 0;
}
.dropdown-menu__list li {
padding: 0px;
}
.dropdown-menu__list a {
text-decoration: none;
color: #000;
font-size: 16px;
display: block;
padding: 8px 16px;
}
@keyframes dropdown-menu-open {
from {
opacity: 0;
transform: scale(0, 0);
}
to {
opacity: 1;
transform: scale(1, 1);
}
}
.dropdown--open .dropdown-menu {
display: block;
}
.dropdown-menu__list {
opacity: 1;
transition: opacity 03s ease;
}
.dropdown-menu__list--hidden {
opacity: 0;
transition: opacity 03s ease;
}
#asma {
float: right! important;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>

&lt;span id=&quot;asma&quot;&gt;
&lt;div class=&quot;dropdown&quot;&gt;
&lt;button class=&quot;dropdown-btn mi-ripple mi-ripple-dark&quot;&gt;
&lt;div class=&quot;dropdown-btn__icon&quot;&gt; &amp;equiv; &lt;/div&gt;
&lt;/button&gt;
&lt;div class=&quot;dropdown-menu&quot;&gt;
&lt;ul class=&quot;dropdown-menu__list&quot;&gt;
&lt;li class=&quot;mi-ripple mi-ripple-dark&quot;&gt;&lt;a href=&quot;#&quot;&gt;Home&lt;/a&gt;&lt;/li&gt;
&lt;li class=&quot;mi-ripple mi-ripple-dark&quot;&gt;&lt;a href=&quot;#&quot;&gt;Dropdown&lt;/a&gt;&lt;/li&gt;
&lt;li class=&quot;mi-ripple mi-ripple-dark&quot;&gt;&lt;a href=&quot;#&quot;&gt;Createanewbjsbsjshsbsticket&lt;/a&gt;&lt;/li&gt;      
&lt;/ul&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/span&gt;
$(document).ready(function() {
$(&#39;.dropdown-btn&#39;).on(&#39;click&#39;, function() {
$(&#39;.dropdown&#39;).toggleClass(&#39;dropdown--open&#39;);
if ($(&#39;.dropdown&#39;).hasClass(&#39;dropdown--open&#39;)) {
$(&#39;.dropdown-menu&#39;).stop(true, true).fadeIn(300);
} else {
$(&#39;.dropdown-menu&#39;).stop(true, true).fadeOut(300, function() {
$(this).removeClass(&#39;dropdown-menu__list--hidden&#39;);
});
}
});
$(document).on(&#39;touchstart click&#39;, function(event) {
if (!$(&#39;.dropdown&#39;).is(event.target) &amp;&amp; $(&#39;.dropdown&#39;).has(event.target).length === 0) {
$(&#39;.dropdown&#39;).removeClass(&#39;dropdown--open&#39;);
$(&#39;.dropdown-menu&#39;).stop(true, true).fadeOut(300, function() {
$(this).addClass(&#39;dropdown-menu__list--hidden&#39;);
});
}
});
});

The jquery fadeOut effect not working. What I can do now? How to add it?

答案1

得分: 1

你实际上不需要使用 fadeInfadeOut,甚至不需要使用CSS animation。相反,你可以使用CSS transition 来实现相同的效果。
更新的脚本:

$(document).ready(function() {
$(&#39;.dropdown-btn&#39;).on(&#39;click&#39;, function() {
$(&#39;.dropdown&#39;).toggleClass(&#39;dropdown--open&#39;);
});
$(document).on(&#39;touchstart click&#39;, function(event) {
if (!$(&#39;.dropdown&#39;).is(event.target) &amp;&amp; $(&#39;.dropdown&#39;).has(event.target).length === 0) {
$(&#39;.dropdown&#39;).removeClass(&#39;dropdown--open&#39;);
}
});
});

更新的样式:

.dropdown-menu {
transition: all 0.3s ease-in-out;     &lt;--- 这里是关键
transform: scale(0);                  &lt;---
position: absolute;
top: 50px;
right: 0;
background-color: #fff;
border-radius: 4px;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.26);
min-width: 150px;
overflow: hidden;
transform-origin: top right;
}
.dropdown.dropdown--open .dropdown-menu {
transform: scale(1);                  &lt;---
}

查看 https://jsfiddle.net/cheack/kd92r8g0/42/

更新:如果你想在关闭时应用不同的过渡效果,只需添加另一个过渡效果并在第一个上添加延迟。

.dropdown-menu {
transition: opacity 0.3s ease-in-out, transform 0.3s ease-in-out 0.3s;   &lt;--- 最后的0.3s是用于缩放的延迟(首先淡出,然后缩小)
transform: scale(0);
opacity: 0;
}
.dropdown.dropdown--open .dropdown-menu {
transform: scale(1); 
opacity: 1;
transition: transform 0.3s ease-in-out;
}
英文:

You don't actually need to use fadeIn and fadeOut, or even CSS animation at all. Instead, you can achieve the same effect using CSS transition.
Updated script:

$(document).ready(function() {
$(&#39;.dropdown-btn&#39;).on(&#39;click&#39;, function() {
$(&#39;.dropdown&#39;).toggleClass(&#39;dropdown--open&#39;);
});
$(document).on(&#39;touchstart click&#39;, function(event) {
if (!$(&#39;.dropdown&#39;).is(event.target) &amp;&amp; $(&#39;.dropdown&#39;).has(event.target).length === 0) {
$(&#39;.dropdown&#39;).removeClass(&#39;dropdown--open&#39;);
}
});
});

Updated styles:

.dropdown-menu {
transition: all 0.3s ease-in-out;     &lt;--- here is the trick
transform: scale(0);                  &lt;---
position: absolute;
top: 50px;
right: 0;
background-color: #fff;
border-radius: 4px;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.26);
min-width: 150px;
overflow: hidden;
transform-origin: top right;
}
.dropdown.dropdown--open .dropdown-menu {
transform: scale(1);                  &lt;---
}

See https://jsfiddle.net/cheack/kd92r8g0/42/

Update: If you want to apply a different transition when closing, simply add another transition and add a delay to the first one.

.dropdown-menu {
transition: opacity 0.3s ease-in-out, transform 0.3s ease-in-out 0.3s;   &lt;--- last 0.3s is a delay for a scaling (to fade out first, then scale down)
transform: scale(0);
opacity: 0;
}
.dropdown.dropdown--open .dropdown-menu {
transform: scale(1); 
opacity: 1;
transition: transform 0.3s ease-in-out;
}

huangapple
  • 本文由 发表于 2023年2月18日 02:18:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75487959.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定