情形:
$(function(){
var img_41 = "url('https://s33.postimg.org/e11jeeisv/lagoon-467.jpg')",
img_42 = "url('https://s33.postimg.org/68avmepof/christmas-467.jpg')",
img_43 = "url('https://s33.postimg.org/cyrcvu54f/cars-467.jpg')",
img_1 = "url('https://s33.postimg.org/vr37zgr8v/lagoon-720.jpg')",
img_2 = "url('https://s33.postimg.org/mw2doxfb3/christmas-720.jpg)",
img_3 = "url('https://s33.postimg.org/f3bpwyorj/cars-720.jpg')",
img_91 = "url('https://s33.postimg.org/qsfpkxv5r/lagoon-991.jpg')",
img_92 = "url('https://s33.postimg.org/5is3a2rpr/christmas-991.jpg')",
img_93 = "url('https://s33.postimg.org/a4o7ieidr/cars-991.jpg')",
slider1 =[img_1, img_2, img_3],
slider2 =[img_41, img_42, img_43],
slider3 =[img_91, img_92, img_93],
i =0;
var $target = $('.wrapper');
var $win=$(window);
var arr;
$win.on('resize', function() {
var screen= $win.width();
if(screen < 468){
arr = slider2;
}
else if(screen > 991){
arr = slider3;
}
else{
arr = slider1;
}
$target.css({
'background-repeat':'no-repeat',
'background-position': 'center center',
'display':'block'
});
}).resize();
setInterval(function() {
$target.animate({ opacity: 0 }, 500, function() {
console.log(arr[i]);
$target.css('background-image', arr[i]);
i++;
$target.animate({ opacity: 1 }, 500, function() {
if(i === arr.length) i = 0;
});
});
}, 6000);
});
答案 0 :(得分:0)
当我检查你6秒后调用你的图像功能。你需要在6秒前调出一次动画。所以它会先显示出来。没有等到6秒。
请查看此链接
<强> HTML 强>
<div class="wrapper"></div>
<强> CSS 强>
.wrapper{height:500px}
<强>的jQuery 强>
$(函数(){
var img_41 = "url('https://s33.postimg.org/e11jeeisv/lagoon-467.jpg')",
img_42 = "url('https://s33.postimg.org/68avmepof/christmas-467.jpg')",
img_43 = "url('https://s33.postimg.org/cyrcvu54f/cars-467.jpg')",
img_1 = "url('https://s33.postimg.org/vr37zgr8v/lagoon-720.jpg')",
img_2 = "url('https://s33.postimg.org/mw2doxfb3/christmas-720.jpg)",
img_3 = "url('https://s33.postimg.org/f3bpwyorj/cars-720.jpg')",
img_91 = "url('https://s33.postimg.org/qsfpkxv5r/lagoon-991.jpg')",
img_92 = "url('https://s33.postimg.org/5is3a2rpr/christmas-991.jpg')",
img_93 = "url('https://s33.postimg.org/a4o7ieidr/cars-991.jpg')",
slider1 =[img_1, img_2, img_3],
slider2 =[img_41, img_42, img_43],
slider3 =[img_91, img_92, img_93],
i =0;
var $target = $('.wrapper');
var $win=$(window);
var arr;
$win.on('resize', function() {
var screen= $win.width();
if(screen < 468){
arr = slider2;
}
else if(screen > 991){
arr = slider3;
}
else{
arr = slider1;
}
$target.css({
'background-repeat':'no-repeat',
'background-position': 'center center',
'display':'block'
});
}).resize();
rotateImages($target,arr,i)
setInterval(function() {
rotateImages($target,arr,i)
}, 6000);
});
function rotateImages($target,arr,i) {
$target.animate({ opacity: 0 }, 500, function() {
$target.css('background-image', arr[i]);
i++;
$target.animate({ opacity: 1 }, 500, function() {
if(i === arr.length) i = 0;
});
});
}
希望这会对你有所帮助:)。
答案 1 :(得分:0)
这是一个小提琴。 您必须使所有变量全局化。然后你创建一个功能,使变化本身
然后在document.ready上,您将调用该函数然后设置循环
/*global variables here*/
function slide(){
$target.animate({ opacity: 0 }, 500, function() {
console.log(arr[i]);
$target.css('background-image', arr[i]);
i++;
$target.animate({ opacity: 1 }, 500, function() {
if(i === arr.length) i = 0;
});
});
}
$(function(){
/*other code here*/
slide();
setInterval(function() {
slide();
}, 6000);
});