Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery Flashes Div Element Before Executing .hide() and .fadeIn() Methods

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?

like image 680
Mackelito Avatar asked Jan 26 '12 11:01

Mackelito


People also ask

How to fadeIn jQuery?

The jQuery fadeIn() method is used to fade in a hidden element. Syntax: $(selector). fadeIn(speed,callback);

What is the difference between fadeOut and hide in jQuery?

fadeOut() the place will be removed at once. . show(duration) and . hide(duration) animate the size of element (also the opacity) to 100% and 0% and the place of elements is also animated in that duration.

What is the opposite of hide in jQuery?

ready(function(){ jQuery('body').


2 Answers

Hide using CSS and then fade it in when required :

css :

.items {
   display:none;
}

JavaScript

$('.items').html(response).fadeIn();

like image 125
Manse Avatar answered Sep 23 '22 11:09

Manse


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});
like image 30
Steve O Avatar answered Sep 25 '22 11:09

Steve O