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>
相关推荐
十一.36616 分钟前
79-82 call和apply,arguments,Date对象,Math
开发语言·前端·javascript
霍格沃兹测试开发学社-小明21 分钟前
测试左移2.0:在开发周期前端筑起质量防线
前端·javascript·网络·人工智能·测试工具·easyui
用户990450177800923 分钟前
若依工作流-包含网关
前端
by__csdn34 分钟前
Vue 中计算属性、监听属性与函数方法的区别详解
前端·javascript·vue.js·typescript·vue·css3·html5
on_pluto_1 小时前
【debug】关于如何让电脑里面的两个cuda共存
linux·服务器·前端
r***F2621 小时前
Go-Gin Web 框架完整教程
前端·golang·gin
chilavert3181 小时前
技术演进中的开发沉思-220 Ajax:XMLHttpRequest 对象
前端·javascript
IT_陈寒1 小时前
Python开发者必看:5个被低估但能提升200%编码效率的冷门库实战
前端·人工智能·后端
g***78912 小时前
鸿蒙NEXT(五):鸿蒙版React Native架构浅析
android·前端·后端
q***71852 小时前
Webpack、Vite区别知多少?
前端·webpack·node.js