纯CSS3画出小黄人并实现动画效果

前言

前两天我刚发布了一篇CSS3实现小黄人动画的博客,但是实现的CSS3动画是基于我在站酷网找到的一张小黄人的jpg格式图片,并自己用PS抠出需要实现动画的部分,***才完成的动画效果。但是,其实我的初衷是想体验一下用图片做动画的赶脚,但是找不到素材,才无奈用了最笨的方法来满足自己的需求,本想勉强能看就行了。可是呢,还是抵不住自己内心的***,于是乎,用了一个晚上用CSS3画出了小黄人再实现类似的动画效果。

正题

OK,大家看一下下面两张图有什么不同:

当然,很多人会说,明显大小不同,嘴巴,头发也是不一样的。但是其实***的不同是,用一个形象一点的例子就是:这是两个孩子,***个是从超市货架上拿的,另外一个是我自己生的。怎么生呢?——用CSS3!

那么生下来得会动啊,不然就夭折了,所以,我就多拍了几个,就变成这样子了:

怎么样?虽然手脚动画还是不怎么神似,但人类聪明地大脑还是能看得出啥意思的吧!今天就让我们一起学习如何生出一个小黄人来。

首先,还是直接把HTML结构放出来给大家打量打量:

 
 
 
 
  1.         
  2.             
  3.                 
  4.                     
  5.                         
  •                         
  •                     
  •                     
  •                     
  •                     
  •                     
  •                     
  •                     
  •                 
  •             
  •             
  •                 
  •                 
  •             
  •             
  •                 
  •                     
  •                         
  •                     
  •                 
  •                 
  •                     
  •                         
  •                     
  •                 
  •             
  •             
  •                 
  •             
  •             
  •                 
  •                 
  •             
  •             
  •                 
  •                 
  •             
  •             
  •         
  •      
  • 好的,每一处我都标明了它的用途,大家可以稍微理解一下,在头脑里有一个大概的模型或者层次结构。

    接下来就是猛料了。。。我将分步讲解,一步一步实现:

    首先对容器和整体做一些简单的样式设置:

     
     
     
     
    1. body{
    2.     margin: 0;
    3.     padding:0;
    4. }
    5. .wrapper{
    6.     width: 300px;
    7.     margin:100px auto;
    8. }
    9. .litteH{
    10.     position: relative;
    11. }
    12. .bodyH{
    13.     position: absolute;
    14.     width: 240px;
    15.     height: 400px;
    16.     border:5px solid #000;
    17.     border-radius: 115px;
    18.     background: rgb(249,217,70);
    19.     overflow: hidden;
    20.     z-index: 2;

    其中.bodyH主要就是绘制小黄人的身体,身体的比例很重要,现在运行看一下效果:

    OK,身体已经有了,现在赶紧给它穿个裤子吧!

     
     
     
     
    1. .bodyH .condoleBelt{
    2.     position: absolute;
    3. }
    4. .bodyH .trousers{
    5.     position: absolute;
    6.     bottom: 0;
    7.     width: 100%;
    8.     height: 100px;
    9.     border-top: 6px solid #000;
    10.     background: rgb(32,116,160);
    11. }
    12. .trousers_top{
    13.     width: 160px;
    14.     height: 60px;
    15.     border:6px solid #000;
    16.     border-bottom: none;
    17.     border-radius: 0 0 5px 5px;
    18.     background: rgb(32,116,160);
    19.     position: absolute;
    20.     bottom: 100px;
    21.     left:34px;

    其中.trousers_top就是矩形部分,那么穿完裤子是什么样子的呢?

    现在怎么看都像一颗胶囊,不行,赶紧修饰修饰,给它一个吊带,不然裤子会掉呢:

     
     
     
     
    1. .bodyH .condoleBelt .left,
    2. .bodyH .condoleBelt .right{
    3.     width: 100px;
    4.     height: 16px;
    5.     border:5px solid #000;
    6.     background: rgb(32,116,160);
    7.     position: absolute;
    8.     top:-90px;
    9.     left:-35px;
    10.     z-index: 2;
    11.     -webkit-transform:rotate(45deg);
    12. }
    13. .bodyH .condoleBelt .left{
    14.     top:-88px;
    15.     left:165px;
    16.     -webkit-transform:rotate(-45deg);
    17. }
    18. .bodyH .condoleBelt .left:after,
    19. .bodyH .condoleBelt .right:after{
    20.     content: '';
    21.     width: 8px;
    22.     height: 8px;
    23.     border-radius: 50%;
    24.     background: #000;
    25.     position: absolute;
    26.     top:4px;
    27.     left:88px;
    28. }
    29.  
    30. .bodyH .condoleBelt .left:after{
    31.     left:5px;

    这一块主要就是实现两个吊带,左右各一条,然后我使用:after伪元素来实现吊带上的小黑点:

    咦,开始有模有样了,接下来我们一鼓作气把裤袋和一下修饰的边边给实现了:

     
     
     
     
    1. .pocket{
    2.     width: 60px;
    3.     height: 45px;
    4.     border:6px solid #000;
    5.     border-radius: 0px 0px 25px 25px;
    6.     position: absolute;
    7.     bottom:65px;
    8.     left:84px;
    9. }
    10. .line_right{
    11.     width: 30px;
    12.     height: 30px;
    13.     border-bottom-left-radius: 100px;
    14.     border-bottom:6px solid #000;
    15.     border-left:6px solid #000;
    16.     position: absolute;
    17.     left: 0;
    18.     bottom:60px;
    19.     -webkit-transform:rotate(-75deg);
    20. }
    21. .line_left{
    22.     width: 30px;
    23.     height: 30px;
    24.     border-bottom-right-radius: 100px;
    25.     border-bottom:6px solid #000;
    26.     border-right:6px solid #000;
    27.     position: absolute;
    28.     right: 0;
    29.     bottom:63px;
    30.     -webkit-transform:rotate(75deg);
    31. }
    32. .line_bottom{
    33.     height: 40px;
    34.     border:3px solid #000;
    35.     border-radius: 3px;
    36.     position: absolute;
    37.     left:118px;
    38.     bottom: 0px;

    先看效果图:

    OK,两个注意点说一下:

    1.我把裤子放在身体里面,然后给.bodyH添加overflow:hidden,这样我们就不用去多虑溢出怎么办,而且很轻松就能实现裤子的效果;

    2.可以看到裤子两边有两条有弧度的曲线段,我是通过给矩形某一个角实现圆角效果,然后只设置与这个角相邻的两条边的边框样式,然后旋转一下角度就实现这个效果了,当然只能实现简单一点的效果。

    好的,我们继续,接下来先实现以下眼睛部分吧:

     
     
     
     
    1. .eyes{
    2.     position: relative;
    3.     z-index: 3;
    4. }
    5. .eyes .leftEye,.eyes .rightEye{
    6.     width: 85px;
    7.     height: 85px;
    8.     border-radius: 50%;
    9.     border:6px solid #000;
    10.     background: #fff;
    11.     position: absolute;
    12.     top:60px;
    13.     left: 27px;
    14. }
    15. .eyes .leftEye{
    16.     left: 124px;
    17. }
    18. .eyes .leftEye .left_blackEye,
    19. .eyes .rightEye .right_blackEye{
    20.     width: 40px;
    21.     height: 40px;
    22.     border-radius: 50%;
    23.     background: #000;
    24.     position: absolute;
    25.     top:24px;
    26.     left:22px;
    27. }
    28. .eyes .leftEye .left_blackEye .left_white,
    29. .eyes .rightEye .right_blackEye .right_white{
    30.     width: 20px;
    31.     height: 20px;
    32.     border-radius: 50%;
    33.     background: #fff;
    34.     position: absolute;
    35.     top:7px;
    36.     left:17px;
    37. }
    38. .eyes .leftEye .left_blackEye .left_white{
    39.     top:4px;
    40.     left:17px;
    41. }
    42. .eyes .leftEye:after,
    43. .eyes .rightEye:after{
    44.     content: '';
    45.     width: 28px;
    46.     height: 18px;
    47.     background: #000;
    48.     position: absolute;
    49.     left:-30px;
    50.     top:37px;
    51.     -webkit-transform:skewX(20deg) rotate(7deg);
    52. }
    53. .eyes .leftEye:after{
    54.     left:89px;
    55.     top:37px;
    56.     -webkit-transform:skewX(-20deg) rotate(-7deg);

    眼睛部分还是挺好实现的,先画眼镜,然后黑眼珠,再画白点,***我才画的眼镜框,眼睛框我是用:after伪元素实现的。效果图如下:

    嘴巴是最困惑我的,但还是***还是能实现:

     
     
     
     
    1. .mouse{
    2.     position: relative;
    3. }
    4. .mouse .mouse_shape{
    5.     width: 55px;
    6.     height: 35px;
    7.     border:5px solid #000;
    8.     border-bottom-left-radius: 30px;
    9.     background: #fff;
    10.     position: absolute;
    11.     top:175px;
    12.     left:98px;
    13.     z-index: 3;
    14.     -webkit-transform:rotate(-35deg);

    首先也是通过矩形其中一个角实现圆角来模拟嘴巴形状,再加以旋转:

    结果是这样的,那么想要达到我们预期的效果,就需要把上半部分给遮掉,所以我用:after伪元素来实现遮罩效果:

     
     
     
     
    1. .mouse .mouse_shape:after{
    2.     content: '';
    3.     width: 70px;
    4.     height: 32px;
    5.     border-bottom:5px solid #000;
    6.     border-radius:35px 26px 5px 5px;
    7.     background: red;
    8.     position: absolute;
    9.     top:-16px;
    10.     left:3px;
    11.     -webkit-transform:rotate(34deg);} 

    这个地方就需要慢慢调到合适的位置,模拟图如下:

    红色部分就是遮罩了,也是一个矩形,设置了底边框,实现和嘴巴下半部分形成闭合效果,现在我们就可以把颜色改成肤色就行了:

     
     
     
     
    1. .mouse .mouse_shape:after{
    2.     content: '';
    3.     width: 70px;
    4.     height: 32px;
    5.     border-bottom:5px solid #000;
    6.     border-radius:35px 26px 5px 5px;
    7.     background: rgb(249,217,70);
    8.     position: absolute;
    9.     top:-16px;
    10.     left:3px;
    11.     -webkit-transform:rotate(34deg);
    12.     -webkit-animation: mouse_mask 5s ease-in-out infinite;

    效果图如下:

    接下来实现双手部分,这一部分不难,就是两个圆角矩形,旋转了角度,然后层级低于身体的层级就行,然后再通过:after伪元素实现胳膊关节处就搞定了。

     
     
     
     
    1. .hands{
    2.     position: relative;
    3. }
    4. .hands .leftHand,
    5. .hands .rightHand{
    6.     width: 80px;
    7.     height: 80px;
    8.     border:6px solid #000;
    9.     border-radius: 25px;
    10.     background: rgb(249,217,70);
    11.     position: absolute;
    12.     top:220px;
    13.     left:-23px;
    14.     -webkit-transform:rotate(40deg);
    15. }
    16. .hands .leftHand{
    17.     left:182px;
    18.     top:220px;
    19.     -webkit-transform:rotate(-40deg);
    20. }
    21. .hands .leftHand:after,
    22. .hands .rightHand:after{
    23.     content: '';
    24.     width: 6px;
    25.     border:3px solid #000;
    26.     border-radius: 3px;
    27.     position: absolute;
    28.     left:13px;
    29.     top:50px;
    30.     -webkit-transform:rotate(90deg);
    31. }
    32.  
    33. .hands .leftHand:after{
    34.     left:53px;
    35.     top:50px;
    36.     -webkit-transform:rotate(-90deg);

    效果图如下:

    再接下来就是双脚的部分了,主要就是鞋面的设计,我是通过一个矩形,然后设置不同的圆角值,加上一点点的旋转来模拟的,这一块的话,怎么做我都觉得不是很像,所以***差不多我就妥协了。

     
     
     
     
    1. .feet{
    2.     position: relative;
    3. }
    4. .feet .left_foot,
    5. .feet .right_foot{
    6.     width: 36px;
    7.     height: 50px;
    8.     border-bottom-right-radius: 6px;
    9.     border-bottom-left-radius: 9px;
    10.     background: #000;
    11.     position: absolute;
    12.     top: 406px;
    13.     left:88px;
    14.     -webkit-transform-origin: right top;
    15. }
    16. .feet .left_foot{
    17.     border-bottom-right-radius: 9px;
    18.     border-bottom-left-radius: 6px;
    19.     left:130px;
    20.     -webkit-transform-origin: left top;
    21. }
    22. .feet .left_foot:after,
    23. .feet .right_foot:after{
    24.     content: '';
    25.     width: 60px;
    26.     height: 35px;
    27.     border-radius: 20px 10px 21px 15px;
    28.     background: #000;
    29.     position: absolute;
    30.     left:-36px;
    31.     top:14.4px;
    32.     -webkit-transform:rotate(5deg);
    33. }
    34. .feet .left_foot:after{
    35.     border-radius: 10px 20px 15px 21px;
    36.     left:13px;
    37.     -webkit-transform:rotate(-5deg);

    效果图如下:

    本来是不想画头发的,感觉怪难的,***尝试了一下,还是能出个样子,那就添加上去吧。原理跟裤子左右两边的实现原理是一样的,只是参数值不一样罢了,大家参考代码琢磨一下:

     
     
     
     
    1. .hair{
    2.     position: relative;
    3. }
    4. .left_hair_one{
    5.     width: 130px;
    6.     height: 100px;
    7.     border-radius: 50%;
    8.     border-top:8px solid #000;
    9.     position: absolute;
    10.     left:17px;
    11.     top:-17px;
    12.     -webkit-transform:rotate(27deg);
    13. }
    14. .left_hair_two{
    15.     width: 80px;
    16.     height: 80px;
    17.     border-radius: 50%;
    18.     border-top:6px solid #000;
    19.     position: absolute;
    20.     left:45px;
    21.     top:-10px;
    22.     -webkit-transform:rotate(15deg);
    23. }
    24. 阴影部分:
    25. .groundShadow{
    26.     width: 200px;
    27.     height: 2px;
    28.     border-radius: 50%;
    29.     background: rgba(0,0,0,0.3);
    30.     box-shadow: 0 0 2px 4px rgba(0,0,0,0.3);
    31.     position: relative;
    32.     top: 455px;
    33.     left:25px;

    ***就是最初的效果了:

    这时大家可能会问:然后呢?不是还会动的吗?

    是的,这里我就大概讲一下实现的思路吧:

    1.头发:头发我想实现的是被小风轻轻地吹起来,然后很快就塌下去,所以吹起和下塌分配的时间很关键;

    2.眼睛:眼睛我设想的场景是小黄人在悠闲地走路,然后不时左右看一下两边的风景,所以眼睛转动很悠哉,所以分配的时间可以稍微长一点点;

    3.嘴巴:让嘴巴动起来确实比较难,因为要动的话,遮罩也得实时跟着改变,而且不能露馅,所以***实现的是当它往左边看了一下之后卖了一下萌;

    4.双手和双脚:这部分确实很难逼真,本来脚就不是正常的走路状态,所以只能按卓别林老爷爷的走路方式模拟一下,而且左右脚动作是紧接实现而不是同步实现。双手的话,我是简单模拟人在走路时有摆手的自然惯性,而且是左脚配右手,右脚配左手。考虑到它的双手是放在背后的,所以我的摆动幅度设置为非常小,有效果但不明显。

    OK,我就直接把动画代码放上来吧:

     
     
     
     
    1. .left_hair_one{
    2.     -webkit-animation: lefthair 2s ease-in-out infinite;
    3. }
    4. @-webkit-keyframes lefthair{
    5.     0%,25%,31%,100%{
    6.     }
    7.     30%{
    8.         -webkit-transform: rotate(31deg) translate3d(-3px,-1px,0);
    9.     }
    10. }
    11. .eyes .leftEye .left_blackEye,
    12. .eyes .rightEye .right_blackEye{
    13.     -webkit-animation: blackeye 5s ease-in infinite;
    14. }
    15. @-webkit-keyframes blackeye{
    16.     0%,20%,50%,70%,100%{
    17.         -webkit-transform: translateX(0px);
    18.     }
    19.     30%,40%{
    20.         -webkit-transform: translateX(15px);
    21.     }
    22.     80%,90%{
    23.         -webkit-transform: translateX(-15px);
    24.     }
    25. }
    26. .eyes .leftEye .left_blackEye .left_white,
    27. .eyes .rightEye .right_blackEye .right_white{
    28.     -webkit-animation: whiteeye 5s ease-in-out infinite;
    29. }
    30. @-webkit-keyframes whiteeye{
    31.     0%,20%,50%,70%,100%{
    32.         -webkit-transform: translateX(0px);
    33.     }
    34.     30%,40%{
    35.         -webkit-transform: translate3d(3px,4px,0);
    36.     }
    37.     80%,90%{
    38.         -webkit-transform: translate3d(-15px,4px,0);
    39.     }
    40. }
    41. .mouse .mouse_shape{
    42.     -webkit-animation: mouse 5s ease-in-out infinite;
    43. }
    44. @-webkit-keyframes mouse{
    45.     40%,43%{
    46.         width: 45px;
    47.         height: 25px;
    48.         top:180px;
    49.     }
    50.     0%,35%,48%,100%{
    51.         width: 55px;
    52.         height: 35px;
    53.         top:175px;
    54.         -webkit-transform:rotate(-35deg);
    55.  &n

      网页标题:纯CSS3画出小黄人并实现动画效果
      链接地址:http://www.shufengxianlan.com/qtweb/news35/349785.html

      网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

      广告

      声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联