当前位置:Gxlcms > css > 纯CSS3绘制打火机动画火焰效果示例代码

纯CSS3绘制打火机动画火焰效果示例代码

时间:2021-07-01 10:21:17 帮助过:49人阅读

本文实例为大家分享了纯CSS3绘制打火机动画火焰效果的具体代码,供大家参考,具体内容如下

主要涉及到了以下属性:

  1. <span style="font-family:Comic Sans MS;">animation(webkit-animation),
  2. @keyframes name (@-webkit-keyframes name),
  3. transform,
  4. transform-origin,
  5. transition,
  6. box-shadow(spread属性),
  7. text-shadow
  8. z-index
  9. linear-gradient。
  10. radial-gradient
  11. background-image
  12. </span>


首先html代码如下:

  1. <span style="font-family:Comic Sans MS;"><!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>纯CSS绘制打火机动画火焰特效</title>
  6. <link rel="stylesheet" href="css/style.css">
  7. </head>
  8. <body class="keBody">
  9. <h1 class="keTitle">纯CSS3绘制打火机动画火焰特效</h1>
  10. <p class="kePublic">
  11. <p class="playground">
  12. <p class="flame"></p>
  13. <p class="lighterBody">
  14. <p class="hover">hover me</p>
  15. </p>
  16. <p class="lid"></p>
  17. </p>
  18. <p class="clear"></p>
  19. </p>
  20. </body>
  21. </html></span>

