[英]Simple Image Gallery with Javascript Needs to Rotate Images?
This was supposed to be a simple photogallery in javascript that would rotate the thumbnail images randomly into the bigger image. 这应该是javascript中的一个简单的照相馆,它将缩略图随机地旋转成更大的图像。 I had the basic gallery working before I changed it to align with the class, so I think something is wrong with my onclick function (what I changed). 在更改基础类以使其与类保持一致之前,我已经使用了基本库,因此我认为onclick函数(我所做的更改)有问题。 I cannot figure out why though. 我不知道为什么。 At the bottom I added what I was asked to change, but I'm not supposed to use. 在底部,我添加了要求更改的内容,但我不应该使用。
<div id = "mainImg" ></div>
<div id = "thumbs" ></div>
<script>
var pics = new Array();
pics[0] = 'cat.jpg';
pics[1] = 'dog.jpg';
pics[2] = 'panda.jpg';
pics[3] = 'piggy.jpg';
var picsDesc = new Array();
picsDesc[0] = 'Cat';
picsDesc[1] = 'Dog';
picsDesc[2] = 'Panda';
picsDesc[3] = 'Piggy';
//display stuff
document.getElementById('mainImg').innerHTML = `<img src = '${pics[0]}' class = "mainSize" alt = '${picsDesc[0]}'>`;
for(let i = 0; i<pics.length; i++){
document.getElementById('thumbs').innerHTML += `<img src = '${pics[i]}' class = "thumbSize" alt = '${picsDesc[i]}' onclick = "change(${i})"/>`;
}
//change picture functions
var rotate = setInterval(changeOn3, 3000);
function changeOn3(){
meh = Math.ceil(Math.random() * 3);
change(meh);
}
function change(index){
document.getElementById('mainImg').src = pics[index];
}
//function changeImg(event){
// var target = event.target;
//
// if(target.tagName == "IMG"){
// document.getElementById("mainImg").src =
//target.getAttribute("src");
// }
//}
</script>
document.getElementById('mainImg').innerHTML = `<img src = '${pics[index]}' class = "mainSize" alt = '${picsDesc[index]}'>`;
This should be your answer. 这应该是你的答案。 You cannot change the DOM element's .src when u have not defined it in the actual HTML code. 如果您尚未在实际的HTML代码中定义DOM元素的.src,则无法对其进行更改。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.