以下代码每秒或给定的时间更改背景,但图像不带有 css 属性(中心、封面、位置),我如何将此功能包含在图像的 CSS 设置中背景?
$(function() {
var body = $('body');
var backgrounds = ['url(http://static.jsbin.com/images/jsbin_static.png)', 'url(http://static.jsbin.com/images/popout.png)'];
var current = 0;
function nextBackground() {
body.css(
'background',
backgrounds[current = ++current % backgrounds.length]
);
setTimeout(nextBackground, 1000);
}
setTimeout(nextBackground, 1000);
body.css('background', backgrounds[0]);
});
最佳答案
元素有一个 style.backgroundImage 属性来操作背景图像。在 javascript 中设置背景图片:
body.style.backgroundImage = "url('yoururl')";
关于javascript - 在 javascript 函数中包含 CSS 设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37575830/