我从以下Codepen示例开始:https ://codepen.io/dudleystorey/pen/qEoKzZ
JS:
var bgImageArray = ["lonely.jpg", "uluwatu.jpg"..."],
base = "https://s3-us-west-2.amazonaws.com/s.cdpn.io/4273/full-",
secs = 4;
bgImageArray.forEach(function(img){
new Image().src = base + img;
// caches images, avoiding white flash between background replacements
});
function backgroundSequence() {
window.clearTimeout();
var k = 0;
for (i = 0; i < bgImageArray.length; i++) {
setTimeout(function(){
document.documentElement.style.background =
"url(" + base + bgImageArray[k] + ") no-repeat center center fixed";
document.documentElement.style.backgroundSize ="cover";
if ((k + 1) === bgImageArray.length) { setTimeout(function()
{ backgroundSequence() }, (secs * 1000))} else { k++; }
}, (secs * 1000) * i)
}
}
backgroundSequence();
我在这里尝试的小提琴:https : //jsfiddle.net/ByteMyPixel/x37fk6js/1/
JS:
var bgImageArray = ['~text?txtsize=69&txt=Slide+2&w=816&h=300&txttrack=0',
'~text?txtsize=69&txt=Slide+3&w=816&h=300&txttrack=0'],
base = "https://placeholdit.imgix.net/",
secs = 4;
bgImageArray.forEach(function(img){
new Image().src = base + img;
// caches images, avoiding white flash between background replacements
});
function backgroundSequence() {
window.clearTimeout();
var k = 0;
for (i = 0; i < bgImageArray.length; i++) {
setTimeout(function(){
$('.tab-content-wrapper').css('backgroundImage',
"url(" + base + bgImageArray[k] + ") no-repeat center center fixed");
$('.tab-content-wrapper').css('backgroundSize' , "cover");
if ((k + 1) === bgImageArray.length) { setTimeout(function()
{ backgroundSequence() }, (secs * 1000))} else { k++; }
}, (secs * 1000) * i)
}
}
backgroundSequence();
基本上,我希望本部分右边的背景图像在约5张不同的图像之间转换:
我已经使用工作版本更新了您的提琴:https : //jsfiddle.net/sm1215/r5or5w6m/3/
几件事关闭了
This line below is slightly off.
$('.tab-content-wrapper').css('backgroundImage', "url(" + base + bgImageArray[k] + ") no-repeat center center fixed");
You're setting too many properties at once for "backgroundImage". The properties "no-repeat center center fixed" are multiple properties that defined "background-repeat, background-position, and background-attachment" respectively. You could set all of these at once by just using 'background' (this is considered shorthand css), or you can set each one individually by wrapping your css in curly braces like so:
$('.tab-content-wrapper').css({
'backgroundImage': "url(" + base + bgImageArray[k] + ")",
'background-repeat': "no-repeat",
'background-position': "center center",
'background-attachment': fixed"
});
It's also unnecessary to set all of these for every new image based on your current fiddle's CSS. These are already set once there, so no need to do it all again. You only need to change the URL.
Careful with your jQuery selectors. There are too many periods on this line and it will cause problems
$('..tab-content-wrapper').css('backgroundSize' , "cover");
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句