javascript - 使用绝对定位更改另一个 div 悬停时的 div 外观(不重复)

标签 javascript jquery html css

我有几个 div,其绝对位置位于具有相对位置 (duh) 的主要 div 之上。当将另一个 div 悬停在同一父 div 中时,我试图让一个 div 更改其背景颜色等。

现在,我知道 adjacent-div 类,但它们似乎不起作用(可能是因为绝对定位)。

下面是我的代码示例(实际要大得多)。最好的改变方式是什么?悬停在 m2wrap-hover 上时的 m2wrap-back 宽度和颜色(100% 覆盖在其他 div 上)?

附言如果 CSS 本身不是一个选项,jQuery 解决方案也可以。

<div class="m2wrap">
  <div class="m2wrap-back">
    <h3 class="m2wrap-back-title">Title</h3>
  </div>
  <h3 class="xhfb-text"> Some text here.. </h3>
  <div class="m2wrap-bz1"></div>
  <div class="m2wrap-bz2"></div>
  <div class="m2wrap-hover"></div>
</div>
<style>
.m2wrap {
    position: relative
}
.m2wrap-back {
    position: absolute;
    top: 15px;
    left: 0;
    width: 110px;
    height: 0;
    text-align: center;
    vertical-align: middle;
}
.m2wrap-hover {
    position: absolute;
    width: 100%;
    height: 100%;
    z-index: 2;
    border-radius: 4px;
    opacity: 0.6;
    cursor: pointer;
}
div.m2wrap-hover:hover {
    background-color: #bf0000;
}
</style>

最佳答案

你不能用纯 css 和你当前的 html 结构来做,需要 javascipt 或 jquery 来做。

示例:

$('.m2wrap-hover').hover(function() {
  $(this).closest('.m2wrap').find('.m2wrap-back').addClass('hover');
}, function() {
  $(this).closest('.m2wrap').find('.m2wrap-back').removeClass('hover');
})
.m2wrap {
    position: relative
}
.m2wrap-back {
    position: absolute;
    top: 15px;
    left: 0;
    width: 110px;
    height: 0;
    text-align: center;
    vertical-align: middle;
}
.m2wrap-hover {
    position: absolute;
    width: 100%;
    height: 100%;
    z-index: 2;
    border-radius: 4px;
    opacity: 0.6;
    cursor: pointer;
}
div.m2wrap-hover:hover {
    background-color: #bf0000;
}
.m2wrap-back.hover {
  width: 120px;
  color: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="m2wrap">
  <div class="m2wrap-back">
    <h3 class="m2wrap-back-title">Title</h3>
  </div>
  <h3 class="xhfb-text"> Some text here.. </h3>
  <div class="m2wrap-bz1"></div>
  <div class="m2wrap-bz2"></div>
  <div class="m2wrap-hover">hover here</div>
</div>

或者如果你只想使用 css,你需要改变你的元素的顺序(因为它有 position: absolute 所以顺序无关紧要):

.m2wrap {
    position: relative
}
.m2wrap-back {
    position: absolute;
    top: 15px;
    left: 0;
    width: 110px;
    height: 0;
    text-align: center;
    vertical-align: middle;
}
.m2wrap-hover {
    position: absolute;
    width: 100%;
    height: 100%;
    z-index: 2;
    border-radius: 4px;
    opacity: 0.6;
    cursor: pointer;
}
div.m2wrap-hover:hover {
    background-color: #bf0000;
}

.m2wrap-hover:hover + .m2wrap-back {
    width: 120px;
    color: red;
}
<div class="m2wrap">
  <h3 class="xhfb-text"> Some text here.. </h3>
  <div class="m2wrap-bz1"></div>
  <div class="m2wrap-bz2"></div>
  <div class="m2wrap-hover">hover here</div>
  <div class="m2wrap-back">
    <h3 class="m2wrap-back-title">Title</h3>
  </div>
</div>

关于javascript - 使用绝对定位更改另一个 div 悬停时的 div 外观(不重复),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57431760/

相关文章:

javascript - 如何使用 JQuery 更改 EJS 中的单个元素

javascript - AngularJS 表格过滤器

javascript - 如何添加 printThis() 元素异常?

javascript - 使用 Javascript 添加日期

javascript - D3 中的中断退出转换

jquery - Prettyphoto 插件升级到 jQuery 3.1.1 后停止工作

html - 在 IE7/8 中从外部目标拖动时防止文本选择

javascript - 为什么要在 angularjs 中将函数注册为服务?

javascript - requestAnimationFrame 在图像在屏幕外移动时表现得很奇怪

javascript - 如何验证至少已选择这些单选按钮之一?