css特效(使用伪元素生成随机颜色爱心)

使用伪元素生成爱心

不多bb直接上代码

 .love {
            position: fixed;
            width: 20px;
            height: 20px;
            background-color: red;
            transform: rotate(45deg);
            opacity: 0;
            animation: love 1s ease;
        }
        
        .love::after {
            position: absolute;
            display: block;
            content: "";
            width: 20px;
            height: 20px;
            border-radius: 50%;
            transform: translate(0px, -10px);
            background-color: inherit;
        }
        
        .love::before {
            position: absolute;
            display: block;
            content: "";
            width: 20px;
            height: 20px;
            border-radius: 50%;
            background-color: inherit;
            transform: translate(-10px, 0px);
        }
<div class='love'></love>

我们可以通过使用伪元素结合之前说过的定位和css3的transform属性来生成爱心
在这里插入图片描述
那么我们想点击页面让爱心随着鼠标移动并且随机颜色的爱心,且当鼠标点击后爱心往上移动并逐渐失效

先申明一个函数 生成十六进制的颜色

 function getColor() {
        let newStr = ''
        let str = '1234567890ABCDEF'
        for (let i = 0; i < 6; i++) {

            newStr += str.charAt(Math.floor(Math.random() * 16))
        }
        return '#' + newStr
    }
    console.log(getColor());

然后动画属性

 @keyframes love {
       0% {
             transform: rotate(45deg) translateY(0);
             opacity: 1;
         }
         100% {
             transform: rotate(45deg) translateY(-15px);
             opacity: 0;
         }
     }

现在需要的效果是鼠标点击页面,页面生成爱心,爱心的颜色是随机的。并且爱心有动画效果
思路:
1 点击页面生成div
2 给div加上love属性 让它前后有伪元素生成爱心
3 获取鼠标的位置赋值给div的left top
4 将生成的div放到body中
5 给div赋值不同颜色
6 一段时间让dom消失
附上完整代码

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        @keyframes love {
            0% {
                transform: rotate(45deg) translateY(0);
                opacity: 1;
            }
            100% {
                transform: rotate(45deg) translateY(-15px);
                opacity: 0;
            }
        }
        
        html,
        body {
            height: 100%;
            width: 100%;
        }
        
        * {
            margin: 0;
            padding: 0;
        }
        
        .love {
            position: fixed;
            width: 20px;
            height: 20px;
            background-color: red;
            transform: rotate(45deg);
            opacity: 0;
            animation: love 1s ease;
        }
        
        .love::after {
            position: absolute;
            display: block;
            content: "";
            width: 20px;
            height: 20px;
            border-radius: 50%;
            transform: translate(0px, -10px);
            background-color: inherit;
        }
        
        .love::before {
            position: absolute;
            display: block;
            content: "";
            width: 20px;
            height: 20px;
            border-radius: 50%;
            background-color: inherit;
            transform: translate(-10px, 0px);
        }
    </style>
</head>

<body>
    <!-- <div class="love"></div> -->
</body>
<script>
    // 编写一个函数,获得一个十六进制的随机颜色的字符串(例如:#20CD4F)
    function getColor() {
        let newStr = ''
        let str = '1234567890ABCDEF'
        for (let i = 0; i < 6; i++) {

            newStr += str.charAt(Math.floor(Math.random() * 16))
        }
        return '#' + newStr
    }
    console.log(getColor());
    // 给页面绑定点击事件
    document.querySelector('body').onclick = function(e) {
        let div = document.createElement('div')
        div.className = 'love'
            //获取鼠标位置 xy 位置
        let x = e.clientX
        let y = e.clientY
        div.style.left = 5 + x + 'px'
        div.style.top = -30 + y + 'px'
        document.body.append(div);
        div.style.background = getColor()
        setTimeout(() => {
            div.remove()
        }, 500)
    }
</script>

</html>

上面的代码可以直接运行

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱玩亚索的程序员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值