单击项目以更改另一个项目的背景图像
Click on item to change background image of another
尝试使用javascript创建一个"按钮",使用悬停在li项目上,这将改变另一个li项目的背景(phoneChange)。这些项目围绕着手机的图像,其中包含我想在将鼠标悬停在相关 li 项目上时更改的屏幕截图。我一直在这里寻找类似的例子,但找不到任何适合我的东西。
<ol class="list-b">
<li class="firstPhone"><span class="title">First Box</span> Lorem ipsum sit dolor.</li>
<li class="secondPhone"><span class="title">Second Box</span> Lorem ipsum sit dolor.</li>
<li class="thirdPhone"><span class="title">Third Box</span> Lorem ipsum sit dolor.</li>
<li class="fourthPhone"><span class="title">Fourth Box</span> Lorem ipsum sit dolor.</li>
<li class="fifthPhone"><span class="title">Fifth Box</span> Lorem ipsum sit dolor.</li>
<li class="sixthPhone"><span class="title">Sixth Box</span> Lorem ipsum sit dolor.</li>
<li id="phoneChange" class="firstPhone"></li>
</ol>
.CSS
.firstPhone{
background: url(../images/mobile1.png) }
.secondPhone{
background: url(../images/mobile2.png) }
.thirdPhone{
background: url(../images/mobile3.png)}
.fourthPhone{
background: url(../images/mobile4.png)}
.fifthPhone{
background: url(../images/mobile5.png)}
.sixthPhone{
background: url(../images/mobile6.png)}
为每个"按钮"添加一些标识class
(为了简单起见,我称之为按钮):
<ol class="list-b">
<li class="button firstPhone"><span class="title">First Box</span> Lorem ipsum sit dolor.</li>
<li class="button secondPhone"><span class="title">Second Box</span> Lorem ipsum sit dolor.</li>
<li class="button thirdPhone"><span class="title">Third Box</span> Lorem ipsum sit dolor.</li>
<li class="button fourthPhone"><span class="title">Fourth Box</span> Lorem ipsum sit dolor.</li>
<li class="button fifthPhone"><span class="title">Fifth Box</span> Lorem ipsum sit dolor.</li>
<li class="button sixthPhone"><span class="title">Sixth Box</span> Lorem ipsum sit dolor.</li>
<li id="phoneChange"></li>
</ol>
将事件处理程序附加到这些元素:
var buttons = document.getElementsByClassName('button');
for (var i = 0; i < buttons.length; i++) {
buttons[i].addEventListener('click', function() {
document.getElementById('phoneChange').css.background = this.css.background;
});
}
使用卧底单选按钮的纯 CSS 解决方案:
html {
width: 100%;
height: 100%;
background: honeydew;
}
input {
display: none;
}
.item {
cursor: pointer;
user-select: none;
-webkit-user-select: none;
}
#target {
position: absolute;
display: inline-block;
height: 176px;
width: 300px;
border: 2px solid crimson;
left: calc(50% - 150px);
top: 10px;
}
#click:checked ~ #target {
background: url('http://i.imgur.com/3jxqrKP.jpg');
background-repeat: no-repeat;
background-size: 100% 100%;
}
#click2:checked ~ #target {
background: url('http://i.imgur.com/23lydbM.jpg');
background-repeat: no-repeat;
background-size: 100% 100%;
}
<input type="radio" id="click" name="click" value="click" />
<input type="radio" id="click2" name="click" value="click2" />
<label for="click">
<li class="firstPhone item">click A</li>
</label>
<label for="click2">
<li class="secondPhone item">click B</li>
</label>
<div id=target></div>
你可以试试这个:
<ol id="phoneList" class="list-b">
<li onmouseover="phoneChanged('firstPhone');"><span class="title">First Box</span> Lorem ipsum sit dolor.</li>
<li onmouseover="phoneChanged('secondPhone');"><span class="title">Second Box</span> Lorem ipsum sit dolor.</li>
<li onmouseover="phoneChanged('thirdPhone');"><span class="title">Third Box</span> Lorem ipsum sit dolor.</li>
<li onmouseover="phoneChanged('fourthPhone');"><span class="title">Fourth Box</span> Lorem ipsum sit dolor.</li>
<li onmouseover="phoneChanged('fifthPhone');"><span class="title">Fifth Box</span> Lorem ipsum sit dolor.</li>
<li onmouseover="phoneChanged('sixthPhone');"><span class="title">Sixth Box</span> Lorem ipsum sit dolor.</li>
<li id="phoneChange"></li>
</ol>
和代码:
<script>
function phoneChanged(className) {
document.getElementById('phoneChange').className = className;
}
</script>
此代码对鼠标悬停在每个项目上做出反应。要对点击做出反应,请将"鼠标悬停"替换为"点击"。
相关文章:
- 使用JS如何动态更改显示的html文件中的文本背景颜色
- 正在将数据主题添加到所有项目
- 在BootStrap菜单栏中为所选项目设置背景,类似于BootStrap中的父导航选项
- 使用圆中的数组更改背景颜色项目
- 单击项目以更改另一个项目的背景图像
- 加载所有项目后更新背景
- 如何在项目符号列表中插入背景(图片)元素,其 URL 写入相邻的项目符号列表中
- 在导航中对活动项目背景进行动画处理失败
- Foreach项目悬停在不同的背景上
- 如何更改项目's的背景颜色使用jquery平滑
- HTML选择选项多次更改选中的项目灰色背景时,不在焦点上
- 如何改变所有选定的项目背景/类,而不是只改变一个选定的项目在ng-repeat角
- 当项目被点击改变背景颜色- CSS(没有jQuery)
- 如何使用jQuery设置每个项目的背景颜色到下一个项目
- 如何设置重复器项目背景颜色
- 当弹出框处于活动状态时,如何在离子中与背景中的项目进行交互
- 无法更改 UL 中所选 li 项目的背景颜色
- 如何更改表中项目的背景
- 如何动态地添加一个类到li项目,并使用javascript和css改变它的背景颜色
- 设置悬停项目的背景