使用 javascript 更改 css 背景图像 [英] Change css background-Image using javascript

查看:33
本文介绍了使用 javascript 更改 css 背景图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个图像数组 images 有四个随机图像,

我想使用 javascript 更改 contents 类的 css 样式中的 background-image.

为此,我创建了 buildimagechangeImage 并在 onclick 上尝试更改它.

我怎样才能实现它?

var images = ['https://picsum.photos/200/300/?random', 'https://picsum.photos/200/300/?random', 'https://picsum.photos/200/300/?random'];无功指数 = 0;函数 buildImage() {var img = document.createElement('img');img.src = 图像[索引];document.getElementById('content').style.background - image = (img);}函数更改图像(){var img = document.getElementById('content').getElementsByTagName('img')[0];指数++;指数 = 指数 % 4;//这是因为如果这是最后一张图像然后转到第一张图像我有 4 张图像所以我相应地给出了 4 个更改img.src = 图像[索引];}

.contents {宽度:100px;高度:100px;边框:2px 实心 #FF0000;}

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><头><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/></风格><body onload="buildImage();"><div class="contents" id="content"></div><button onclick="changeImage()">NextImage</button></html>

解决方案

问题:

  1. 你不需要创建 img 标签来应用 background-imageCSS 样式
  2. 您仅应用图像路径

<块引用>

解决方案:您需要在 URL() 之间应用图像路径,就像正常的 CSS 背景图片适用

检查以下代码:

var images = ['https://picsum.photos/200/300/?random', 'https://picsum.photos/200/300/?random', 'https://picsum.photos/200/300/?random'];无功指数 = 0;函数 buildImage() {document.getElementById('content').style.backgroundImage = 'url('+images[index]+')';}函数更改图像(){指数++;if (index >= images.length) index = 0;document.getElementById('content').style.backgroundImage = 'url(' + images[index] + ')';}

.contents {宽度:200px;高度:200px;边框:2px 实心 #FF0000;}

<div class="contents" id="content"></div><button onclick="changeImage()">NextImage</button>

I have an image array images having four random images,

I want to change the background-image in style of css of class contents using javascript.

for that i have created buildimage and changeImage and on onclick tried to change it .

How can i achieve it?

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() {
  var img = document.createElement('img');
  img.src = images[index];
  document.getElementById('content').style.background - image = (img);
}

function changeImage() {
  var img = document.getElementById('content').getElementsByTagName('img')[0];
  index++;
  index = index % 4; // This is for if this is the last image then goto first image I have 4 images so I've given 4 change accordingly 
  img.src = images[index];
}

.contents {
  width: 100px;
  height: 100px;
  border: 2px solid #FF0000;
}

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  </style>
</head>

<body onload="buildImage();">
  <div class="contents" id="content"></div>
  <button onclick="changeImage()">NextImage</button>
</body>

</html>

解决方案

Problem :

  1. You don't need to create img tag to apply background-image in CSS Style
  2. You are applying only image path

Solution : You need to apply image path between URL() just like normal CSS background image applies

Check below code :

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: 200px;
    height: 200px;
    border: 2px solid #FF0000;
}

<div class="contents" id="content"></div>
<button onclick="changeImage()">NextImage</button>

这篇关于使用 javascript 更改 css 背景图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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