HTML实战:爱心图的实现

设计思路

  • 使用纯CSS创建多种风格的爱心

  • 添加平滑的动画效果

  • 实现交互式爱心生成器

  • 响应式设计适应不同设备

  • 优雅的UI布局和色彩方案

  • <!DOCTYPE html>
    <html lang="zh-CN">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>HTML爱心图实战</title>
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
        <style>
            * {
                margin: 0;
                padding: 0;
                box-sizing: border-box;
                font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            }
            
            body {
                background: linear-gradient(135deg, #ffafbd, #ffc3a0);
                color: #5a2a2a;
                min-height: 100vh;
                padding: 20px;
                display: flex;
                flex-direction: column;
                align-items: center;
            }
            
            header {
                text-align: center;
                padding: 30px 0;
                width: 100%;
                max-width: 1200px;
            }
            
            h1 {
                font-size: 2.8rem;
                margin-bottom: 10px;
                text-shadow: 2px 2px 4px rgba(0,0,0,0.1);
                color: #7a1f1f;
                position: relative;
                display: inline-block;
            }
            
            h1::after {
                content: "";
                position: absolute;
                bottom: -10px;
                left: 50%;
                transform: translateX(-50%);
                width: 120px;
                height: 4px;
                background: linear-gradient(90deg, transparent, #ff5e7d, transparent);
                border-radius: 2px;
            }
            
            .subtitle {
                font-size: 1.2rem;
                max-width: 700px;
                margin: 20px auto;
                line-height: 1.6;
                color: #5a2a2a;
            }
            
            .container {
                display: flex;
                flex-wrap: wrap;
                justify-content: center;
                gap: 40px;
                max-width: 1200px;
                margin: 20px auto;
            }
            
            .card {
                background: rgba(255, 255, 255, 0.85);
                border-radius: 20px;
                box-shadow: 0 10px 30px rgba(150, 50, 50, 0.2);
                padding: 25px;
                width: 100%;
                max-width: 500px;
                transition: transform 0.3s ease;
            }
            
            .card:hover {
                transform: translateY(-10px);
            }
            
            .card h2 {
                text-align: center;
                margin-bottom: 20px;
                color: #d83f5d;
                display: flex;
                align-items: center;
                justify-content: center;
                gap: 10px;
            }
            
            .heart-container {
                display: flex;
                justify-content: center;
                align-items: center;
                min-height: 300px;
                margin: 20px 0;
            }
            
            /* 方法1:使用伪元素创建爱心 */
            .heart-1 {
                width: 100px;
                height: 100px;
                background-color: #ff5e7d;
                position: relative;
                transform: rotate(-45deg);
                animation: beat-1 1.2s infinite;
            }
            
            .heart-1::before,
            .heart-1::after {
                content: "";
                width: 100px;
                height: 100px;
                background-color: #ff5e7d;
                border-radius: 50%;
                position: absolute;
            }
            
            .heart-1::before {
                top: -50px;
                left: 0;
            }
            
            .heart-1::after {
                top: 0;
                left: 50px;
            }
            
            /* 方法2:使用border-radius创建爱心 */
            .heart-2 {
                width: 100px;
                height: 100px;
                background-color: #ff3366;
                position: relative;
                animation: beat-2 1.4s infinite;
            }
            
            .heart-2::before,
            .heart-2::after {
                content: "";
                position: absolute;
                width: 100px;
                height: 100px;
                background-color: #ff3366;
                border-radius: 50%;
            }
            
            .heart-2::before {
                top: 0;
                left: -50px;
            }
            
            .heart-2::after {
                top: -50px;
                left: 0;
            }
            
            /* 方法3:使用CSS clip-path创建爱心 */
            .heart-3 {
                width: 100px;
                height: 90px;
                background-color: #ff1493;
                clip-path: path("M10,30 A20,20,0,0,1,50,30 A20,20,0,0,1,90,30 Q90,60,50,90 Q10,60,10,30 Z");
                animation: beat-3 1.6s infinite;
            }
            
            /* 方法4:使用SVG创建爱心 */
            .heart-4 {
                width: 100px;
                height: 100px;
                animation: beat-4 1.8s infinite;
            }
            
            .heart-4 svg {
                width: 100%;
                height: 100%;
            }
            
            /* 方法5:文字爱心 */
            .heart-5 {
                font-size: 100px;
                color: #ff5e7d;
                text-shadow: 0 0 20px rgba(255, 0, 85, 0.4);
                animation: beat-5 2s infinite;
            }
            
            /* 动画效果 */
            @keyframes beat-1 {
                0%, 100% { transform: rotate(-45deg) scale(1); }
                50% { transform: rotate(-45deg) scale(1.1); }
            }
            
            @keyframes beat-2 {
                0%, 100% { transform: scale(1); }
                50% { transform: scale(1.1); }
            }
            
            @keyframes beat-3 {
                0%, 100% { transform: scale(1); }
                50% { transform: scale(1.15); }
            }
            
            @keyframes beat-4 {
                0%, 100% { transform: scale(1); }
                50% { transform: scale(1.12); }
            }
            
            @keyframes beat-5 {
                0%, 100% { transform: scale(1); }
                50% { transform: scale(1.2); }
            }
            
            .code-block {
                background: #2d2d2d;
                color: #f8f8f2;
                border-radius: 10px;
                padding: 15px;
                margin-top: 20px;
                font-family: 'Courier New', monospace;
                font-size: 0.9rem;
                line-height: 1.5;
                overflow-x: auto;
                max-height: 200px;
                overflow-y: auto;
            }
            
            .generator {
                background: rgba(255, 255, 255, 0.85);
                border-radius: 20px;
                box-shadow: 0 10px 30px rgba(150, 50, 50, 0.2);
                padding: 30px;
                width: 100%;
                max-width: 800px;
                margin: 40px auto;
                text-align: center;
            }
            
            .controls {
                display: flex;
                flex-wrap: wrap;
                justify-content: center;
                gap: 20px;
                margin: 30px 0;
            }
            
            .control-group {
                display: flex;
                flex-direction: column;
                align-items: center;
            }
            
            label {
                margin-bottom: 8px;
                font-weight: 500;
                color: #7a1f1f;
            }
            
            input[type="range"] {
                width: 200px;
                accent-color: #ff5e7d;
            }
            
            input[type="color"] {
                width: 60px;
                height: 40px;
                border: none;
                border-radius: 8px;
                cursor: pointer;
            }
            
            .generated-heart {
                margin: 30px auto;
                width: 150px;
                height: 150px;
                background-color: #ff5e7d;
                position: relative;
                transform: rotate(-45deg);
            }
            
            .generated-heart::before,
            .generated-heart::after {
                content: "";
                position: absolute;
                width: 150px;
                height: 150px;
                background-color: inherit;
                border-radius: 50%;
            }
            
            .generated-heart::before {
                top: -75px;
                left: 0;
            }
            
            .generated-heart::after {
                top: 0;
                left: 75px;
            }
            
            button {
                background: #ff5e7d;
                color: white;
                border: none;
                padding: 12px 25px;
                border-radius: 50px;
                font-size: 1rem;
                font-weight: 600;
                cursor: pointer;
                transition: all 0.3s ease;
                margin: 10px;
                box-shadow: 0 4px 15px rgba(255, 94, 125, 0.4);
            }
            
            button:hover {
                background: #ff3366;
                transform: translateY(-3px);
                box-shadow: 0 7px 20px rgba(255, 94, 125, 0.6);
            }
            
            footer {
                text-align: center;
                padding: 30px 0;
                width: 100%;
                max-width: 1200px;
                color: #5a2a2a;
                font-size: 1rem;
                margin-top: auto;
            }
            
            @media (max-width: 768px) {
                .container {
                    flex-direction: column;
                    align-items: center;
                }
                
                .card {
                    max-width: 90%;
                }
                
                h1 {
                    font-size: 2.2rem;
                }
                
                .controls {
                    flex-direction: column;
                    align-items: center;
                }
            }
        </style>
    </head>
    <body>
        <header>
            <h1><i class="fas fa-heart"></i> HTML爱心图实战</h1>
            <p class="subtitle">探索使用纯CSS和HTML创建爱心的多种方法。从基础形状到高级技巧,学习如何实现各种风格的爱心及其动画效果。</p>
        </header>
        
        <div class="container">
            <div class="card">
                <h2><i class="fas fa-shapes"></i> 伪元素方法</h2>
                <div class="heart-container">
                    <div class="heart-1"></div>
                </div>
                <div class="code-block">
    /* 使用两个伪元素创建爱心 */
    .heart {
      width: 100px;
      height: 100px;
      background-color: #ff5e7d;
      position: relative;
      transform: rotate(-45deg);
    }

    .heart::before,
    .heart::after {
      content: "";
      width: 100px;
      height: 100px;
      background-color: #ff5e7d;
      border-radius: 50%;
      position: absolute;
    }

    .heart::before {
      top: -50px;
      left: 0;
    }

    .heart::after {
      top: 0;
      left: 50px;
    }</div>
            </div>
            
            <div class="card">
                <h2><i class="fas fa-border-style"></i> Border-radius方法</h2>
                <div class="heart-container">
                    <div class="heart-2"></div>
                </div>
                <div class="code-block">
    /* 使用border-radius创建爱心 */
    .heart {
      width: 100px;
      height: 100px;
      background-color: #ff3366;
      position: relative;
    }

    .heart::before,
    .heart::after {
      content: "";
      position: absolute;
      width: 100px;
      height: 100px;
      background-color: #ff3366;
      border-radius: 50%;
    }

    .heart::before {
      top: 0;
      left: -50px;
    }

    .heart::after {
      top: -50px;
      left: 0;
    }</div>
            </div>
            
            <div class="card">
                <h2><i class="fas fa-cut"></i> Clip-path方法</h2>
                <div class="heart-container">
                    <div class="heart-3"></div>
                </div>
                <div class="code-block">
    /* 使用CSS clip-path创建爱心 */
    .heart {
      width: 100px;
      height: 90px;
      background-color: #ff1493;
      clip-path: path("M10,30 A20,20,0,0,1,50,30 A20,20,0,0,1,90,30 Q90,60,50,90 Q10,60,10,30 Z");
    }</div>
            </div>
            
            <div class="card">
                <h2><i class="fas fa-code"></i> SVG方法</h2>
                <div class="heart-container">
                    <div class="heart-4">
                        <svg viewBox="0 0 32 29.6">
                            <path d="M23.6,0c-3.4,0-6.3,2.7-7.6,5.6C14.7,2.7,11.8,0,8.4,0C3.8,0,0,3.8,0,8.4c0,9.4,9.5,11.9,16,21.2
                            c6.1-9.3,16-12.1,16-21.2C32,3.8,28.2,0,23.6,0z" fill="#ff5e7d"/>
                        </svg>
                    </div>
                </div>
                <div class="code-block">
    <!-- 使用内联SVG创建爱心 -->
    &lt;svg viewBox="0 0 32 29.6"&gt;
      &lt;path d="M23.6,0c-3.4,0-6.3,2.7-7.6,5.6C14.7,2.7,11.8,0,8.4,0C3.8,0,0,3.8,0,8.4c0,9.4,9.5,11.9,16,21.2
      c6.1-9.3,16-12.1,16-21.2C32,3.8,28.2,0,23.6,0z" fill="#ff5e7d"/&gt;
    &lt;/svg&gt;</div>
            </div>
        </div>
        
        <div class="generator">
            <h2><i class="fas fa-magic"></i> 爱心生成器</h2>
            <p>自定义您的爱心:调整大小、颜色和动画速度</p>
            
            <div class="controls">
                <div class="control-group">
                    <label for="size">尺寸: <span id="size-value">150px</span></label>
                    <input type="range" id="size" min="50" max="300" value="150">
                </div>
                
                <div class="control-group">
                    <label for="color">颜色</label>
                    <input type="color" id="color" value="#ff5e7d">
                </div>
                
                <div class="control-group">
                    <label for="speed">动画速度: <span id="speed-value">正常</span></label>
                    <input type="range" id="speed" min="0" max="10" value="5">
                </div>
            </div>
            
            <div class="heart-container">
                <div class="generated-heart" id="custom-heart"></div>
            </div>
            
            <button id="animate-btn"><i class="fas fa-play"></i> 播放动画</button>
            <button id="reset-btn"><i class="fas fa-redo"></i> 重置</button>
        </div>
        
        <footer>
            <p>© 2023 HTML爱心图实战 | 使用纯CSS和HTML创建 | 探索前端设计的艺术</p>
            <p>❤️ 让爱在代码中传递 ❤️</p>
        </footer>

        <script>
            // 获取DOM元素
            const sizeSlider = document.getElementById('size');
            const colorPicker = document.getElementById('color');
            const speedSlider = document.getElementById('speed');
            const customHeart = document.getElementById('custom-heart');
            const animateBtn = document.getElementById('animate-btn');
            const resetBtn = document.getElementById('reset-btn');
            const sizeValue = document.getElementById('size-value');
            const speedValue = document.getElementById('speed-value');
            
            // 更新尺寸显示
            sizeSlider.addEventListener('input', function() {
                const size = this.value;
                sizeValue.textContent = `${size}px`;
                
                // 更新爱心尺寸
                customHeart.style.width = `${size}px`;
                customHeart.style.height = `${size}px`;
                
                // 更新伪元素尺寸
                const pseudoSize = `${size}px`;
                customHeart.style.setProperty('--pseudo-size', pseudoSize);
                
                // 更新伪元素位置
                const pseudoOffset = `${size / 2}px`;
                customHeart.style.setProperty('--pseudo-offset', pseudoOffset);
            });
            
            // 更新颜色
            colorPicker.addEventListener('input', function() {
                customHeart.style.backgroundColor = this.value;
            });
            
            // 更新速度显示
            speedSlider.addEventListener('input', function() {
                const speed = this.value;
                let speedText;
                
                if (speed < 3) speedText = '慢速';
                else if (speed < 7) speedText = '正常';
                else speedText = '快速';
                
                speedValue.textContent = speedText;
            });
            
            // 动画按钮事件
            animateBtn.addEventListener('click', function() {
                const speed = speedSlider.value;
                const duration = 2 - (speed * 0.15); // 根据速度计算动画时长
                
                customHeart.style.animation = `none`;
                setTimeout(() => {
                    customHeart.style.animation = `beat ${duration}s infinite`;
                }, 10);
            });
            
            // 重置按钮事件
            resetBtn.addEventListener('click', function() {
                // 重置滑块和值
                sizeSlider.value = 150;
                colorPicker.value = '#ff5e7d';
                speedSlider.value = 5;
                
                // 更新显示
                sizeValue.textContent = '150px';
                speedValue.textContent = '正常';
                
                // 重置爱心样式
                customHeart.style.width = '150px';
                customHeart.style.height = '150px';
                customHeart.style.backgroundColor = '#ff5e7d';
                customHeart.style.animation = 'none';
                
                // 重置伪元素尺寸
                customHeart.style.setProperty('--pseudo-size', '150px');
                customHeart.style.setProperty('--pseudo-offset', '75px');
            });
            
            // 添加CSS动画关键帧
            const style = document.createElement('style');
            style.textContent = `
                @keyframes beat {
                    0%, 100% { transform: rotate(-45deg) scale(1); }
                    50% { transform: rotate(-45deg) scale(1.15); }
                }
            `;
            document.head.appendChild(style);
        </script>
    </body>
    </html>

  • 功能亮点

  • 五种爱心实现方法

    • 伪元素方法(最常用)

    • Border-radius方法

    • CSS clip-path方法

    • SVG方法

    • 文字方法(使用❤️字符)

  • 动画效果

    • 每个爱心都有独特的脉动动画

    • 平滑的缩放效果模拟心跳

    • 不同的动画速度创造多样化效果

  • 爱心生成器

    • 实时调整爱心尺寸(50px-300px)

    • 自定义爱心颜色

    • 控制动画速度(慢速/正常/快速)

    • 播放/重置功能

  • 响应式设计

    • 在手机、平板和桌面设备上均完美显示

    • 在小屏幕设备上自动调整布局

  • 代码展示

    • 每个方法都附带源代码展示

    • 语法高亮提高可读性

    • 代码块可滚动查看

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.pswp.cn/pingmian/83031.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

2022年 中国商务年鉴(excel电子表格版)

2022年 中国商务年鉴&#xff08;excel电子表格版&#xff09;.ziphttps://download.csdn.net/download/2401_84585615/89772883 https://download.csdn.net/download/2401_84585615/89772883 《中国商务年鉴2022》是由商务部国际贸易经济合作研究院主办的年度统计资料&#xf…

Redis核心数据结构操作指南:字符串、哈希、列表详解

注&#xff1a;此为苍穹外卖学习笔记 Redis作为高性能的键值数据库&#xff0c;其核心价值来自于丰富的数据结构支持。本文将深入解析字符串&#xff08;String&#xff09;、哈希&#xff08;Hash&#xff09;、**列表&#xff08;List&#xff09;**三大基础结构的操作命令&…

如何以 9 种方式将照片从 iPhone 传输到笔记本电脑

您的 iPhone 可能充满了以照片和视频形式捕捉的珍贵回忆。无论您是想备份它们、在更大的屏幕上编辑它们&#xff0c;还是只是释放设备上的空间&#xff0c;您都需要将照片从 iPhone 传输到笔记本电脑。幸运的是&#xff0c;有 9 种方便的方法可供使用&#xff0c;同时满足 Wind…

如何使用Python从MySQL数据库导出表结构到Word文档

在开发和维护数据库的过程中&#xff0c;能够快速且准确地获取表结构信息是至关重要的。本文将向您展示一种简单而有效的方法&#xff0c;利用Python脚本从MySQL数据库中提取指定表的结构信息&#xff0c;并将其导出为格式化的Word文档。此方法不仅提高了工作效率&#xff0c;还…

写作-- 复合句练习

文章目录 练习 11. 家庭的支持和老师的指导对学生的学术成功有积极影响。2. 缺乏准备和未能适应通常会导致在挑战性情境中的糟糕表现。3. 吃垃圾食品和忽视锻炼可能导致严重的健康问题,因此人们应注重保持均衡的生活方式。4. 昨天的大雨导致街道洪水泛滥,因此居民们迁往高地以…

QT使用说明

QT环境准备 推荐Ubuntu平台上使用&#xff0c;配置简单&#xff0c;坑少。 Ubuntu 20.04 安装 sudo apt-get install qt5-default -y sudo apt-get install qtcreator -y sudo apt-get install -y libclang-common-8-dev启动 qtcreatorHelloWorld 打开 Qt Creator。选择 …

React 第四十九节 Router中useNavigation的具体使用详解及注意事项

前言 useNavigation 是 React Router 中一个强大的钩子&#xff0c;用于获取当前页面导航的状态信息。 它可以帮助开发者根据导航状态优化用户体验&#xff0c;如显示加载指示器、防止重复提交等。 一、useNavigation核心用途 检测导航状态&#xff1a;判断当前是否正在进行…

列表单独展开收起同时关闭其余子项的问题优化

如图所示&#xff0c;当在列表中&#xff0c;需要分别单独点开子选项时&#xff0c;直接这样用一个index参数判断即可&#xff0c;非常简单方便&#xff0c;只需要满足点开当前index,然后想同index用null值自动关闭即可

WPF【11_5】WPF实战-重构与美化(MVVM 实战)

11-10 【重构】创建视图模型&#xff0c;显示客户列表 正式进入 MVVM 架构的代码实战。在之前的课程中&#xff0c; Model 和 View 这部分的代码重构实际上已经完成了。 Model 就是在 Models 文件夹中看到的两个文件&#xff0c; Customer 和 Appointment。 而 View 则是所有与…

LangChain-结合魔塔社区modelscope的embeddings实现搜索

首先要安装modelscope pip install modelscope 安装完成后测试 from langchain_community.embeddings import ModelScopeEmbeddingsembeddings ModelScopeEmbeddings(model_id"iic/nlp_gte_sentence-embedding_chinese-base")text "这是一个测试句子"…

可定制化货代管理系统,适应不同业务模式需求!

在全球化贸易的浪潮下&#xff0c;货运代理行业扮演着至关重要的角色。然而&#xff0c;随着市场竞争的日益激烈&#xff0c;货代企业面临着越来越多的挑战&#xff1a;客户需求多样化、业务流程复杂化、运营成本上升、利润空间压缩……这些挑战迫使货代企业不断寻求创新和突破…

Lyra学习笔记2 GFA_AddComponents与ULyraPlayerSpawningManagerComponent

目录 前言GameFeatureAction_AddComponentsULyraPlayerSpawningManagerComponent缓存所有PlayerStart位置选择位置 前言 1.以control模式为例 2.比较散&#xff0c;想单独拿出一篇梳理下Experience的流程 GameFeatureAction_AddComponents 这部分建议看 《InsideUE5》GameFeatu…

进程生命周期

进程生命周期 Linux是多任务操作系统&#xff0c;系统中的每个进程能够分时复用CPU时间片&#xff0c;通过有效的进程调度策略实现多任务并行执行。进程在被CPU调度运行&#xff0c;等待CPU资源分配以及等待外部事件时会处于不同的状态。进程状态如下&#xff1a; 创建状态&a…

文字转图片的字符画生成工具

软件介绍 今天要介绍的这款软件可以将文字转换成图片的排列形式&#xff0c;非常适合需要将文字图形化的场景&#xff0c;建议有需要的朋友收藏。 软件名称与用途 这款软件名为《字符画大师》&#xff0c;是一款在网吧等场所非常流行的聊天辅助工具&#xff0c;其主要功能就…

历年南京大学计算机保研上机真题

2025南京大学计算机保研上机真题 2024南京大学计算机保研上机真题 2023南京大学计算机保研上机真题 在线测评链接&#xff1a;https://pgcode.cn/school Count Number of Binary Strings 题目描述 Given a positive integer n n n ( 3 ≤ n ≤ 90 3 \leq n \leq 90 3≤n≤…

王树森推荐系统公开课 排序06:粗排模型

shared bottom 表示神经网络被所有特征共享。精排模型主要开销在神经网络&#xff0c;神经网络很大且很复杂。 每做一次推荐&#xff0c;用户塔只做一次推理。物品塔存放入向量数据库。 后期融合模型常用于召回&#xff0c;前期融合模型常用于精排。 物品塔短时间内比较稳…

VSCode的下载与安装(2025亲测有效)

目录 0 前言1 下载2 安装3 后记 0 前言 丫的&#xff0c;谁懂啊&#xff0c;尝试了各种办法不行的话&#xff0c;我就不得不拿出我的最后绝招了&#xff0c;卸载&#xff0c;重新安装&#xff0c;我经常要重新安装&#xff0c;所以自己写了一个博客&#xff0c;给自己&#xf…

端午节互动网站

端午节互动网站 项目介绍 这是一个基于 Vue 3 Vite 开发的端午节主题互动网站&#xff0c;旨在通过有趣的交互方式展示中国传统端午节文化。网站包含三个主要功能模块&#xff1a;端午节介绍、互动包粽子游戏和龙舟竞赛游戏。 预览网站&#xff1a;https://duanwujiekuaile…

Python+requests+pytest接口自动化测试框架的搭建(全)

&#x1f345; 点击文末小卡片&#xff0c;免费获取软件测试全套资料&#xff0c;资料在手&#xff0c;涨薪更快 框架的设计思路 首先要明确进行接口自动化需要的步骤&#xff0c;如下图所示&#xff1a; 然后逐步拆解需要完成的工作&#xff1a; 1&#xff09;了解分析需求&…

OpenCV视觉图片调整:从基础到实战的技术指南

引言:数字图像处理的现代意义与OpenCV深度应用 在人工智能与计算机视觉蓬勃发展的今天,图像处理技术已成为多个高科技领域的核心支撑。根据市场研究机构Grand View Research的数据,全球计算机视觉市场规模预计将从2022年的125亿美元增长到2030年的253亿美元,年复合增长率达…