You need to enable JavaScript to run this app. hover其他元素时CSS Transition未触发问题求助
优惠活动
大模型
产品
解决方案
定价
更多

hover其他元素时CSS Transition未触发问题求助

问题根因

  1. 你使用的相邻兄弟选择器+只能匹配紧跟在当前元素后的第一个同级元素,你的.image元素后第一个同级是岛屿图的<img>标签,不是.bar元素,导致hover触发的样式规则完全无法命中目标元素。
  2. transition属性写在hover伪类规则中时,只有鼠标悬停状态下才会加载过渡配置,鼠标移出时会没有过渡效果直接复位,建议写在元素的默认样式规则中。

修改方案

将CSS中相邻兄弟选择器替换为通用兄弟选择器~,同时把transition移动到.bar的默认样式里即可:

修改后CSS代码

div.invis_container {
    margin: 0 0;
    padding: 0 0;
    background: transparent;
    border: none;
    position: relative;
    width: 0;
    height: 0;
}

.image {
    width: 750px;
    height: 750px;
    position: absolute;
}

.bar {
    background-color: rgb(0, 37, 107);
    width: 50px;
    height: 10px;
    position: absolute;
    transition: all 0.8s ease;
}

.image:hover ~ .bar {
    height: 200px;
}

可选优化(HTML规范)

建议给HTML标签的属性值加上引号,避免特殊场景下解析异常:

<script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script>
<div class="invis_container">
    <img src="assets/paint_green_blue.png" class="image">
    <img src="assets/island.png" style="width: 200px; height: 200px; opacity: 0.3; position: absolute;
    top: 150px; left: 350px;">
    <div class="bar"></div>
    <h3 style="font-size: 40px; width: 1000px; position: absolute; top: 400px; left: 400px;">
        Test text
    </h3>
</div>

内容的提问来源于stack exchange,提问作者ShadeOfLight

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.19 12:36:03