You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

实现网页鼠标悬停显示描述效果(CSS/JavaScript)

实现鼠标悬停显示描述的完整优化方案

嘿,看你已经有了实现鼠标悬停显示描述的基础代码,我帮你把它补全并优化,让效果更流畅贴合需求~

首先,先放完整的可运行代码,包含HTML和CSS:

<!DOCTYPE html>
<html>
<head>
    <style>
        @import url('https://fonts.googleapis.com/css?family=Ubuntu');
        a.box {
            color: #fff;
            background-color: #2f9b9b;
            display: block;
            width: 400px;
            height: 250px;
            overflow: hidden;
            position: relative;
            cursor: pointer;
            font-family: 'Ubuntu', sans-serif;
            text-decoration: none;
        }
        .content {
            position: absolute;
            bottom: 0;
            right: 0;
            top: 0;
            margin: auto;
            padding: 0 4em;
            z-index: 2;
            height: 3em;
            transition: height 0.5s ease;
            display: flex;
            align-items: center;
            background: rgba(0,0,0,0.3);
        }
        a.box:hover .content {
            height: 100%;
            padding: 2em 4em;
        }
        .content h3 {
            margin: 0 0 1em 0;
            opacity: 0;
            transition: opacity 0.3s ease 0.2s;
        }
        .content p {
            margin: 0;
            opacity: 0;
            transition: opacity 0.3s ease 0.3s;
        }
        a.box:hover .content h3,
        a.box:hover .content p {
            opacity: 1;
        }
    </style>
</head>
<body>
    <a href="#" class="box">
        <div class="content">
            <div>
                <h3>标题示例</h3>
                <p>这是鼠标悬停后显示的详细描述内容,可以根据需求替换成你想要的文本哦~</p>
            </div>
        </div>
    </a>
</body>
</html>

接下来给你拆解关键部分,方便你理解和调整:

  • 基础容器设置:a.box作为主容器,用overflow: hidden确保悬停时超出的内容不显示,position: relative是为了让内部的.content能基于它定位。
  • 内容区域过渡:.content的height从初始的3em(只显示一小部分),悬停时变为100%,配合transition实现平滑的展开动画,同时加了半透明背景让文本更清晰。
  • 文本渐显效果:标题和段落初始opacity: 0,悬停时延迟一点时间再变成opacity: 1,这样动画更有层次感,不会显得突兀。
  • 排版优化:用display: flex和align-items: center让内容在垂直方向居中,悬停时增加内边距,提升阅读体验。

如果你想调整动画速度,只需要修改transition里的0.5s(高度变化速度)和0.3s(文本渐显速度)就行;要是想改背景色或者文本样式,直接对应修改颜色值和字体属性就好~

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 07:08:22