简体   繁体   English

使用Array在javascript中单击更改图像

[英]Change Image on click in javascript with Array

So I have a question. 所以我有一个问题。 Right now I have a code that changes my image when I click on it, which is great. 现在,我有一个代码,当我单击它时可以更改我的图像,这很棒。 But I actually need it to function multiple times with different images. 但是我实际上需要它可以使用不同的图像多次运行。 My conclusion: I need an array. 我的结论是:我需要一个数组。 However, I have yet to succeed in actually figuring out how to create a working one with this function. 但是,我还没有成功地弄清楚如何使用此功能来创建一个有效的工具。

I searched all over and managed to kind of gather a code that does something similar, but not quite (like changing images with an array but with an button) This JSfiddle is the one I'm talking about, with changing the image with the button https://jsfiddle.net/ffd7tmwy/1/ 我到处搜索并设法收集执行类似但不完全相同的代码(例如使用数组但使用按钮来更改图像)。我正在谈论的JSfiddle是通过按钮来更改图像。 https://jsfiddle.net/ffd7tmwy/1/

So I tried to edit it, but I failed. 因此我尝试对其进行编辑,但失败了。 This is my attempt: 这是我的尝试:

var change = document.getElementById('one');
var counter = 0;
var myPictures = [
    "url(img/one_two.jpg"
];

function nextPic() {
  counter += 1;
  if (counter > myPictures.length -1) {
    counter = 0;
  }
  change.src = myPictures[counter];
}

This is the one that is working but without the array or multiple pictures: 这是可行的,但没有数组或多张图片:

$(".one").on("click", function() {
  $(this).css("background-image", "url(img/one_two.jpg)");
});

This is another one that did not work.. 这是另一个没有用的..

var backgroundImg = new Array(); 
backgroundImage[0] = "url/one_two.jpg";

  ];

$(function() {
$('.one').on('click', function(){
  $('.one').css('background-image', 'url(' + backgroundImg[0] + ')');
  console.log(backgroundImg);
});

I hope someone could help me and explain how I could fix it. 我希望有人能帮助我并解释如何解决。

Thanks! 谢谢!

Based on the one that works, it looks like you just need to modify your code to correctly modify the background-image CSS property instead of the src property. 基于一种有效的方法,看起来您只需要修改代码即可正确修改background-image CSS属性而不是src属性。 Here's your code but modified to do just that (and different image URLs): 这是您的代码,但经过修改可以做到这一点(以及不同的图像URL):

 var change = document.getElementById('one'); var counter = 0; var myPictures = [ "https://picsum.photos/200/200?image=929", "https://picsum.photos/200/200?image=735", "https://picsum.photos/200/200?image=1063", ]; function nextPic() { counter += 1; if (counter > myPictures.length -1) { counter = 0; } change.style.backgroundImage = "url(" + myPictures[counter] + ")"; } // Here's how you can hookup the click event change.addEventListener('click', nextPic); // Load the first image counter -= 1; // Do this so it starts at the first, not the second nextPic(); 
 #one { width: 200px; height: 200px; border: solid 1px #000; } 
 <div id="one"></div> 

If you wanted to make this work with multiple elements, you could create a function which hooks everything up for you and tracks which image is visible. 如果要使用多个元素来实现此功能,则可以创建一个函数,该函数为您挂接所有内容并跟踪可见的图像。

 var myPictures = [ "https://picsum.photos/200/200?image=929", "https://picsum.photos/200/200?image=735", "https://picsum.photos/200/200?image=1063", ]; function createImageSwitcher(elementID) { var element = document.getElementById(elementID); var counter = 0; function nextPic() { counter += 1; if (counter > myPictures.length - 1) { counter = 0; } element.style.backgroundImage = "url(" + myPictures[counter] + ")"; } element.addEventListener('click', nextPic); // Load the first image counter -= 1; nextPic(); } createImageSwitcher('one'); createImageSwitcher('two'); createImageSwitcher('three'); 
 #one, #two, #three { width: 200px; height: 200px; border: solid 1px #000; } 
 <div id="one"></div> <div id="two"></div> <div id="three"></div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM