Currently facing a peculiar issue. I am utilizing jQuery to load articles from JSON and would like to dynamically add a class of 'animate' to each loaded element.
$.each(jsonArticles, function (i, article) {
var $articleHTML = $(
'<article class="article">' +
'<a href="' + jsonObject.filmLink + article.reviewLink + '"><img src="' + jsonObject.imagePath + article.reviewImage + '" alt=""></a>' +
'<h1><a href="' + jsonObject.filmLink + article.reviewLink + '">' + article.reviewTitle + '</a></h1>' +
'<p><a href="' + jsonObject.filmLink + article.reviewLink + '">' + article.reviewSummary + '</a></p>' +
'</article>');
$articles
.append($articleHTML)
.find("article")
.addClass("animate");
});
The addition of the class works well as verified in Firebug where it shows up for each article tag.
However, when attempting to apply a CSS transition to animate the class added to the article, there is no animation and it instantly switches to the final style (opacity: 1).
.article {
opacity: 0;
-webkit-transition: all 0.5s ease;
-moz-transition: all 0.5s ease;
-o-transition: all 0.5s ease;
transition: all 0.5s ease;
}
.article.animate {
opacity: 1;
}
The expected animation does not occur despite the class being added and successfully setting the article to opacity: 1. It appears abruptly.
If the .animate
class is modified to include a :hover
, then the articles will only appear on hovering with the animation working as intended. This inconsistency between immediate addition and hover effects is puzzling.
.article.animate:hover {
opacity: 1;
}
Your insights on this matter are highly appreciated.
Thank you, Mikey.
Live Demo: http://jsfiddle.net/Pz5CD/ Observe how the articles abruptly display at full opacity without any animation.