正文背景图像旋转应支持动态数量的图像

Body Background Image Rotation should support dynamic number of images

本文关键字:图像 动态 背景 旋转 正文 支持      更新时间:2023-09-26

我使用jquery幻灯片来旋转背景图像。但是我的要求是什么,我必须根据特定日期显示背景图像。我的意思是有时我必须旋转一张图像,有时旋转 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();
您可以使用

var d = new Date();
var n = d.getDay(); 

找出今天是哪一天..对于图像旋转,我会推荐拉斐尔图书馆。 这在大多数浏览器中都兼容(Firefox 3.0+,Safari 3.0 +,Chrome 5.0 +,Opera 9.5 +和Internet Explorer 6.0 +),并且非常易于使用。