jquery Flashes Div Element Before Executing .hide() and .fadeIn() Methods
21,536
Solution 1
You could using the opacity instead if you want to keep the dimensions of the element intact:
$('.items').html(response).css({'opacity':0}).animate({'opacity':1});
Solution 2
Hide using CSS and then fade it in when required :
css :
.items {
display:none;
}
JavaScript
$('.items').html(response).fadeIn();
Solution 3
This is a cleaner solution since it avoids a flashing effect of the element being added first, then quickly having its opacity set to 0.
This way the elem is added already having an opacity of 0.
var elem = $(response).css({'opacity': 0});
$('.items').html(elem);
elem.animate({'opacity': 1});
![Mackelito](https://i.stack.imgur.com/LULOr.jpg?s=256&g=1)
Author by
Mackelito
Updated on February 15, 2021Comments
-
Mackelito over 3 years
This is my code:
$('.items').html(response).hide().fadeIn();
The problem is that when this loads, the page "jumps" up due to the element being rendered on page first (having a height) before the
.hide().fadeIn()
is triggered.. is there some other way to do this? -
Mackelito over 12 yearsProblem is that this is a ajax response.. there are items showing there on page load that I´m just filtering
-
Mackelito over 12 yearsClean and simple.. now why didn´t I think of that? ;) Thx!
-
Manse over 12 yearsyou have a couple of other options - replace the HTML and highlight the div to show its changed ... append a div and slide the previous div up and then slide the new div up .... or fadeOut() / fadeIn() ?
-
Mackelito over 12 yearsI found out that this also works... is one faster that the other? $(response).hide().appendTo(ajaxElement).fadeIn();
-
Steve O over 12 yearsI don't think the speed will differ...it's up to you really :)
-
Mackelito over 12 yearsAfter some minor testing the difference was like 0.05s so I quess I´ll pick the one I think looks best :)