gpt4 book ai didi

javascript - Body Background Image Rotation 应支持动态图像数量

转载 作者:行者123 更新时间:2023-11-30 13:21:30 24 4
gpt4 key购买 nike

我使用 jquery slideshow 来旋转背景图片。但是我的要求是,我必须根据特定的日期显示背景图像。我的意思是有时我必须像这样旋转一个图像,有时旋转 3 个,有时旋转 3 个以上。对于这个,我每次都必须更改脚本。如何在不更改脚本的情况下动态旋转背景图像。如果有人有解决方案请给我一些示例脚本。

var slideshowSpeed = 6000;
var photos = [
{image1.png},
{image2.png},
{image3.png}
];
$(document).ready(function() {
$("#back").click(function() {
stopAnimation();
navigate("back");
});
$("#next").click(function() {
stopAnimation();
navigate("next");
});
var interval;
$("#control").toggle(function() {
stopAnimation();
}, function() {
$(this).css({"background-image":"url(images/btn_pause.png)"});
navigate("next");
interval = setInterval(function() {
navigate("next");
}, slideshowSpeed);
});
var activeContainer = 1;
var currentImg = 0;
var animating = false;
var navigate = function(direction) {
if (animating) {
return;
}
if (direction == "next") {
currentImg++;
if (currentImg == photos.length + 1) {
currentImg = 1;
}
} else {
currentImg--;
if (currentImg == 0) {
currentImg = photos.length;
}
}
var currentContainer = activeContainer;
if (activeContainer == 1) {
activeContainer = 2;
} else {
activeContainer = 1;
}
showImage(photos[currentImg - 1], currentContainer, activeContainer);
};
var currentZindex = -1;
var showImage = function(photoObject, currentContainer, activeContainer) {
animating = true;
currentZindex--;
$("#headerimg" + activeContainer).css({"background-image":"url(images/" + photoObject.image + ")","display":"block","z-index":currentZindex});
$("#headertxt").css({"display":"none"});
$("#firstline").html(photoObject.firstline);
$("#secondline").attr("href", photoObject.url).html(photoObject.secondline);
$("#pictureduri").attr("href", photoObject.url).html(photoObject.title);
$("#headerimg" + currentContainer).fadeOut(function() {
setTimeout(function() {
$("#headertxt").css({"display":"block"});
animating = false;
}, 500);
});
};
var stopAnimation = function() {
$("#control").css({"background-image":"url(images/btn_play.png)"});
clearInterval(interval);
};
navigate("next");
interval = setInterval(function() {
navigate("next");
}, slideshowSpeed);
});

我有上面的脚本,下面是我的 html

<div id="headerimgs">
<div id="headerimg1" class="headerimg"></div>
<div id="headerimg2" class="headerimg"></div>
<div id="headerimg3" class="headerimg"></div>
</div>

最佳答案

您可以制作一个简单的 javascript 函数,它根据当前日期返回 var photos

function getPhotos() {
var currentDate = new Date(),
photos;

if (currentDate === new Date("December 25, 2012")) {
photos = [{ .... }];
}
return photos;
}

你的代码将是:

 var photos = getPhotos();

关于javascript - Body Background Image Rotation 应支持动态图像数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10124046/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com