16

The .fadeOut() method animates the opacity of the matched elements. Once the opacity reaches 0, the display style property is set to none, so the element no longer affects the layout of the page and same is for fadeIn().

My Question is can they use visibility property so they the element occupy the space in layout of the page and is not just visible?

Cœur
  • 37,241
  • 25
  • 195
  • 267
GajendraSinghParihar
  • 9,051
  • 11
  • 36
  • 64

3 Answers3

36

Use jQuery's fadeTo() and then have a callback set the visibility. Example:

$('#fade').on("click", function(){
    $(this).fadeTo(500, 0, function(){
        $(this).css("visibility", "hidden")
    }) // duration, opacity, callback
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<a href="#" id="fade">Click to Fade</a>
<div>This won't move</div>
methodofaction
  • 70,885
  • 21
  • 151
  • 164
8

animate with css opacity seems to achieve a similar effect.

$('#element').animate({opacity: 0}, 1000);

Run the same with opacity: 1 to fade back in.

Credit.

Community
  • 1
  • 1
yuvilio
  • 3,795
  • 32
  • 35
  • 2
    This solution is simple and elegant for certain use-cases, but it bears mention that an element with zero opacity still responds to events (click, key-press, etc.) and participates in the tab-order. The caveat with this strategy is that it has accessibility implications. See: http://stackoverflow.com/questions/272360/does-opacity0-have-exactly-the-same-effect-as-visibilityhidden – Ben Johnson Nov 05 '15 at 20:50
6

Just Overwrite the property in the call back

$('Element').on("click", function() {
    $(this).fadeOut(500, function() {
        $(this).css({"display": "block","visibility": "hidden"});  // <-- Style Overwrite 
    }); 
})​