2

I am appending DOM to some element in an interval, I want it to fade in, but it seems that it first apears, then disappears, and only then fade in is run.

var el = document.getElementById('cont');
el.innerHTML = '';
var toAppend = getElementsToAppend();
$(el).hide().append(toAppend).fadeIn(300);

How should I do this smoothly?

user99999
  • 1,994
  • 5
  • 24
  • 45

1 Answers1

1

Firstly you seem to have an mix of native JS and jQuery methods. I'd suggest sticking with one or the other as much as possible. Also note that you're calling fadeIn() on the $(el), not on the content that was appended to the DOM.

Presuming that elementsToAppend() returns an array of DOMElements, you can put them all in a single jQuery selector and call hide() on them, before you call fadeIn() after appending. Try this:

var $toAppend = $(getElementsToAppend()).hide();
$('#cont').empty().append($toAppend);
$toAppend.fadeIn(300);
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339