HTML实战:爱心图的实现

news2025/6/5 3:51:16

设计思路

  • 使用纯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.coloradmin.cn/o/2396828.html

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

相关文章

定时任务:springboot集成xxl-job-core(二)

定时任务实现方式&#xff1a; 存在的问题&#xff1a; xxl-job的原理&#xff1a; 可以根据服务器的个数进行动态分片&#xff0c;每台服务器分到的处理数据是不一样的。 1. 多台机器动态注册 多台机器同时配置了调度器xxl-job-admin之后&#xff0c;执行器那里会有多个注…

DeviceNET转EtherCAT网关:医院药房自动化的智能升级神经中枢

在现代医院药房自动化系统中&#xff0c;高效、精准、可靠的设备通信是保障患者用药安全与效率的核心。当面临既有支持DeviceNET协议的传感器、执行器&#xff08;如药盒状态传感器、机械臂限位开关&#xff09;需接入先进EtherCAT高速实时网络时&#xff0c;JH-DVN-ECT疆鸿智能…

一:UML类图

一、类的设计 提示:这里可以添加系列文章的所有文章的目录,目录需要自己手动添加 学习设计模式的第一步是看懂UML类图,类图能直观的表达类、对象之间的关系,这将有助于后续对代码的编写。 类图在软件设计及应用框架前期设计中是不可缺少的一部分,它的主要成分包括:类名、…

Java 中 MySQL 索引深度解析:面试核心知识点与实战

&#x1f91f;致敬读者 &#x1f7e9;感谢阅读&#x1f7e6;笑口常开&#x1f7ea;生日快乐⬛早点睡觉 &#x1f4d8;博主相关 &#x1f7e7;博主信息&#x1f7e8;博客首页&#x1f7eb;专栏推荐&#x1f7e5;活动信息 文章目录 Java 中 MySQL 索引深度解析&#xff1a;面试…

设计模式之结构型:装饰器模式

装饰器模式(Decorator Pattern) 定义 装饰器模式是一种​​结构型设计模式​​&#xff0c;允许​​动态地为对象添加新功能​​&#xff0c;而无需修改其原始类。它通过将对象包装在装饰器类中&#xff0c;以​​组合代替继承​​&#xff0c;实现功能的灵活扩展(如 Java I/O …

MySQL安装及启用详细教程(Windows版)

MySQL安装及启用详细教程&#xff08;Windows版&#xff09; &#x1f4cb; 概述 本文档将详细介绍MySQL数据库在Windows系统下的下载、安装、配置和启用过程。 &#x1f4e5; MySQL下载 官方下载地址 官方网站: https://dev.mysql.com/downloads/社区版本: https://dev.my…

【HarmonyOS Next之旅】DevEco Studio使用指南(二十九) -> 开发云数据库

目录 1 -> 开发流程 2 -> 创建对象类型 3 -> 添加数据条目 3.1 -> 手动创建数据条目文件 3.2 -> 自动生成数据条目文件 4 -> 部署云数据库 1 -> 开发流程 云数据库是一款端云协同的数据库产品&#xff0c;提供端云数据的协同管理、统一的数据模型和…

批量导出CAD属性块信息生成到excel——CAD C#二次开发(插件实现)

本插件可实现批量导出文件夹内大量dwg文件的指定块名的属性信息到excel&#xff0c;效果如下&#xff1a; 插件界面&#xff1a; dll插件如下&#xff1a; 使用方法&#xff1a; 1、获取此dll插件。 2、cad命令行输入netload &#xff0c;加载此dll&#xff08;要求AutoCAD&…

Goreplay最新版本的安装和简单使用

一&#xff1a;概述 Gor 是一个开源工具&#xff0c;用于捕获实时 HTTP 流量并将其重放到测试环境中&#xff0c;以便使用真实数据持续测试您的系统。它可用于提高对代码部署、配置更改和基础设施更改的信心。简单易用。 项目地址&#xff1a;buger/goreplay: GoReplay is an …

Android Studio 解决报错 not support JCEF 记录

问题&#xff1a;Android Studio 安装Markdown插件后&#xff0c;报错not support JCEF不能预览markdown文件。 原因&#xff1a;Android Studio不是新装&#xff0c;之前没留意IDE自带的版本是不支持JCEF的。 解决办法&#xff1a; 在菜单栏选中Help→Find Action&#xff…

