Valli69
Valli69

Reputation: 9902

Body Background Image Rotation should support dynamic number of images

I have used jquery slideshow to rotate background images. But what my requirement is, I have to display background images according to the specific days. I mean some times I have to rotate one image, some times 3, and some times morethan 3 like this. For this one I have to change the script everytime. How can I rotate background images dynamically without changing my script. If anyone have solution plz suggest me some sample script.

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);
});

I'm having the above script.And below is my html

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

Upvotes: 1

Views: 408

Answers (2)

nandu
nandu

Reputation: 779

you can use

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

to find which day it is .. And for image rotation I would recommend raphael library. This is compatible in most of the browsers ( Firefox 3.0+, Safari 3.0+, Chrome 5.0+, Opera 9.5+ and Internet Explorer 6.0+ ) and very easy to use.

Upvotes: 0

antonjs
antonjs

Reputation: 14318

You can make a simple javascript function which returns the var photos based on the current date.

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

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

And your code will be:

 var photos = getPhotos();

Upvotes: 3

Related Questions