Background attenuation only after fully loaded

I am trying to load a large background image ... Although I use .load , the image just loads normally and does not have a smooth fadeIn after loading. Here is my code.

 $(function(){ var bgimage = new Image(); bgimage.src="http://placeimg.com/760/460/tech"; $(bgimage).load(function(){ $(".feature").css("background-image","url("+$(this).attr("src")+")").fadeIn(slow); }); }); 

Demo: http://jsfiddle.net/nud2bfb1/

What am I doing wrong?

+5
source share
4 answers

Try the following: first you need to hide the image, and then use fadeIn() , also fadeIn(slow) should be fadeIn("slow") ie slow in quotes

 $(function(){ var bgimage = new Image(); bgimage.src="http://placeimg.com/760/460/tech"; $(".feature").hide(); $(bgimage).load(function(){ $(".feature").css("background-image","url("+$(this).attr("src")+")").fadeIn(2000); }); }); 

Demo

+6
source

You cannot detect the load on background images. You cannot translate / animate background images.

What you and other answers are trying to do is fade / animate the whole div after loading the img element. It does not work flawlessly every time. Because this IMO is destructive, because either the entire div (along with its contents) must be hidden when loading the image, or you hide the div , and then try to fade it. This will give you a flicker, as the div will be hidden before fading.

I would suggest that you use a dummy img element, as you already do, but set it to absolute (with respect to the div ) with the same size and negative z-index . Thus, the div and its contents can be visible during image loading. After loading, you lose the dummy img in (behind the div ), then assign the background-image , and then delete the dummy img .

Demo: http://jsfiddle.net/abhitalks/q7cvn2nd/

See this snippet:

 var $img = $("<img />"); $img.load(function() { $(this).fadeIn("slow", function() { $('.feature').css('background-image', 'url(http://lorempixel.com/240/240)'); $(this).remove(); }); }); $img.addClass("dummy"); $img.attr('src', 'http://lorempixel.com/240/240'); $(".feature").append($img); 
 * { box-sizing: border-box; } .feature { position: relative; width: 240px; height: 240px; border: 1px solid gray; padding: 8px; color: #f00; background-color: transparent; background-repeat: no-repeat; background-size: 100% 100%; } img.dummy { top: 0; left: 0; bottom: 0; right: 0; width: 100%; height: 100%; position: absolute; z-index: -1; display: none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="feature"><h1>This is some text.</h1></div> 

It is important . Remember to delete the dummy image.

+2
source

Your image is displayed by default. Hide it before fading in it:

 <div class="feature" style="display:none;"></div> 

FIDDLE : http://jsfiddle.net/nud2bfb1/3/

0
source
 $(function(){ var bgimage = new Image(); bgimage.src="http://placeimg.com/760/460/tech"; $(bgimage).load(function(){ $(".feature").css("background-image","url("+$(this).attr("src")+")").not(':visible').fadeIn("slow"); }); }); 

demo

0
source

Source: https://habr.com/ru/post/1207107/


All Articles