css+js实现鼠标移动跟随动画

<!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>
    <head>
        <style>
            body,
            html,
            * {
                padding: 0;
                margin: 0;
            }
            
            #wrapper {
                width: 1500px;
                height: 400px;
                margin: 100px auto;
                background-color: lavender;
                font-size: 0;
                overflow: hidden;
            }

            .circle {
                width: var(--circle-width);
                height: var(--circle-height);
                display: inline-flex;
                border-radius: 50%;
                animation: var(--circle-show);
                position: relative;
            }

            .tip {
                position: absolute;
                width: 5px;
                height: 10px;
                left: 50%;
                top: -10%;
                border-radius: 100% 0 0 100%/50%;
                transform: translate(-50%, -50%);
                animation: var(--tip-show);
            }

            @keyframes tipAnimation {

                0% {

                    border: 1px solid black;

                    border-right: none;

                }



                100% {

                    border: 1px solid transparent;

                    border-right: none;

                }

            }

            @keyframes circleAnimation {

                0% {

                    background-color: rosybrown;

                    transform: rotate(0deg);

                }



                10% {

                    background-color: lightcoral;

                }



                20% {

                    background-color: lemonchiffon;

                }



                30% {

                    background-color: lightgreen;

                }



                40% {

                    background-color: lawngreen;

                }



                50% {

                    background-color: lightblue;

                }



                60% {

                    background-color: lightskyblue;

                }



                70% {

                    background-color: lightseagreen;

                }



                80% {

                    background-color: pink;

                }



                90% {

                    background-color: plum;

                }



                100% {

                    background-color: purple;

                    transform: rotate(360deg);

                }

            }

            @keyframes circleAnimationLeave {

                0% {

                    background-color: rosybrown;

                }



                10% {

                    background-color: lightcoral;

                }



                20% {

                    background-color: lemonchiffon;

                }



                30% {

                    background-color: lightgreen;

                }



                40% {

                    background-color: lawngreen;

                }



                50% {

                    background-color: lightblue;

                }



                60% {

                    background-color: lightskyblue;

                }



                70% {

                    background-color: lightseagreen;

                }



                80% {

                    background-color: pink;

                }



                90% {

                    background-color: plum;

                }



                100% {

                    background-color: purple;

                }

            }
        </style>
    </head>
<body>
    <div id="wrapper">
    </div>
</body>

<script>
    const wrapper = document.getElementById('wrapper');
    const width = 1500 / 60;
    const height = 800 / 33;
    const len = width * height;
    for (let i = 0; i < len; i++) {
        let circle = document.createElement('div');
        let tip = document.createElement('tip');
        circle.className = 'circle';
        tip.className = 'tip';
        circle.appendChild(tip);
        circle.style.setProperty('--circle-width', `${width}px`);
        circle.style.setProperty('--circle-height', `${height}px`);
        tip.style.setProperty('--tip-show', 'none');
        wrapper.appendChild(circle);
    }

    const circles = document.getElementsByClassName('circle');
    if (circles.length > 0) {
        for (let i = 0; i < circles.length; i++) {
            circles[i].addEventListener('mouseenter', () => {
                circles[i].style.setProperty('--circle-show', 'circleAnimation 3s linear');
                let tip = circles[i].firstChild;
                tip.style.setProperty('--tip-show', 'tipAnimation 1s linear');
            });
            circles[i].addEventListener('mouseleave', () => {
                circles[i].style.setProperty('--circle-show', 'circleAnimationLeave 2s linear');
            });
        }
    }
</script>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值