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

标签 javascript jquery html

我使用 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/

相关文章:

html - 对齐周围具有相等空间的元素并保持纵横比?

javascript - 向场景添加超过 37 个聚光灯时出现 Three.js 错误

javascript - 在 contenteditable 范围内检测自动换行

javascript - 如何在 IF 语句内的字符串中使用条件

javascript - 在滚动上缩放 div - Javascript

javascript - 使用javascript单击div部分外部后如何删除文本样式?

html - 如何修复另一个 div 中的 float div?

javascript - 从字符串和时区构建日期对象

javascript - 突出显示事件页面

PHP html电子邮件数组显示