admin管理员组文章数量:1377537
I'm trying to implement a simple background image slideshow on my website but I can't find a good tutorial on the internet so I thought I'd ask here.
HTML:
<body>
<h3>Wele!</h1>
<p>Lorem ipsum dolor sit amet</p>
</body>
JS:
var i = 0;
var images = [];
var slideTime = 3000; // 3 seconds
images[0] = '/img/bg1.jpg';
images[1] = '/img/bg2.jpg';
images[2] = '/img/bg3.jpg';
function changePicture() {
document.body.style.backgroundImage = images[i];
if (i < images.length - 1) {
i++;
} else {
i = 0;
}
setTimeout("changePicture()", slideTime);
}
window.onload = changePicture;
CSS:
body {
background-image: url("img/bg2.jpg");
background-position: center;
background-size: cover;
}
I'm trying to implement a simple background image slideshow on my website but I can't find a good tutorial on the internet so I thought I'd ask here.
HTML:
<body>
<h3>Wele!</h1>
<p>Lorem ipsum dolor sit amet</p>
</body>
JS:
var i = 0;
var images = [];
var slideTime = 3000; // 3 seconds
images[0] = '/img/bg1.jpg';
images[1] = '/img/bg2.jpg';
images[2] = '/img/bg3.jpg';
function changePicture() {
document.body.style.backgroundImage = images[i];
if (i < images.length - 1) {
i++;
} else {
i = 0;
}
setTimeout("changePicture()", slideTime);
}
window.onload = changePicture;
CSS:
body {
background-image: url("img/bg2.jpg");
background-position: center;
background-size: cover;
}
Share
Improve this question
asked Jul 8, 2020 at 22:08
DavidDavid
751 gold badge2 silver badges10 bronze badges
1
- 1 Is your code working? Is there a problem with it? – imvain2 Commented Jul 8, 2020 at 22:18
1 Answer
Reset to default 4Background image requires URL to wrap the image file when passing an image file.
var i = 0;
var images = [];
var slideTime = 3000; // 3 seconds
images[0] = 'https://via.placeholder./150/300AAA';
images[1] = 'https://via.placeholder./150/000300';
images[2] = 'https://via.placeholder./150/AAA300';
function changePicture() {
document.body.style.backgroundImage = "url(" + images[i] + ")";
if (i < images.length - 1) {
i++;
} else {
i = 0;
}
setTimeout(changePicture, slideTime);
}
window.onload = changePicture;
body {
background-image: url("https://via.placeholder./150/300AAA");
background-position: center;
background-size: cover;
}
<h3>Wele!</h3>
<p>Lorem ipsum dolor sit amet</p>
本文标签: javascriptBackgroundImage Slideshow in Js (amp HTMLcss)Stack Overflow
版权声明:本文标题:javascript - Background-Image Slideshow in Js (& HTML, CSS) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744490557a2608708.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论