javascript - 将鼠标悬停在图像上时如何从页面中删除内容。每条内容都有自己的id
问题描述
问题儿童 工作儿童我的 html 页面上有 4 个不同的图像。当我将鼠标悬停在每个图像上时,会出现一个文本框。问题是其中一个框显示的内容覆盖了具有 100% 不透明度的图像,使得文本无法阅读。我的解决方案是写一些 JS 说
如果我将鼠标悬停在图像 2 上,则图像 3 和 4 的不透明度变为 0
我不是经验丰富的编码员,所以我确信一个简单的解决方案可以解决我的问题。
解决方案
将此类添加到您的 css 文件中
.hide {
opacity: 0!important;
}
将 id 选择器添加到您的 html img 标签
<img src="/..." id="imageOneID"/>
<img src="/..." id="imageTwoID"/>
<img src="/..." id="imageThreeID"/>
<img src="/..." id="imageFourID"/>
这就是javascript
// create an array of your images
const images = [
document.getElementById('imageOneID'),
document.getElementById('imageTwoID'),
document.getElementById('imageThreeID'),
document.getElementById('imageFourID')
];
// show the hovered image and hide others
images.forEach((singleImage,index) => {
singleImage.addEventListener('mouseover', ()=>{
singleImage.classList.remove('hide');
images.forEach((_singleImage, _index) => {
if (_index !== index) {
_singleImage.classList.add('hide');
}
})
})
})
推荐阅读
- reactjs - 我的状态有一个值,我想传递到另一个页面
- node.js - 如何使用 Nodejs 导入 Excel 文件?
- python - NameError:名称'url_data'未定义
- java - 有效的jsonpath给出UnsupportedOperationException,CriteriaType无法解析
- html - 如何使用引导程序将导航栏中的图标居中
- python - 任何人都可以帮助解决以下代码中的 TypeError:'float' object is not callable'
- vba - 如何为 Outlook 2010 启用外出 (OOO)?
- jenkins - 上一阶段成功后触发下一阶段流水线
- python - 从序列化程序中的模型获取自动生成的字段,而不返回它
- ios - 使用未解析的标识符“SDKApplicationDelegate”