Javascript 背景图像随着单击而改变
我正在尝试更改Javascript 背景图像随着单击而改变,javascript,html,onclick,addeventlistener,mouseclick-event,Javascript,Html,Onclick,Addeventlistener,Mouseclick Event,我正在尝试更改鼠标单击上的4个背景图像 此时,同一图像在屏幕上出现4次(每个角1次)。 我想只有一个图像出现在整个屏幕上,其他人取代它,当我点击当前的图像;当你再次点击时,等等。 我不确定我的代码现在出了什么问题 HTML: CSS: 与其为每个图像使用类,不如将它们存储在数组中,然后以编程方式进行更改。请查找下面的代码片段 让imgList=[ 'https://dummyimage.com/200x200/000/fff&text=image+1', 'https://dummyimage
鼠标单击
上的4个背景图像
此时,同一图像在屏幕上出现4次(每个角1次)。
我想只有一个图像出现在整个屏幕上,其他人取代它,当我点击当前的图像;当你再次点击时,等等。
我不确定我的代码现在出了什么问题
HTML:
CSS:
与其为每个图像使用类,不如将它们存储在数组中,然后以编程方式进行更改。请查找下面的代码片段
让imgList=[
'https://dummyimage.com/200x200/000/fff&text=image+1',
'https://dummyimage.com/200x200/000/fff&text=image+2', 'https://dummyimage.com/200x200/000/fff&text=image+3', 'https://dummyimage.com/200x200/000/fff&text=image+4'
];
设currentIndex=0;
函数更改img(){
$('#body').css('backgroundImage','url(${imgList[currentIndex]})`);
currentIndex++;
如果(currentIndex==imgList.length)currentIndex=0;
}
changeImg();
$('#body')。在('click',changeImg')代码>
#主体{
宽度:200px;
高度:200px;
}
与其为每个图像使用类,不如将它们存储在一个数组中,然后以编程方式进行更改。请查找下面的代码片段
让imgList=[
'https://dummyimage.com/200x200/000/fff&text=image+1',
'https://dummyimage.com/200x200/000/fff&text=image+2', 'https://dummyimage.com/200x200/000/fff&text=image+3', 'https://dummyimage.com/200x200/000/fff&text=image+4'
];
设currentIndex=0;
函数更改img(){
$('#body').css('backgroundImage','url(${imgList[currentIndex]})`);
currentIndex++;
如果(currentIndex==imgList.length)currentIndex=0;
}
changeImg();
$('#body')。在('click',changeImg')代码>
#主体{
宽度:200px;
高度:200px;
}
这里是另一种方法(像您那样使用类):
var bodyObj=document.getElementById('body'),
指数=0,
类名=[
“imageOne”,
"图像二",,
"三",,
“图像四”
];
bodyObj.onclick=函数(e){
索引=(索引+1)%className.length;
e、 currentTarget.className=类名[索引];
}
html,正文,#正文{边距:0;高度:100%;宽度:100%;}
#正文{背景位置:中心;背景大小:封面;}
#body.imageOne{背景图像:url(“https://picsum.photos/id/9/536/354"); }
#body.imageTwo{背景图像:url(“https://picsum.photos/id/3/536/354"); }
#body.imageThree{背景图像:url(“https://picsum.photos/id/1/536/354"); }
#body.imageFour{背景图像:url(“https://picsum.photos/id/2/536/354);}
这里是另一种方法(像您一样使用类):
var bodyObj=document.getElementById('body'),
指数=0,
类名=[
“imageOne”,
"图像二",,
"三",,
“图像四”
];
bodyObj.onclick=函数(e){
索引=(索引+1)%className.length;
e、 currentTarget.className=类名[索引];
}
html,正文,#正文{边距:0;高度:100%;宽度:100%;}
#正文{背景位置:中心;背景大小:封面;}
#body.imageOne{背景图像:url(“https://picsum.photos/id/9/536/354"); }
#body.imageTwo{背景图像:url(“https://picsum.photos/id/3/536/354"); }
#body.imageThree{背景图像:url(“https://picsum.photos/id/1/536/354"); }
#body.imageFour{背景图像:url(“https://picsum.photos/id/2/536/354);}
我试过这个,效果很好
var index = 0;
var className = ['imageOne',
'imageTwo',
'imageThree',
'imageFour',
'imageFive'];
$(document).ready(function() {
$("#main").on("click", function() {
index = (index + 1) % className.length;
$(this).attr('class', className[index]);
});
});
我试过这个,效果很好
var index = 0;
var className = ['imageOne',
'imageTwo',
'imageThree',
'imageFour',
'imageFive'];
$(document).ready(function() {
$("#main").on("click", function() {
index = (index + 1) % className.length;
$(this).attr('class', className[index]);
});
});
html, body, #body {
height: 100%;
width: 100%;
}
#body.imageOne {
background-image: url("img1.png");
}
#body.imageTwo {
background-image: url("img2.png");
}
#body.imageTwo {
background-image: url("img3.png");
}
#body.imageTwo {
background-image: url("img4.png");
}
var index = 0;
var className = ['imageOne',
'imageTwo',
'imageThree',
'imageFour',
'imageFive'];
$(document).ready(function() {
$("#main").on("click", function() {
index = (index + 1) % className.length;
$(this).attr('class', className[index]);
});
});