How do I make my background change images automatically every 15 seconds? JavaScript? jQuery?

后端 未结 3 2100
小蘑菇
小蘑菇 2021-02-06 19:06

I know this sounds simple and I searched up and down the web for a script but cant find anything. I just want my backgrounds to change every 15 seconds or so automatically. (lik

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-06 19:10

    Simple enough to do with setInterval:

    var currentIndex = 1;
    var totalCount = 21;
    
    setInterval(function() {
        if (currentIndex > totalCount)
            currentIndex = 1;
    
        $(body).css('background-image', 'url(/bg' + currentIndex++ + '.jpg)');
    }, 15000);
    

提交回复
热议问题