Fading visibility of element using jQuery

前端 未结 5 1676
广开言路
广开言路 2020-12-13 09:02

I\'m having some trouble with finding the visibility param for JQuery.

Basically... the code below does nothing.

$(\'ul.load_details\').animate({
            


        
5条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-13 09:15

    This is what worked for me (based on @Alan's answer)

            var foo = $('ul.load_details'); // or whatever
            var duration = "slow";  // or whatever
    
            if (foo.css('visibility') == 'visible') {
                foo.css({ opacity: 1 }).animate({ opacity: 0 }, duration, function () {
                    foo.css({ visibility: "hidden" });
                });
            } else {
                foo.css({ opacity: 0 }).animate({ opacity: 1 }, duration).css({ visibility: "visible" });
            }
    

    When the foo element is visible, then slowly change the opacity to zero (via animate) and then wait until that's done before setting foo's visibility to be hidden. Otherwise, if set to hidden during the animate process then the fading out effect will not happen since it's hidden immediately.

    Alternatively, you can use the simpler, cleaner fadeTo():

            var foo = $('ul.load_details'); // or whatever
            var duration = "slow";  // or whatever
    
            if (foo.css('visibility') == 'visible') {
                foo.fadeTo(duration, 0, function () {
                    foo.css({ visibility: "hidden" });
                });
            } else {
                foo.fadeTo(duration, 1).css({ visibility: "visible" });
            }
    

提交回复
热议问题