动态更换网页背景

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// 创建一个图片数组
var images = [
"test1.jpg",
"test2.jpg",
"test3.jpg",
"test4.jpg"
];
// 编辑数组为每个图片附上src路径
$(images).each(function () {
$('<img/>')[0].src = this;
});

// 用来标识是否为目前显示的图片
var index = 0;
//默认初始化函数
$.backstretch(images[index], { speed: 500 },test);

function test() {
$("#p_result").html("我是回调函数输出的结果 :"+index+"");
}
$("#btnChange").click(function () {
index = Math.floor((Math.random()*images.length));
$.backstretch(images[index], { speed: 500 },test);
});
$("#btnChange2").click(function () {
// 定时切换调用
setInterval(function () {
index = (index >= images.length - 1) ? 0 : index + 1;
$.backstretch(images[index], { speed: 500 },test);
}, 3000);
});

DEMO 地址: 点我查看