html/css实现鼠标hover移入时的卡片显示动画效果

以下代码来自于国内直连GPT/Claude

1.鼠标移入红色圆圈,白色卡片过渡动画显示

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Hover Card Animation</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <div class="icon-container">
        <div class="icon">⭐</div>
        <div class="card">
            <p>套餐:免费</p>
            <ul>
                <li>标准查询:36</li>
                <li>高级查询:0</li>
                <li>高级积分:0</li>
            </ul>
            <p>升级解锁专属特权:</p>
            <ul>
                <li>无限畅享 GPT-4</li>
                <li>DALL-E 3 驱动的智能绘画</li>
                <li>一键总结网页 & YouTube 视频</li>
                <li>更多高级功能</li>
            </ul>
        </div>
    </div>
</body>
<style>
    body {
    font-family: Arial, sans-serif;
    margin: 0;
    padding: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    background-color: #f9f9f9;
}

.icon-container {
    position: relative;
    display: inline-block;
    cursor: pointer;
}

.icon {
    font-size: 30px;
    background-color: #ffcc00;
    padding: 10px;
    border-radius: 50%;
    text-align: center;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}

.card {
    position: absolute;
    top: -320px;
    left: 50%;
    transform: translateX(-50%);
    width: 250px;
    background-color: #fff;
    border-radius: 10px;
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.3s ease-in-out, transform 0.3s ease-in-out;
    padding: 20px;
    box-sizing: border-box;
}

.card p {
    margin: 0 0 10px;
    font-weight: bold;
}

.card ul {
    list-style: none;
    padding: 0;
    margin: 0;
}

.card ul li {
    margin: 5px 0;
}

.icon-container:hover .card {
    opacity: 1;
    visibility: visible;
    transform: translateX(-50%) translateY(10px);
}

</style>
</html>

2.优化一下,当鼠标移动到卡片上时,卡片不要隐藏消失,并且增加结束过渡效果

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Hover Card Animation</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <div class="icon-container">
        <div class="icon">⭐</div>
        <div class="card">
            <p>套餐:免费</p>
            <ul>
                <li>标准查询:36</li>
                <li>高级查询:0</li>
                <li>高级积分:0</li>
            </ul>
            <p>升级解锁专属特权:</p>
            <ul>
                <li>无限畅享 GPT-4</li>
                <li>DALL-E 3 驱动的智能绘画</li>
                <li>一键总结网页 & YouTube 视频</li>
                <li>更多高级功能</li>
            </ul>
        </div>
    </div>
</body>
<style>
body {
    font-family: Arial, sans-serif;
    margin: 0;
    padding: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    background-color: #f9f9f9;
}

.icon-container {
    position: relative;
    display: inline-block;
    cursor: pointer;
}

.icon {
    font-size: 30px;
    background-color: #ffcc00;
    padding: 10px;
    border-radius: 50%;
    text-align: center;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}

.card {
    position: absolute;
    top: 50px;
    left: 50%;
    transform: translateX(-50%) translateY(10px);
    width: 250px;
    background-color: #fff;
    border-radius: 10px;
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.3s ease-in-out, transform 0.3s ease-in-out, visibility 0s linear 0.3s;
    padding: 20px;
    box-sizing: border-box;
    pointer-events: none; /* Prevent accidental interaction when hidden */
}

.card p {
    margin: 0 0 10px;
    font-weight: bold;
}

.card ul {
    list-style: none;
    padding: 0;
    margin: 0;
}

.card ul li {
    margin: 5px 0;
}

/* When hovering over icon or card */
.icon-container:hover .card,
.icon-container .card:hover {
    opacity: 1;
    visibility: visible;
    transform: translateX(-50%) translateY(10px);
    pointer-events: auto; /* Allow interaction when visible */
    transition: opacity 0.3s ease-in-out, transform 0.3s ease-in-out, visibility 0s;
}

/* When hiding the card */
.icon-container:not(:hover) .card {
    opacity: 0;
    visibility: hidden;
    transform: translateX(-50%) translateY(0);
    pointer-events: none;
    transition: opacity 0.3s ease-in-out, transform 0.3s ease-in-out, visibility 0s linear 0.3s;
}


</style>
</html>
相关推荐
岳轩子27 分钟前
使用idea创建一个JAVA WEB项目
java·前端·intellij-idea
一雨方知深秋1 小时前
VueCli自定义创建项目
前端·javascript·eslint·router·vuecli
杰夫贾维斯1 小时前
ruoyi Cannot find module ‘@/views/system/user/index‘
前端
brrdg_sefg1 小时前
前端html,vue使用第三方地图详细教程,以百度地图为例,实现地图标注,导航,定位,路线规划,坐标转换
前端·vue.js·html
不会玩技术的技术girl1 小时前
HTML技术贴:深入理解与实践
前端·html
小林熬夜学编程2 小时前
【Linux网络编程】第十弹---打造初级网络计算器:从协议设计到服务实现
linux·服务器·c语言·开发语言·前端·网络·c++
打野赵怀真2 小时前
你配置过Webpack optimize吗?
前端·javascript
打野赵怀真2 小时前
在webpack中,什么是chunks?
前端·javascript
web150854159352 小时前
基于YOLOv的目标追踪与无人机前端查看系统开发
前端·无人机
RoyLin2 小时前
现代工作流引擎
前端·人工智能·后端