All I want to do is fade my logo in on the page loading. I am new today to jQuery and I can\'t managed to fadeIn on load please help. Sorry if this question has already been
CSS3 + jQuery Solution
I wanted a solution that did NOT employ jQuery's fade effect as this causes lag in many mobile devices.
Borrowing from Steve Fenton's answer I have adapted a version of this that fades the image in with the CSS3 transition property and opacity. This also takes into account the problem of browser caching, in which case the image will not show up using CSS.
Here is my code and working fiddle:
HTML
<img src="http://placehold.it/350x150" class="fade-in-on-load">
CSS
.fade-in-on-load {
opacity: 0;
will-change: transition;
transition: opacity .09s ease-out;
}
jQuery Snippet
$(".fade-in-on-load").each(function(){
if (!this.complete) {
$(this).bind("load", function () {
$(this).css('opacity', '1');
});
} else {
$(this).css('opacity', '1');
}
});
What's happening here is the image (or any element) that you want to fade in when it loads will need to have the .fade-in-on-load
class applied to it beforehand. This will assign it a 0 opacity and assign the transition effect, you can edit the fade speed to taste in the CSS.
Then the JS will search each item that has the class and bind the load event to it. Once done, the opacity will be set to 1, and the image will fade in. If the image was already stored in the browser cache already, it will fade in immediately.
Using this for a product listing page.
This may not be the prettiest implementation but it does work well.
Using Desandro's imagesloaded plugin
1 - hide images in css:
#content img {
display:none;
}
2 - fade in images on load with javascript:
var imgLoad = imagesLoaded("#content");
imgLoad.on( 'progress', function( instance, image ) {
$(image.img).fadeIn();
});
I figure out the answer! You need to use the window.onload function as shown below. Thanks to Tec guy and Karim for the help. Note: You still need to use the document ready function too.
window.onload = function() {$('#logo').hide().fadeIn(3000);};
$(function() {$("#div").load(function() {$('#div').hide().fadeIn(750););
It also worked for me when placed right after the image...Thanks
Like Sohne mentions, but I would add this:
$("img").hide();
$("img").bind("load", function () { $(this).fadeIn(); });
or:
$("img").bind("load", function () {
$(this).css({opacity: 0, visibility: "visible"}).animate({opacity: 1}, "slow");
});
You have a syntax error on line 5:
$('#logo').hide();.fadeIn(3000);
Should be:
$('#logo').hide().fadeIn(3000);
window.onload is not that trustworthy.. I would use:
<script type="text/javascript">
$(document).ready(function () {
$('#logo').hide().fadeIn(3000);
});
</script>