我正在尝试消除 Chrome 和 FF 中的闪烁,尽管它在 FF 中要少得多。该脚本根据水平鼠标位置滚动 20 个背景 jpg。这种工作但非常闪烁。
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>
忘了提我用这个预加载
(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 etc])