我在使用 jquery 预加载功能时遇到了一些问题,想知道是否有人可以帮助我。我在 jquery 预加载插件中添加了为我的站点加载背景图像,但我想创建一个 onComplete 函数,以便在加载图像时启动背景随机播放功能。但我似乎无法做到这一点。如果您知道更好的方法或方法,那将非常感谢。
$(document).ready(function () {
$.fn.preload = function () {
this.each(function () {
$('<img/>')[0].src = this;
});
}
$(['../images/background1.jpg', '../images/background2.jpg', '../images/background3.jpg', '../images/background4.jpg', '../images/background5.jpg', '../images/background6.jpg', '../images/background7.jpg']).preload();
var images = new Array();
images[1] = "../images/background-1.jpg",
images[2] = "../images/background-7.jpg",
images[3] = "../images/background-4.jpg",
images[4] = "../images/background-5.jpg",
images[5] = "../images/background-6.jpg",
images[6] = "../images/background-2.jpg",
Array.prototype.shuffle = function () {
var len = this.length;
var i = len;
while (i--) {
var p = parseInt(Math.random() * len);
var t = this[i];
this[i] = this[p];
this[p] = t;
}
};
images.shuffle();
// A little script for preloading all of the images
// It's not necessary, but generally a good idea
var index = 0;
$.backstretch(images[index], { speed: 1000 });
var slideshow = setInterval(function () {
index = (index >= images.length - 1) ? 0 : index + 1;
$.backstretch(images[index]);
}, 5000);
});