sigmastar实现SD卡升级

参考文章:http://wx.comake.online/doc/DD22dk2f3zx-SSD21X-SSD22X/customer/development/software/Px/zh/sys/P3/usb%20&%20sd%20update.html#21-sd 1、构建SD卡升级包 在project下make image完成后使用make_sd_upgrade_sigmastar.sh脚本打包SD卡升级包。 ./make_sd_up…

kafka学习笔记(三、消费者Consumer使用教程——配置参数大全及性能调优)

本章主要介绍kafka consumer的配置参数及性能调优的点&#xff0c;其kafka的从零开始的安装到生产者&#xff0c;消费者的详解介绍、源码及分析及原理解析请到博主kafka专栏 。 1.消费者Consumer配置参数 配置参数默认值含义bootstrap.servers无&#xff08;必填&#xff09;…

【论文笔记】Transcoders Find Interpretable LLM Feature Circuits

Abstract 机制可解释性(mechanistic interpretability)的核心目标是路径分析(circuit analysis)&#xff1a;在模型中找出与特定行为或能力对应的稀疏子图。 然而&#xff0c;MLP 子层使得在基于 Transformer 的语言模型中进行细粒度的路径分析变得困难。具体而言&#xff0c;…

每天总结一个html标签——a标签

文章目录 一、定义与使用说明二、支持的属性三、支持的事件四、默认样式五、常见用法1. 文本链接2. 图片链接3. 导航栏 在前端开发中&#xff0c;a标签&#xff08;锚点标签&#xff09;是最常用的HTML标签之一&#xff0c;主要用于创建超链接&#xff0c;实现页面间的跳转或下…

android binder(1)基本原理

一、IPC 进程间通信&#xff08;IPC&#xff0c;Inter-Process Communication&#xff09;机制&#xff0c;用于解决不同进程间的数据交互问题。 不同进程之间用户地址空间的变量和函数是不能相互访问的&#xff0c;但是不同进程的内核地址空间是相同和共享的&#xff0c;我们可…

行业分析---小米汽车2025第一季度财报

1 背景 最近几年是新能源汽车的淘汰赛&#xff0c;前短时间比亚迪再次开始了降价&#xff0c;导致一片上市车企的股价大跌&#xff0c;足见车圈现在的敏感度。因此笔者会一直跟踪新势力车企的财报状况&#xff0c;对之前财报分析感兴趣的读者朋友可以参考以下博客&#xff1a;…

边缘计算网关支撑医院供暖系统高效运维的本地化计算与边缘决策

一、项目背景 医院作为人员密集的特殊场所&#xff0c;对供暖系统的稳定性和高效性有着极高的要求。其供暖换热站传统的人工现场监控方式存在诸多弊端&#xff0c;如人员值守成本高、数据记录不及时不准确、故障发现和处理滞后、能耗难以有效监测和控制等&#xff0c;难以满足…

简单了解string类的特性及使用(C++)

string的特性 string类不属于STL&#xff0c;它属于标准库 但由于它具有数据结构的特性&#xff0c;所以从归类的角度&#xff0c;可以将string类归类到容器里面去 在C标准库中&#xff0c;std::string 是一个特化的类型&#xff0c;实际上是 std::basic_string 的别名。std…

FastAPI+Pyomo实现线性回归解决饮食问题

之前在 FastAPI介绍-CSDN博客 中介绍过FastAPI&#xff0c;在 Pyomo中线性规划接口的使用-CSDN博客 中使用Pyomo解决饮食问题&#xff0c;这里将两者组合&#xff0c;即FastAPI在服务器端启动&#xff0c;通过Pyomo实现线性回归&#xff1b;客户端通过浏览器获取饮食的最优解。…

16.FreeRTOS

目录 第1章 FreeRTOS 实时操作系统 1.1 认识实时操作系统 1.1.1 裸机的概念 1.1.2 操作系统的概念 1.2 操作系统的分类 1.3 常见的操作系统 1.4 认识实时操作系统 1.4.1 可剥夺型内核与不可剥夺型内核 1.4.2 嵌入式操作系统的作用 1.4.3 嵌入式操作系统的发展 1.4.4…