javascript - Swap an image without flicker while showing the new one immediately -


tl;dr: there way swap images reliably while showing whichever image being loaded @ time without causing page flicker?


i have 2 images , 2 buttons , when hover on 1 button shows 1 image. hovering on other button swaps second image. doing this:

$('#button1').mouseover(function() {     $('#image').attr('src', 'image1.png'); });  $('#button2').mouseover(function() {     $('#image').attr('src', 'image2.png'); }); 

this works fine when first image has loaded , second hasn't, doesn't show second image until has completed loading. try give user indication of when new image loading (which they're expecting appear immediately), forced add null image before these swaps, this:

$('#button1').mouseover(function() {     $('#image').attr('src', '#');     $('#image').attr('src', 'image1.png'); });  $('#button2').mouseover(function() {     $('#image').attr('src', '#');     $('#image').attr('src', 'image2.png'); }); 

this works great when 1 image loading showing image it's loading once both loaded, null image in between them causes flicker when switching images. thought fix turning null image off once both images loaded has turned out unreliable. both $('#image').prop('complete') , imagesloaded suggested in other locations on stackoverflow inconsistent @ noticing whether image has been loaded or not. detecting loaded images seems dead end.

i considered trying force images show , hide before , after created doesn't seem work @ though i'm not sure why. new 1 doesn't show while loading , i'm not sure if they're swapping properly:

$('#button1').mouseover(function() {     $('#image').hide();     $('#image').attr('src', 'image1.png');     $('#image').show(); });  $('#button2').mouseover(function() {     $('#image').hide();     $('#image').attr('src', 'image2.png');     $('#image').show(); }); 

is there way swap images reliably while showing whichever image being loaded @ time without causing page flicker haven't tried?

what you're wanting preload images cached in browser. there's no delay on mouse over. here's jquery plugin cache images , call them.

$.fn.preload = function() {     this.each(function(){         $('<img/>')[0].src = this;     }); }  // usage:  $(['image1.png','image2.png']).preload(); 

this not code: credit james @ preloading images jquery


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -