javascript - 将图像分配给不同的按钮
问题描述
如何在单击每个不同按钮时更改图片并将显示的图片设为默认图片?
从代码片段中,请注意每个按钮应如何更改图像。
谢谢!
* {
box-sizing: border-box;
}
.imagebox {
width: 50%;
float: left;
height: 300px;
position: relative;
}
.imagebox img {
position: relative;
top: 40%;
transform: translateY(-50%);
overflow: hidden;
width: 100%;
}
.textbox-cont {
width: 50%;
height: 300px;
float: right;
position: relative;
overflow: hidden;
}
.textbox {
color: #000000;
position: absolute;
text-align: center;
width: 100%;
padding: 20px;
top: 53%;
transform: translateY(-50%);
}
@media only screen and (max-width: 700px) {
.imagebox,
.textbox-cont {
width: 100%;
display: flex;
flex-direction: column;
}
<div class="imagebox"></div>
<img src="https://s.ftcdn.net/v2013/pics/all/curated/RKyaEDwp8J7JKeZWQPuOVWvkUjGQfpCx_cover_580.jpg?r=1a0fc22192d0c808b8bb2b9bcfbf4a45b1793687" alt="">
<div class="textbox-cont">
<div class="textbox">
Pick a color to see how it will look<br>
<button style="margin: 8px;">Cobalt Blue</button><br>
<button style="margin: 8px">Cobaltt Blue</button><br>
<button style="margin: 8px">Cardinal Red</button><br>
<button style="margin: 8px">Coral Orange</button><br>
</div>
</div>
解决方案
您可以将EventListener添加到每个按钮并根据按钮更改图像src
属性id
:
const img = document.getElementById('img');
for (let button of document.getElementsByTagName('button')) {
button.addEventListener('click', () => {
switch (button.id) {
case 'but1':
img.src = "https://s.ftcdn.net/v2013/pics/all/curated/RKyaEDwp8J7JKeZWQPuOVWvkUjGQfpCx_cover_580.jpg?r=1a0fc22192d0c808b8bb2b9bcfbf4a45b1793687";
break;
case 'but2':
img.src = "https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcTgfERcK1BcdgSekipirrCw6zpQJ9l40h48Musvp7-dfy8Ym3Yl&usqp=CAU";
break;
case 'but3':
img.src = "https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcS9wvzviWw6ag6p_aZgw6kRs267_3jpiI_gGCxvLh7U9QIVAmQa&usqp=CAU";
break;
case 'but4':
img.src = "https://xzdl43v0mdf2m45tz2aj7kkv35-wpengine.netdna-ssl.com/wp-content/uploads/2010/10/orange-780x400.jpg";
break;
}
});
}
*{
box-sizing:border-box;
}
.imagebox {
width: 50%;
float: left;
height:300px;
position:relative;
}
.imagebox img{
position:relative;
top:40%;
transform:translateY(-50%);
overflow:hidden;
width: 100%;
}
.textbox-cont {
width: 50%;
height:300px;
float: right;
position:relative;
overflow:hidden;
}
.textbox{
color: #000000;
position:absolute;
text-align: center;
width:100%;
padding:20px;
top:53%;
transform:translateY(-50%);
}
@media only screen and (max-width: 700px) {
.imagebox,
.textbox-cont {
width: 100%;
display: flex;
flex-direction: column;
}
<div class="imagebox">
<img id="img" src="https://s.ftcdn.net/v2013/pics/all/curated/RKyaEDwp8J7JKeZWQPuOVWvkUjGQfpCx_cover_580.jpg?r=1a0fc22192d0c808b8bb2b9bcfbf4a45b1793687"
alt="">
</div>
<div class="textbox-cont">
<div class="textbox">
Pick a color to see how it will look<br>
<button style="margin: 8px" id="but1">Cobalt Blue</button><br>
<button style="margin: 8px" id="but2">Cobaltt Blue</button><br>
<button style="margin: 8px" id="but3">Cardinal Red</button><br>
<button style="margin: 8px" id="but4">Coral Orange</button><br>
</div>
</div>
推荐阅读
- c# - 在子列表框项 WPF 悬停时删除父列表框项上的悬停效果
- java - 尝试更新进度条
- mysql - Excel Vba从mysql数据库中删除行的问题
- ocaml - 解析ocaml时通过eol分隔列表
- python - 为什么`numpy.ndarray.view`会忽略之前对`numpy.ndarray.newbyteorder`的调用?
- node.js - Mongoose - 在中间件中找不到模型方法
- reinforcement-learning - 了解游戏 2048 示例的强化学习
- c# - C#如何让应用程序接收我的输入
- google-cloud-sql - 能够连接,尽管授权网络中没有外部 IP / 网络?
- opencart - Opencart 2.3.0.2分类foreach产品作为产品克隆无限制