简体   繁体   English

无法为第二次单击的jQuery / css设置动画

[英]can't animate second click jquery/css

I'm animating the on click using CSS, which works when clicked, but on the second click the animation does not work. 我正在使用CSS为单击动画,当单击时可以使用CSS,但是在第二次单击时动画不起作用。 The click event is being overridden with jQuery (mainly to allow the dropdown menu to open as a click event instead of hover). jQuery将覆盖click事件(主要是允许将下拉菜单作为click事件而不是悬停来打开)。

Am I doing something wrong in my CSS? 我在CSS中做错了吗? or should I use jQuery to animate the dropdown? 还是应该使用jQuery动画下拉菜单?

you can view it here: http://elevationspa.wpengine.com 您可以在这里查看: http : //elevationspa.wpengine.com

It's the SERVICES tab. 这是“服务”选项卡。

Navigation: 导航:

<nav id="top-menu-nav">
    <ul id="top-menu" class="nav">
        <li id="menu-item-95" class="mega-menu menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children menu-item-95"><a href="#">Services</a>
            <ul class="sub-menu">
                <li id="menu-item-96" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-96"><a href="http://elevationspa.wpengine.com/massage/">Massage</a></li>
                <li id="menu-item-97" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-97"><a href="http://elevationspa.wpengine.com/nails/">Nails</a></li>
                <li id="menu-item-98" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-98"><a href="http://elevationspa.wpengine.com/hair/">Hair</a></li>
                <li id="menu-item-99" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-99"><a href="http://elevationspa.wpengine.com/esthetics/">Esthetics</a></li>
                <li id="menu-item-100" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-100"><a href="http://elevationspa.wpengine.com/acupuncture/">Acupuncture</a></li>
            </ul>
        </li>
    </ul>
</nav>

JS: JS:

<script type="text/javascript">
(function($) {

    function setup_collapsible_submenus() {
        var $menu = $('#top-menu'),
            top_level_link = '#top-menu .menu-item-has-children > a';

        $menu.find('a').each(function() {
            $(this).off('click');

            if ( $(this).is(top_level_link) ) {
                $(this).attr('href', '#');
            }

            if ( ! $(this).siblings('.sub-menu').length ) {
                $(this).on('click', function(event) {
                    $(this).parents('.nav').trigger('click');
                });
            } else {
                $(this).on('click', function(event) {
                    event.preventDefault();
                    $(this).parent().toggleClass('visible');
                });
            } 
        });
    }

    $(window).load(function() {
        setTimeout(function() {
            setup_collapsible_submenus()
        }, 1000);
    });

})(jQuery);
</script>

CSS: CSS:

#main-header #top-menu ul.sub-menu {
        display: none !important;
        visibility: hidden !important;
        transition: all 0.7s ease-in-out;
        animation: gridFadeIn 0.7s ease-in-out both;
      }
#main-header #top-menu .visible  ul.sub-menu {
        display: block !important;
        visibility: visible !important;
        transition: all 0.7s ease-in-out;
        opacity: 1;
        animation: gridFadeIn 0.7s ease-in-out both;
      }
@keyframes grideFadeIn {
  0% {
    opacity: 0;
    transform: translatey(-5%);
  }
    100% {
      opacity: 1;
      transform: translatey(0);
    }
}

You don't need other visibility properties than opacity on the classes: 除了类的opacity ,您不需要其他可见性属性:

#main-header #top-menu ul.sub-menu {
    opacity: 0;
    transition: all 0.7s ease-in-out;
    animation: gridFadeIn 0.7s ease-in-out both;
  }
#main-header #top-menu .visible  ul.sub-menu {
    transition: all 0.7s ease-in-out;
    opacity: 1;
    animation: gridFadeIn 0.7s ease-in-out both;
  }
@keyframes grideFadeIn {
  0% {
    opacity: 0;
    transform: translatey(-5%);
  }
  100% {
    opacity: 1;
    transform: translatey(0);
  }
}

Here's the CSS fiddle https://jsfiddle.net/9vmeo97d/5/ 这是CSS小提琴https://jsfiddle.net/9vmeo97d/5/
Sorry it's a bit messy. 抱歉,这有点混乱。

--Addition-- - 加成 -

Was a little trickier than I thought at first.. Displaying the submenu on hover comes from the WP theme by jQuery. 比起初我想的要复杂一些。显示悬停时的子菜单来自jQuery的WP主题。 Assuming the other hover styles are done with CSS (as it seems), add this jQuery to your code to unbind the hover event listener from the menu item: 假设其他悬停样式是使用CSS完成的(看上去),请将此jQuery添加到代码中以从菜单项中unbind悬停事件监听器的unbind

$('.menu-item-has-children li').off('mouseenter mouseleave');

I'm not sure if I got the selector right, test and comment if not. 我不确定选择器是否正确,如果不正确,请进行测试和评论。

Make sure that this script is run after http://elevationspa.wpengine.com/wp-content/themes/Divi/js/custom.js?ver=3.0.35 . 确保此脚本在http://elevationspa.wpengine.com/wp-content/themes/Divi/js/custom.js?ver=3.0.35之后运行。

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

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