【发布时间】:2018-09-26 09:01:58
【问题描述】:
我有一个图像数组 images 有四个随机图像,
contents类css样式的背景图片每次点击按钮都会改变,有buildimage和changeImage
是否可以将加载到类 contents 中的每个 图像 设置宽度和高度 为 same 宽度和班级高度
如何实现?
var images = ['https://picsum.photos/200/300/?random', 'https://picsum.photos/200/300/?random', 'https://picsum.photos/200/300/?random'];
var index = 0;
function buildImage() {
document.getElementById('content').style.backgroundImage = 'url('+images[index]+')';
}
function changeImage() {
index++;
if (index >= images.length) index = 0;
document.getElementById('content').style.backgroundImage = 'url(' + images[index] + ')';
}
.contents {
width: 70%;
height: 50%;
border: 2px solid #FF0000;
}
p{
color:#00FF00;}
<div class="contents" id="content"><p>set background image height ans width same as this box </p></div>
<button onclick="changeImage()">NextImage</button>
【问题讨论】:
-
您可以使用
background-size: 100% 100%;简单地实现它 - 这就是您想要的吗? -
试试 background-size:cover;
标签: javascript html css