CSS实现跳动的心形图案?

原理:

  1. 创建1个div 

<div class="heart"></div>
.heart {
              position:relative;
              width: 100px;
              height: 100px;
              margin: 100px auto;
              background-color: tomato;
            }

  2. 用伪元素:before和:after,画出一个粉色的圆和一个黄色的圆,并将圆心分别定位在正方形的左边和上边。

  注意:

    设置:before和:after时必须设置其content属性,否则伪元素就不起作用 

    伪元素不属于文档,所以js无法操作它

    伪元素属于主元素的一部分,因此点击伪元素触发的是主元素的click事件

.heart:before{
              content: "";
              position: absolute;
              top: 0px;
              right: 50px;
              width: 100px;
              height: 100px;
              border-radius: 50%;
              background-color: pink;
            }
.heart:after {
              content: ""; /*设置:before和:after时必须设置其content属性,否则伪元素就不起作用*/
              position:absolute;/*相对于它的父元素来定位,它的父元素要加上position:relative;*/
              top: -50px;
              left: 0;
              width: 100px;
              height: 100px;
              border-radius: 50%;
              background-color: yellow;
            }

  3. 换成相同颜色

.heart:before{
              ...
              background-color: tomato;
            }
.heart:after {
             ...
              background-color: tomato;
            }

  4. div顺时针旋转45度

.heart {
            ...
            transform: rotate(45deg);/*顺时针旋转45度(逆时针旋转是-45deg)*/
            }        

  5. 小心心画好之后,

    使用@keyframes定义一个名为move的动画,

    @keyframes声明的动画名称要和animation配合使用,才能持续让爱心放大缩小

    0%-100%等价于from to,为了获得最佳的浏览器支持,应该始终定义为0%和100%的选择器。

/* 定义动画 */
            @keyframes move {
               0%{
                    transform: scale(0.8) rotate(45deg);/*缩小到0.8倍*/
                    }
                100%{
                    transform: scale(1.2) rotate(45deg);/*放大到1.2倍*/
                }
            }
.heart {
            ...
            animation: move 1s infinite alternate;/*动画名称 运动时间 运动次数infinite表示无限次 alternate往返效果相同*/
            }    

附上完整代码:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title>Css 实现跳动的心形图案?</title>
        <style type="text/css">
            .heart {
              position:relative;
              width: 100px;
              height: 100px;
              margin: 100px auto;
              background-color: tomato;
              /*transform: rotate(45deg);顺时针旋转45度(逆时针旋转是-45deg)*/
              animation: move 1s infinite alternate;/*动画名称 运动时间 运动次数infinite表示无限次 alternate往返效果相同*/
            }
            .heart:before{
              content: "";
              position: absolute;
              top: 0px;
              right: 50px;
              width: 100px;
              height: 100px;
              border-radius: 50%;
              background-color: tomato;
            }
            .heart:after {
              content: ""; /*设置:before和:after时必须设置其content属性,否则伪元素就不起作用*/
              position:absolute;/*相对于它的父元素来定位,它的父元素要加上position:relative;*/
              top: -50px;
              left: 0;
              width: 100px;
              height: 100px;
              border-radius: 50%;
              background-color: tomato;
            }
            @keyframes move {
               0%{
                    transform: scale(0.8) rotate(45deg);/*缩小到0.8倍*/
                    }
                100%{
                    transform: scale(1.2) rotate(45deg);/*放大到1.2倍*/
                }
            }
        </style>
    </head>
    <body>
        <div class="heart"></div>
    </body>
</html>

原文地址:https://www.cnblogs.com/smile01011/p/11686108.html

时间: 2024-08-26 10:52:15

CSS实现跳动的心形图案?的相关文章

java swing实现动态心形图案的代码下载

代码下载地址:http://www.zuidaima.com/share/1852319645518848.htm 原文:java swing实现动态心形图案的代码下载 package com.zuidaima.swing; import java.awt.Color; import java.awt.Graphics; import java.awt.Image; import java.awt.Toolkit; import javax.swing.JFrame; @SuppressWarn

用循环制作的 心形图案

class Program { static void Main(string[] args) { Console.WriteLine("输入4-15的数可以出现不同的心"); Console.WriteLine("输入“q”或“Q”退出"); Console.WriteLine("请输入:"); string s = Console.ReadLine(); //判断用户输入的是否是q while (s.ToUpper() !="Q&q

纯 CSS 绘制图形(心形、六边形等)

<!DOCTYPE html> <html> <head> <title></title> <meta charset="utf-8"> <style> div { width: 50px; height: 50px; background-color: red; margin: 5px; text-align:center; line-height: 50px; } /* border-radius4

CSS实现心形、六角星、六边形、平行四边形等几何

本文将利用border属性实现简单几何的绘制: 效果图: 正八角星 说明:采用两个正方形以中心进行旋转叠加: /* 八角星 */ #burst-8 { background: #6376ff1f; width: 80px; height: 80px; position: relative; text-align: center; transform: rotate(20deg); } #burst-8:before { content: ""; position: absolute;

CSS3心形效果代码

利用css3实现了心形效果,并且还能够跳动. 代码实例如下: <!doctype html> <html> <head> <meta charset="utf-8"> <meta name="author" content="http://www.softwhy.com/" /> <title>web前端开发学习q群:767273102 技术分享,欢迎基础小伙伴</ti

c++打印心形

用c++打印一个心形的图案: 1 #include<iostream> 2 #include<cmath> 3 using namespace std; 4 int main() 5 { 6     float x, y; 7     for (y = 1.5f; y >-1.5f; y -= 0.1f) 8     { 9         for (x = -1.5f; x <1.5f; x += 0.05f)10         {11             fl

【ShaderToy】跳动的心??

写在前面 注:如果你还不了解ShaderToy,请看开篇. 作为ShaderToy系列的第一篇,我们先来点简单的.下面是效果: (CSDN目前不能传gif文件了,暂时空缺,可以看下面的原shader效果,是一样的) 原Shader地址:https://www.shadertoy.com/view/XsfGRn 代码 我们使用了之前的开篇中的基础模板.这里仅仅给出main函数的代码: vec4 main(vec2 fragCoord) { vec2 p = (2.0*fragCoord.xy-iR

css3实现三角形,聊天背景气泡,心形等形状

1.聊天背景气泡: css代码如下: #talkbubble {width: 120px;margin:auto; background: red; position: relative; -moz-border-radius: 10px;-webkit-border-radius: 10px; border-radius: 10px; } #talkbubble:before { content:""; position: absolute; right: 100%; top: 26

一颗跳动的心(css3)

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>一颗跳动的心</title> <style type="text/css"> *{ margin:0 ; padding:0; } body{ background:#1a1c24; } /* 盒子 */ #heart,#