PeteTheGreek
PeteTheGreek

Reputation: 729

Cycle through class

I've got the html below, and I wish to auto-cycle through it. It's currently done manually with a onclick. But how can I get it to cycle? it basically just changes the class name on a wrapper div like so:

<body>
    <div id="client-relations-bg" class="background_tab1">
        ...content...
    </div>
</body>

I have the class image information here, in the head:

<style type="text/css">
    #client-relations-bg.background_tab1{background: url('/media/2/background-image-1.png') no-repeat center top;}
    #client-relations-bg.background_tab2{background: url('/media/32/background-image-6.jpg') no-repeat center top;}
    #client-relations-bg.background_tab3{background: url('/media/17/background-image-3.jpg') no-repeat center top;}
    #client-relations-bg.background_tab4{background: url('/media/12/background-image-2.png') no-repeat center top;}
    #client-relations-bg.background_tab5{background: url('/media/22/background-image-4.jpg') no-repeat center top;}
    #client-relations-bg.background_tab6{background: url('/media/37/background-image-7.jpg') no-repeat center top;}
    #client-relations-bg.background_tab7{background: url('/media/2900/Header_image.jpg') no-repeat center top;}
</style>

So I need this: class="background_tab1" to cycle through tab1 to tab7.

Upvotes: 0

Views: 873

Answers (4)

Salman Arshad
Salman Arshad

Reputation: 272106

Here is one way to do it:

var classes = [
    "background_tab1",
    "background_tab2",
    "background_tab3",
    "background_tab4",
    "background_tab5",
    "background_tab6",
    "background_tab7"
];
setInterval(function() {
    var $div = $("#client-relations-bg");
    $.each(classes, function(i, c) {
        if ($div.hasClass(c)) {
            var j = (i + 1) % classes.length;
            $div.removeClass(c).addClass(classes[j]).text(classes[j]);
            return false;
        }
    });
}, 2000);

});​

Demo here

Upvotes: 1

pxx
pxx

Reputation: 258

You actually don't need to use jQuery

(function(){
    var interval = 1000;
    var currentBackgroundId = 1;
    window.setInterval(function(){
        currentBackgroundId++;
        if (currentBackgroundId == 8) currentBackgroundId = 1;

        var element = document.getElementById('client-relations-bg');
        element.className = 'background_tab' + currentBackgroundId;
    }, interval);
})();

Upvotes: 3

Yoshi
Yoshi

Reputation: 54649

if you know you class names beforehand, this will do:

var
  classList = ['background_tab1', 'background_tab2', 'background_tab3'],
  $el = $('#client-relations-bg');

(function next() {
  setTimeout(function () {
    classList.push(classList.shift());
    $el.removeClass().addClass(classList[0]);
    next();
  }, 2000);
}());

http://jsfiddle.net/c2uqp/

Upvotes: 1

Nicholas Shanks
Nicholas Shanks

Reputation: 10981

jQuery has really good class manipulation methods. All you need to do is either parse your current classes to look for the number at present, or store it in a global variable and do i++ % 7 when setting the next one.

Upvotes: 1

Related Questions