style.css文件如下:

  1. <span style="font-family:Comic Sans MS;">@charset "utf-8";
  2. body{
  3. margin:0;
  4. padding:0;
  5. color:#333;
  6. text-align:center;
  7. font:12px "微软雅黑";
  8. }
  9. :before,:after{
  10. position:absolute;
  11. content:"";
  12. }
  13. .keBody{
  14. background:url(../images/bodyBg.jpg) repeat #333;
  15. }
  16. .keTitle{
  17. height:100px;
  18. line-height:100px;
  19. font-size:30px;
  20. font-family:'微软雅黑';
  21. color:#fff;
  22. text-align:center;
  23. background:url(../images/bodyBg3.jpg) repeat-x bottombottom left;
  24. font-weight:normal;
  25. margin-bottom:0;
  26. }
  27. .kePublic{
  28. background:#cccccc;
  29. padding:50px;
  30. }
  31. .playground{
  32. position:relative;
  33. width:140px;
  34. height:400px;
  35. left:50%;
  36. margin-left:-70px;
  37. }
  38. .flame{
  39. opacity: 0;
  40. position: absolute;
  41. bottombottom: 60%;
  42. left: 42%;
  43. width: 14px;
  44. height: 70px;
  45. background-color: white;
  46. border-radius: 100% 100% 0 0;
  47. box-shadow: 0 0 20px #FFFEF0, 0 0 20px #FFFEE6, 0 0 20px #fefcc9, 10px -10px 30px #feec85, -20px -20px 40px #ffae34, 20px -40px 50px #ec760c, -20px -60px 60px #cd4606, 0 -80px 70px #973716, 10px -90px 80px #451b0e;
  48. }
  49. .playground:hover .flame {
  50. opacity:1;
  51. transition:opacity 3s linear;
  52. -webkit-animation:flame 1s infinite linear;
  53. }
  54. .playground:hover .lid{
  55. -webkit-animation:lidoff 1s linear;
  56. /*把物体动画地从一个地方移动到另一个地方,并让它停留在那里:*/
  57. -webkit-animation-fill-mode:forwards;
  58. }
  59. .lighterBody{
  60. position:absolute;
  61. width:140px;
  62. height:130px;
  63. top:200px;
  64. left:0;
  65. background-image:linear-gradient(to rightright,#959595 0%,#0d0d0d 46%,#010101 50%,#0a0a0a 53%,
  66. #4e4e4e 76%,#383838 87%,#1b1b1b 100%);
  67. border-radius:2% 2% 8% 8%;
  68. box-shadow:inset 0 0 5px 5px #333;
  69. }
  70. .lighterBody:before{
  71. width:47px;
  72. height:47px;
  73. top:-50px;
  74. left: 42px;
  75. content:"...";
  76. color:#e6e6e6;
  77. border-radius:6% 6% 0 0;
  78. background-image:linear-gradient(to rightright,#f5f5f5 0%,#dbdce2 21%,#b8bac6 49%,#dddfe3 80%,#f5f6f6 100%);
  79. font-size:30px;
  80. font-weight:bold;
  81. letter-spacing:7px;
  82. line-height:16px;
  83. text-shadow:0 0 5px black;
  84. padding-top:3px;
  85. padding-left:4px;
  86. }
  87. .lighterBody:after{
  88. width:33px;
  89. height:33px;
  90. background-image:radial-gradient(ellipse at center,#7d737d 0%,#0e0e0e 100%);
  91. border-radius:100%;
  92. top:-33px;
  93. left:10px;
  94. box-shadow:inset 0 0 1px 2px gray;
  95. }
  96. .lid{
  97. z-index:2;
  98. position:absolute;
  99. width:140px;
  100. height:75px;
  101. top:125px;
  102. left:0;
  103. background:linear-gradient(to rightright,#959595 0%,#0d0d0d 46%, #010101 50%, #0a0a0a 53%, #4e4e4e 76%, #383838 87%, #1b1b1b 100%);
  104. border-radius:8% 8% 2% 2%;
  105. box-shadow:inset 0 0 5px 5px #333;
  106. /*border-bottom:3px solid #f2f2f2;*/
  107. }
  108. .lid:before{
  109. width:10px;
  110. height:10px;
  111. top:70px;
  112. left:133px;
  113. background-image:radial-gradient(ellipse at center, #959595 0%, #0d0d0d 46%, #010101 50%, #0a0a0a 53%, #4e4e4e 76%, #383838 87%, #1b1b1b 100%);
  114. border-radius:100%;
  115. }
  116. .hover{
  117. z-index:1;
  118. text-align:center;
  119. margin-top:30%;
  120. color:rgba(255,255,255,0.9);
  121. text-shadow:0 1px 1px rgba(0,0,0,0.8);
  122. font: 20px "微软雅黑";
  123. }
  124. .hover:before{
  125. content:"";
  126. z-index:1;
  127. width:0;
  128. height:0;
  129. top:-50px;
  130. left:20px;
  131. border-bottom:35px solid #f2f2f2;
  132. /*border-left:10px solid transparent;*/
  133. }
  134. @-webkit-keyframes flame{
  135. 0%{
  136. height:70px;
  137. transform:skewY(0deg);
  138. border-radius:100% 100% 0 0;
  139. }
  140. 25%{
  141. height:60px;
  142. transform:skewY(40deg);
  143. border-radius:10% 100% 0 0;
  144. }
  145. 60%{
  146. height:65px;
  147. transform:skewY(-20deg);
  148. border-radius:90% 10% 0 0;
  149. }
  150. 70%{
  151. height:50px;
  152. transform:skewY(10deg);
  153. border-radius:10% 100% 0 0;
  154. }
  155. 100%{
  156. height:70px;
  157. tranform:skewY(0deg);
  158. }
  159. }
  160. @-webkit-keyframes lidoff{
  161. from{
  162. transform:rotate(0deg);
  163. transform-origin:100% 100%;
  164. }
  165. to{
  166. transform:rotate(130deg);
  167. transform-origin:100% 100%;
  168. }
  169. }</span>

初始照片:

动态照片

动画效果主要有两个,一是当鼠标悬停在“hover me”上面时打火机盖子打开的情形,由下面的动画代码实现:

  1. <span style="font-family:Comic Sans MS;font-size:12px;">@-webkit-keyframes lidoff{ 注意这里要加上-webkit-
  2. from{
  3. transform:rotate(0deg);
  4. transform-origin:100% 100%;
  5. }
  6. to{
  7. transform:rotate(130deg);
  8. transform-origin:100% 100%;
  9. }
  10. }
  11. </span>

第二个动画是蜡烛灯光效果的实现:蜡烛由按变量是由transition:opacity 3s linear; 实现的,

蜡烛灯光的随风摆动效果是由下面的代码实现的:

  1. <span style="font-family:Comic Sans MS;font-size:12px;">@-webkit-keyframes lidoff{
  2. from{
  3. transform:rotate(0deg);
  4. transform-origin:100% 100%;
  5. }
  6. to{
  7. transform:rotate(130deg);
  8. transform-origin:100% 100%;
  9. }
  10. </span>

将打火机盖与躯干分离的效果是由:box-shadow:inset 0 0 5px 5px #333; 主要是这里的inset(注意不要写成了insert)实现的。

当打火机打开时出现的圆球和白色的小正方形,是由:after :before 配合z-index实现的效果。

下面来介绍通过这个例子我的学到的几个属性需要注意的地方。

transform-origin: 默认的初始位置是图像的中心,100% 100%相当于 right bottom. 0% 0%相当于 left top.

before 和:after : 注入的内容是有关联的目标元素的字子元素,但它会被置于这个元素任何内容的“前” 或"后"。插入的内容在页面的源码里是看不见的,只能在css里看见。同时插入的元素在默认情况是内联元素。因此,为了给插入的元素赋予高度、填充、边距等等,通常必须显示的定义一个块级元素(display:block)。

此外,虽然可能不会插入任何内容,但是content必须写上,如果不插入任何内容,那么content写成 content:"";

IE6和IE7不兼容这两个为元素

伪元素(双冒号),css3中的伪类是(单冒号)

animation-fill-mode: none|forwards|backwards|both|initial|inherit;

规定当动画不播放时(当动画完成时,或当动画有一个延迟未开始播放时),要应用到元素的样式。

background-image:解决了p只能设置一个背景的问题,使一个p可以设置多个背景图片。

box-shadow:一个经常被遗忘的参数--spread(扩展阴影半径),这个曾经被遗忘的spread改变阴影的大小---其值可以是正负值,如果值为正,则整个阴影都延展扩大,反之是负值,则缩小。有了这个参数后可以使用box-shadow像photoshop中的阴影工作一样,制作单边阴影效果。这里注意一点,这个扩展阴影值需要和阴影模糊半径配合使用,一般情况是“扩展阴影半径一般设置为和模糊半径大小,并去其负值”

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持PHP中文网。

更多纯CSS3绘制打火机动画火焰效果示例代码相关文章请关注PHP中文网!

人气教程排行