CSS3 transition with jQuery .addClass and .removeClass

前端 未结 5 2114
灰色年华
灰色年华 2021-02-07 05:34

I have a big main navigation panel that I want to animate when it\'s deploying (expanding).

I\'m working with jQuery to trigger the visibility of it by adding/removing t

相关标签:
5条回答
  • 2021-02-07 05:56

    Don't remove the .hidden class; it contains your transition styles. Just add and remove the .visible class.

    $(document).ready(function() {
        $('#menu-item-9').on('click', function(e) {
            $('#repair-drop').addClass('visible');
        });
    
        $('#repair-drop').on('mouseleave', function(e) {
            setTimeout(function() {
                $('#repair-drop').removeClass('visible');
            }, 600);        
        });
    });
    

    http://jsfiddle.net/mblase75/LjhNa/


    That said, you might need to improve your solution to account for users rapidly mousing out of #repair-drop and clicking on #menu-item-9 before it can hide.

    $(document).ready(function() {
        $('#menu-item-9').on('click', function(e) {
            $('#repair-drop').data('shown',true).addClass('visible');
        });
    
        $('#repair-drop').on('mouseleave', function(e) {
            $('#repair-drop').data('shown',false);
            setTimeout(function() {
                if (!$('#repair-drop').data('shown')) {
                    $('#repair-drop').removeClass('visible');
                }
            }, 600);        
        });
    });
    

    http://jsfiddle.net/mblase75/b8QpB/

    0 讨论(0)
  • 2021-02-07 05:57

    Have you considered using jQuery UI's animation features? such as

    jQuery('#menu-item-9').hide({duration:200,effect:'blind'});
    

    You could also animate the removal of the class, like

    jQuery('#menu-item-9').removeClass('hidden', {duration:200,effect:'blind'});
    
    0 讨论(0)
  • 2021-02-07 06:00

    I am tired of this issue, better use animation:

    .container .element.animation  {
        animation: SHW .5s;
        animation-fill-mode: both
    }
    @keyframes SHW {
        from {
            transform:scale(0.7);
            opacity:0
        }
        to {
            transform: scale(1);
            opacity:1
        }
    }
    

    Add only to .element class .animation and its working:

    $('.container .element').addClass('animation');
    
    0 讨论(0)
  • 2021-02-07 06:05

    You are adding and removing the class that contains the transition CSS. I recommend moving that to its own rule DEMO.

    .hidden{
        max-height: 0px;
    }
    .visible{
        max-height: 500px;  
    }
    
    #repair-drop{
        -webkit-transition: max-height 0.8s;
        -moz-transition: max-height 0.8s;
        transition: max-height 0.8s;
    }
    
    0 讨论(0)
  • 2021-02-07 06:07

    I found a way to make this work using jquery easing plugin. Thanks to all for your responses

    $(document).ready(function() {
        $('#menu-item-9').click(function(){
            $('#repair-drop').removeClass('hide');
            $('#repair-drop').animate({"max-height":"500px", "padding-top":"20px", "opacity":"1"},1500, "easeOutCubic");
        });
    $('#repair-drop').on('mouseleave', function(e) {
        setTimeout(function() {
            $('#repair-drop').animate({"max-height":"0px", "overflow":"hidden", "padding":"0px","opacity":"0"},2000, "easeOutCubic");
    
        }, 600);        
    
    });
    });
    
    0 讨论(0)
提交回复
热议问题