如何让我的背景每15秒自动更改图像? JavaScript? jQuery? [英] How do I make my background change images automatically every 15 seconds? JavaScript? jQuery?

查看:114
本文介绍了如何让我的背景每15秒自动更改图像? JavaScript? jQuery?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这听起来很简单,我上下搜索一个脚本,但没有找到任何东西。我只是想让我的背景每15秒左右自动更改。 (像照片幻灯片,但作为背景。)
我的样式表是控制身体标签中的bg图像。
感谢您的帮助。

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. (like a photo slide show but as a background.) My style sheet is controls the bg image in the body tag. Thanks for the help.

推荐答案

使用 setInterval ,可以调用任意函数,井,间隔:

With setInterval, you can call an arbitrary function in, well, intervals:

(function() {
    var curImgId = 0;
    var numberOfImages = 42; // Change this to the number of background images
    window.setInterval(function() {
        $('body').css('background-image','url(/background' + curImgId + '.jpg)');
        curImgId = (curImgId + 1) % numberOfImages;
    }, 15 * 1000);
})();

这篇关于如何让我的背景每15秒自动更改图像? JavaScript? jQuery?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