jQuery css Visibility with animation

前端 未结 6 1203
孤独总比滥情好
孤独总比滥情好 2020-12-23 19:23

I have few div\'s placed underneath each other and I\'m using css visibility to fade them in and out. The reason why I use visibility is so that the div\'s don\'t move place

相关标签:
6条回答
  • 2020-12-23 19:44

    I had a similar problem and I solved it this way:

    To fade in :

    $("#id").css({visibility:"visible", opacity: 0.0}).animate({opacity: 1.0},200);
    

    To fade out:

    $("#id").animate({opacity: 0.0}, 200, function(){
        $("#"+txtid).css("visibility","hidden");
    });
    

    As you can see, I hide the div "#id" once the animation has ended. I hope it's not too late

    0 讨论(0)
  • 2020-12-23 19:49

    $('.drop1').css({opacity: 1.0, visibility: "visible"}).animate({opacity: 0}, 200);

    0 讨论(0)
  • 2020-12-23 19:54
    .drop1{ opacity: 0.0; }
    
    $('.drop1').fadeTo( "slow" , 1.0);
    
    0 讨论(0)
  • 2020-12-23 20:06

    I know this is an old post but I came across a similar situation and this is what I ended up doing

    $(".drop1").css("visibility", "visible").show().fadeOut(5000);
    
    0 讨论(0)
  • 2020-12-23 20:07

    why make it so hard, instead of animating the css, you could use the default fade functionality

    $('.drop1').fadeIn(200);
    $('.drop1').fadeOut(200);
    

    edit

    if you however want to fade it out without losing the height . you can use fadeTo(duration, opacity, [callback]);

    $('.drop1').fadeTo(200, 0);
    

    check this example: http://jsfiddle.net/ufLwy/1/

    0 讨论(0)
  • 2020-12-23 20:09

    I was having similar issues, and here's what I ended up doing.

    $.fadeToHidden = function ( selector, duration, complete ) {
        $.when(
            $( selector ).fadeTo( duration, 0 )
        ).done( function(){
            $( selector ).css('visibility', 'hidden')
            complete();
        });
    }
    

    The reason I did this is that

    1. fadeIn()/fadeOut() uses 'display' which F's up an element's height
    2. fadeTo doesn't affect 'visibility', so while the element is visually hidden with opacity:0 users are still able to interact (i.e. click) the invisible element
    3. animate() is asynchronous so chaining CSS at the end doesn't guarantee that it will run when the animation is complete. Only by using the Deferred object that animations return ($.when() / $.done()) will you be guaranteed that the css is applied after all animations are complete.

    EDIT Alternatively, you could apply to "visibility:hidden" to each individual element once their respective animation is complete. This may be slightly quicker for selecting larger groups of elements, since you're only querying the DOM for the group of elements once.

    $.fadeToHidden = function ( selector, duration, complete ) {
        $.when(
            $( selector ).fadeTo( duration, 0 , function(){ 
                $(this).css('visibility', 'hidden');
            } )
        ).done( complete );
    }
    
    0 讨论(0)
提交回复
热议问题