javascript - 具有不透明度的重叠元素并处理这些元素上的 'hover'

标签 javascript jquery html css opacity

这是一个关于如何使用 JS 解决方案处理重叠元素的不透明度并使其在悬停时保持一致的问答。

要求

需求是开发两个元素,透明重叠,如下图两个红框。这些需要是透明的,以便背景内容可见。

enter image description here

现在,将鼠标悬停在这些元素中的任何一个上时,特定元素应该变成不透明,如下所示。

enter image description here

最佳答案

有一个纯 CSS 解决方案,这使得它更高效。例如:

body {
  background-image: linear-gradient(45deg, transparent 50%, #aaa 75%);
  background-size: 20px 20px;
}

#a {
  position: absolute;
  width: 150px;
  height: 150px;
  top: 50px;
  left: 50px;
  background: rgba(255, 0, 0, 1);
}

#b {
  position: absolute;
  width: 150px;
  height: 150px;
  top: 125px;
  left: 125px;
  background: rgba(255, 0, 0, 1);
}

#wrapper {
  opacity: 0.5;
}

/* instead of changing the classes,  
you can use selectors like this:*/

#wrapper:hover #a:hover,
#wrapper:hover #b:hover {
  opacity: 1;
  z-index: 10;
}

#wrapper:hover {
  opacity: 1;
}

#wrapper:hover #b,
#wrapper:hover #a {
  opacity: 0.5;
  z-index: -1;
}
<div id=wrapper>
  <div id="a">
  </div>
  <div id="b">
  </div>
</div>

关于javascript - 具有不透明度的重叠元素并处理这些元素上的 'hover',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49900770/

相关文章:

jquery - 鼠标悬停/图像交换问题

javascript - 从下拉列表中选择选项并使用 nodejs 提交请求

javascript - 将图像拆分为像素 div

javascript - 错误: [$injector:nomod] when attempting to add 3rd party module to mean app.

Javascript 如何引导用户完成使用流程

javascript - 如何将 reactjs 文件与 css 连接

javascript - 谷歌地图 API v3 : Adding custom undo/redo function for edited Polygon

javascript - 提交前更改表单的键名、值和操作 URL

php - 阻止用户未登录时访问

javascript - 从 PHP 返回 JS 源代码