Js 中的背景图片幻灯片 (& HTML, CSS)
Background-Image Slideshow in Js (& HTML, CSS)
我想在我的网站上实现一个简单的背景图片幻灯片,但我在互联网上找不到好的教程,所以我想在这里问一下。
HTML:
<body>
<h3>Welcome!</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;
}
背景图片需要URL在传递图片文件时包裹图片文件。
var i = 0;
var images = [];
var slideTime = 3000; // 3 seconds
images[0] = 'https://via.placeholder.com/150/300AAA';
images[1] = 'https://via.placeholder.com/150/000300';
images[2] = 'https://via.placeholder.com/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.com/150/300AAA");
background-position: center;
background-size: cover;
}
<h3>Welcome!</h3>
<p>Lorem ipsum dolor sit amet</p>
我想在我的网站上实现一个简单的背景图片幻灯片,但我在互联网上找不到好的教程,所以我想在这里问一下。
HTML:
<body>
<h3>Welcome!</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;
}
背景图片需要URL在传递图片文件时包裹图片文件。
var i = 0;
var images = [];
var slideTime = 3000; // 3 seconds
images[0] = 'https://via.placeholder.com/150/300AAA';
images[1] = 'https://via.placeholder.com/150/000300';
images[2] = 'https://via.placeholder.com/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.com/150/300AAA");
background-position: center;
background-size: cover;
}
<h3>Welcome!</h3>
<p>Lorem ipsum dolor sit amet</p>