如何使用css画一个冰墩墩?

冰墩墩,可爱的雪人形象,给人带来了冰凉清爽的冬日氛围。现在让我们用 CSS 来画一个可爱的冰墩墩吧!

实现效果:

以下是全部代码:

<!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>
        * {
            margin: 0;
            padding: 0;
        }

        html {

            /* background-image: linear-gradient(pink, #e4f3f6)  */
            background: no-repeat linear-gradient(pink, #e4f3f6);
            color: #000;
            width: 100%;
            height: 100%;
            overflow: hidden;

        }

        /* 设置冰墩墩的大概范围 */
        .conter {
            position: absolute;
            /* margin: 100px auto; */
            left: 40%;
            top: -200px;
        }

        .er1,
        .er2 {
            background: #393939;
            width: 81px;
            height: 115px;
            border-radius: 50%;
            position: absolute;
            z-index: -1;
        }

        .er1 {
            position: relative;
            top: 280px;
            left: 29px;
        }

        .er2 {
            position: relative;
            top: 160px;
            left: 197px;

        }

        .body {
            position: absolute;
            width: 300px;
            /* height: 400px; */
            height: 350px;
            background-color: #fff;
            border-radius: 88% 88% 62% 68% / 82% 82% 95% 84%;
            border: 8px solid #000;
            margin: 50px auto;
        }

        .quan1 {
            position: relative;
            top: 90px;
            left: 30px;
            width: 250px;
            height: 170px;
            border: 4px solid yellow;
            border-radius: 60% 60% 50% 50%;
        }

        .conter .quan2 {
            position: relative;
            top: 2px;
            left: 1px;
            width: 242px;
            height: 159px;
            border: 4px solid rebeccapurple;
            border-radius: 60% 60% 50% 50%;
        }

        .conter .quan3 {
            position: relative;
            top: 0px;
            left: 0px;
            width: 234px;
            height: 151px;
            border: 4px solid green;
            border-radius: 60% 60% 50% 50%
        }

        .conter .quan4 {
            position: relative;
            top: 0px;
            left: -2px;
            width: 228px;
            height: 143px;
            border: 4px solid #32cbf3;
            border-radius: 60% 60% 50% 50%;
        }

        .eye1,
        .eye2 {
            width: 70px;
            height: 90px;
            background-color: #393939;
            border-radius: 50%;
            position: absolute;
            /* z-index: 21; */
        }

        .eye1 {
            top: 356px;
            left: 65px;
            transform: rotate(50deg);
        }

        .eye2 {
            top: 356px;
            left: 180px;
            transform: rotate(-50deg);

        }

        .eye1:before,
        .eye2:before {
            content: "";
            width: 40px;
            height: 40px;
            border: #fff 5px solid;
            border-radius: 100%;
            position: absolute;
        }

        .eye1:before {
            right: 5px;
            top: 10px;
        }

        .eye2:before {
            left: 6px;
            top: 9px;

        }

        .eye1:after,
        .eye2:after {
            content: "";
            width: 10px;
            height: 10px;
            background: #fff;
            border-radius: 100%;
            position: absolute;
        }

        .eye1:after {
            right: 22px;
            top: 19px;

        }

        .eye2:after {
            left: 38px;
            top: 31px;
        }


        .bizi {
            position: relative;
            top: 0;
            left: 149px;
            width: 20px;
            height: 12px;
            background-color: #393939;
            border-radius: 38% 38% 50% 50%;
        }

        .shou1,
        .shou2 {
            background: #333;
            position: absolute;
            width: 60px;
            height: 80px;
            border-radius: 0 0 30px 30px;
            z-index: -1;
        }

        .shou1 {
            transform: rotate(50deg);
            left: -40px;
            top: 459px;
        }

        .shou2 {
            position: absolute;
            transform: rotate(37deg);
            left: 279px;
            top: 336px;
            height: 120px;
            border-radius: 40% 50% 50% 20%;
            animation: dong 2s infinite alternate linear;
            transform-origin: left bottom;
        }


        @keyframes dong {
            form {
                transform: rotate(27deg);
            }

            to {
                transform: rotate(67deg);
            }
        }

        .shou2:before {
            content: "";
            width: 16px;
            height: 24px;
            background: #bc242c;
            position: absolute;
            border-top-left-radius: 50%;
            border-top-right-radius: 50%;
            transform: rotate(45deg);
            left: 25px;
            top: 21px;

        }

        .shou2:after {
            content: "";
            width: 16px;
            height: 24px;
            background: #bc242c;
            position: absolute;
            border-top-left-radius: 50%;
            border-top-right-radius: 50%;
            transform: rotate(-45deg);
            left: 18px;
            top: 20px;
        }




        .leg1,
        .leg2 {
            background: #333;
            position: absolute;
            width: 63px;
            height: 80px;
            /* border-radius: 0 0 30px 30px; */
            z-index: -1;
        }

        .leg1 {
            left: 72px;
            top: 626px;
            border-radius: 0 0 11px 22px;
        }

        .leg1:after,
        .leg2:after {
            content: "";
            width: 43px;
            height: 30px;
            position: absolute;
            background: #333333;
            border-radius: 30px;
        }

        .leg1:after {
            top: 50px;
            left: 23px;
        }

        .leg2:after {
            top: 50px;
            left: -3px;
        }

        .leg2 {
            left: 180px;
            top: 626px;
            border-radius: 0 0 22px 11px;

        }




        .text {
            position: relative;
            top: 94px;
            left: 114px;
            font: 700 30px '';

        }

        .zuiba {
            position: relative;
            top: -27px;
            left: 132px;
            width: 55px;
            height: 40px;
            background-color: #393939;
            border-radius: 50% 50%;
            z-index: 10;
        }

        .zuiba1 {
            position: relative;
            top: -79px;
            left: 146px;
            width: 25px;
            height: 20px;
            background-color: #fff;
            border-radius: 50% 50%;
            z-index: 10;
        }

        .zuiba2 {
            position: relative;
            top: -70px;
            left: 143px;
            width: 35px;
            height: 20px;
            background-color: #bc242c;
            border-radius: 50% 50%;
            z-index: 10;
        }

        .huan1,
        .huan2,
        .huan3,
        .huan4,
        .huan5 {
            position: relative;
            width: 20px;
            height: 20px;
            border-radius: 50%;
            z-index: 10;

        }

        .huan1 {
            top: 50px;
            left: 117px;
            border: 2px solid #32cbf3;
        }

        .huan2 {
            top: 26px;
            left: 142px;
            border: 2px solid #333;
        }

        .huan3 {
            top: 2px;
            left: 167px;
            border: 2px solid red;
        }

        .huan4 {
            top: -14px;
            left: 129px;
            border: 2px solid tan;
        }

        .huan5 {
            top: -38px;
            left: 155px;
            border: 2px solid green;
        }
    </style>
</head>

<body>
    <div class="conter">
        <!-- 耳朵 -->
        <div class="er1"></div>
        <div class="er2"></div>
        <!-- 身体 -->
        <div class="body"></div>
        <!-- 脸上的圈圈 -->
        <div class="quan1">
            <div class="quan2">
                <div class="quan3">
                    <div class="quan4"></div>
                </div>
            </div>
        </div>
        <!-- 眼睛 -->
        <div class="eye1"></div>
        <div class="eye2"></div>
        <!-- 鼻子 -->
        <div class="bizi"></div>
        <!-- 手 -->
        <div class="shou1"></div>
        <div class="shou2">
            <div id="outer">
                <div id="div1">
                </div>
                <div id="div2">
                </div>
                <div id="div3">
                </div>
            </div>
        </div>
        <!-- 脚 -->
        <div class="leg1"></div>
        <div class="leg2">

        </div>
        <div class="text">冰墩墩</div>
        <!-- 嘴巴 -->
        <div class="zuiba"></div>
        <div class="zuiba1"></div>
        <div class="zuiba2"></div>
        <!-- 奥运五环 -->
        <div class="huan1"></div>
        <div class="huan2"></div>
        <div class="huan3"></div>
        <div class="huan4"></div>
        <div class="huan5"></div>
    </div>
</body>
<script>
    function snow() {
        // 1、定义一片雪花模板
        var flake = document.createElement('div');
        // 雪花字符 ❄❉❅❆✻✼❇❈❊✥✺
        flake.innerHTML = '❅';
        flake.style.cssText = 'position:absolute;color:#fff;';

        //获取页面的高度 相当于雪花下落结束时Y轴的位置
        var documentHieght = window.innerHeight;
        //获取页面的宽度,利用这个数来算出,雪花开始时left的值
        var documentWidth = window.innerWidth;

        //定义生成一片雪花的毫秒数
        var millisec = 10;
        //2、设置第一个定时器,周期性定时器,每隔一段时间(millisec)生成一片雪花;
        setInterval(function () { //页面加载之后,定时器就开始工作
            //随机生成雪花下落 开始 时left的值,相当于开始时X轴的位置
            var startLeft = Math.random() * documentWidth;

            //随机生成雪花下落 结束 时left的值,相当于结束时X轴的位置
            var endLeft = Math.random() * documentWidth;

            //随机生成雪花大小
            var flakeSize = 3 + 20 * Math.random();

            //随机生成雪花下落持续时间
            var durationTime = 4000 + 7000 * Math.random();

            //随机生成雪花下落 开始 时的透明度
            var startOpacity = 0.7 + 0.3 * Math.random();

            //随机生成雪花下落 结束 时的透明度
            var endOpacity = 0.2 + 0.2 * Math.random();

            //克隆一个雪花模板
            var cloneFlake = flake.cloneNode(true);

            //第一次修改样式,定义克隆出来的雪花的样式
            cloneFlake.style.cssText += `
      left: ${startLeft}px;
      opacity: ${startOpacity};
      font-size:${flakeSize}px;
      top:-25px;
       transition:${durationTime}ms;`;

            //拼接到页面中
            document.body.appendChild(cloneFlake);

            //设置第二个定时器,一次性定时器,
            //当第一个定时器生成雪花,并在页面上渲染出来后,修改雪花的样式,让雪花动起来;
            setTimeout(function () {
                //第二次修改样式
                cloneFlake.style.cssText += `
       left: ${endLeft}px;
       top:${documentHieght}px;
       opacity:${endOpacity};`;

                //4、设置第三个定时器,当雪花落下后,删除雪花。
                setTimeout(function () {
                    cloneFlake.remove();
                }, durationTime);
            }, 0);

        }, millisec);
    }
    snow();
</script>

</html>

以下是一个使用HTMLCSS和JavaScript绘制带有动冰墩墩的示例: HTML代码: ```html <div class="ice-container"> <div class="ice-block"></div> <div class="ice-block"></div> <div class="ice-block"></div> <div class="ice-block"></div> <div class="ice-block"></div> <div class="ice-block"></div> <div class="ice-block"></div> <div class="ice-block"></div> </div> ``` CSS代码: ```css .ice-container { position: relative; width: 200px; height: 400px; margin: 50px auto; perspective: 1000px; } .ice-block { position: absolute; width: 100px; height: 100px; background-color: #e0ffff; border-radius: 50%; transform-style: preserve-3d; animation: spin 10s linear infinite; } .ice-block:nth-child(1) { top: 0; left: 50px; transform: rotateX(90deg) translateZ(50px); } .ice-block:nth-child(2) { top: 50px; left: 0; transform: rotateY(90deg) translateZ(50px); } .ice-block:nth-child(3) { top: 100px; left: 50px; transform: rotateX(-90deg) translateZ(50px); } .ice-block:nth-child(4) { top: 50px; left: 100px; transform: rotateY(-90deg) translateZ(50px); } .ice-block:nth-child(5) { top: 50px; left: 50px; transform: translateZ(150px); } .ice-block:nth-child(6) { top: 150px; left: 50px; transform: rotateX(-90deg) translateZ(250px); } .ice-block:nth-child(7) { top: 150px; left: 50px; transform: translateZ(350px); } .ice-block:nth-child(8) { top: 250px; left: 50px; transform: rotateX(-90deg) translateZ(450px); } @keyframes spin { from { transform: rotateY(0); } to { transform: rotateY(360deg); } } ``` JavaScript代码: ```javascript // 为冰墩墩添加鼠标悬停事件 document.querySelector('.ice-container').addEventListener('mouseover', function() { // 将所有冰块的背景色变为白色 document.querySelectorAll('.ice-block').forEach(function(block) { block.style.backgroundColor = '#fff'; }); }); // 为冰墩墩添加鼠标离开事件 document.querySelector('.ice-container').addEventListener('mouseout', function() { // 将所有冰块的背景色变回原来的颜色 document.querySelectorAll('.ice-block').forEach(function(block, index) { block.style.backgroundColor = '#e0ffff'; // 为每个冰块设置不同的延迟时间,使它们逐个变回原来的颜色 setTimeout(function() { block.style.transition = 'background-color 0.5s ease'; }, index * 100); }); }); ``` 这段代码定义了一个.container元素,其中包含八个.ice-block元素,每个元素表示冰墩墩一个面。通过CSS的transform属性和animation属性,可以使这些元素旋转并形成3D效果。同时,JavaScript代码为这个冰墩墩添加了鼠标悬停和离开事件,使其可以响应用户的交互。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值