I am trying to remove flicker in Chrome and FF, although its much less in FF. The script scrolls 20 background jpg according to the horizontal position of the mouse. This kind of work, but very flickering.
JQuery
$( document ).ready( function() { var xSlider = $("#third"); //cache var loopvar = 10; //set start img to 10 xSlider.css({backgroundImage : 'url(images/' + loopvar + '.jpg)'}); document.onmousemove = function(e){ var mouseposimg = Math.floor(e.pageX / Math.floor($(window).width() / 20) + 1); if (mouseposimg > 20) { mouseposimg = 21; } //if outside browser if (mouseposimg < 0) { mouseposimg = 1; } if(loopvar != mouseposimg) { xSlider.css({backgroundImage : 'url(images/' + mouseposimg + '.jpg)'}); loopvar = mouseposimg; } };
});
CSS
#third{ background: no-repeat 100%; -webkit-background-size: cover; -moz-background-size: cover; -o-background-size: cover; background-size: cover;
}
HTML
<div id="third"> </div>
Forgot to mention that I preload this
(function(d){var h=[];d.loadImages=function(a,e){"string"==typeof a&&(a=[a]);for(var f=a.length,g=0,b=0;b<f;b++){var c=document.createElement("img");c.onload=function(){g++;g==f&&d.isFunction(e)&&e()};c.src=a[b];h.push(c)}}})(jQuery);
$. loadImages (['1.jpg', '2.jpg' etc. etc.])
Olivier langelaar
source share