首页 > 设计 > WEB标准 > 正文

通过javascript操作CSS3属性实现动画

2018-10-16 20:54:34
字体:
来源:转载
供稿:网友

   CSS3提供两种方式来实现动画,transition与animation。animation涉及自定义一种为“@keyframes”的东西,这个需要动用到insertRule太复杂了,因此本文跳过它。有人它为transform也算一种,但它是静态的,需要结合transition才能变成动态,因此也跳过。

  transition主要就是以下四个属性,后面跟着的是它们的初始值

  transition-property: all;

  transition-duration: 0s;

  transition-timing-function: ease;

  transition-delay: 0s;

  transition-property的值可以为none,all,或指定上的属性名

  当前可进行补间的CSS属性(比MDC上的少,去掉许多私有属性与比较罕见的属性)

 

  transition-duration,动画的持续时间,其值为一个带单位的数值,单位可以为s与ms

  transition-delay:动画延迟多久开始.

  transition-timing-function:缓动公式,值为ease | linear | ease-in | ease-out | ease-in-out | cubic-bezier(, , , )

  ease

  This keyword sets the easing function to cubic-bezier(0.25, 0.1, 0.25, 1.0).

  linear

  This keyword sets the easing function to cubic-bezier(0.0, 0.0, 1.0, 1.0).

  ease-in

  This keyword sets the easing function to cubic-bezier(0.42, 0.0, 1.0, 1.0).

  ease-out

  This keyword sets the easing function to cubic-bezier(0.0, 0.0, 0.58, 1.0).

  ease-in-out

  This keyword sets the easing function to cubic-bezier(0.42, 0.0, 0.58, 1.0).

  cubic-bezier

  Specifies a cubic bezier curve to use as the easing function. The four number values specify the P1 and P2 points of the curve as (x1, y1, x2, y2). All values must be in the range [0.0, 1.0] inclusive.

  但在JS操作它们时我们其中只需要transition就行了,由于这是浏览器商首先搞出来,因此都带着它们的前缀,如-ms-,-moz-等等,我们需要把它们改成驼峰风格才能调用,见下面的例子。

  示例1,通过JS来操作这些CSS3属性实现动画效果:

  1.   
  2. <!DOCTYPE html> 
  3. <html> 
  4.   <head> 
  5.     <meta charset="utf-8"
  6.     <title>dom Framework</title> 
  7.   <script> 
  8.       var dom = function(s){ 
  9.         return document.getElementById(s) 
  10.       } 
  11.       dom.cssName = function (name){ 
  12.         var prefixes = ['''-ms-','-moz-''-webkit-''-khtml-''-o-'], 
  13.         rcap = /-([a-z])/g,capfn = function($0,$1){ 
  14.           return $1.toUpperCase(); 
  15.         }; 
  16.         dom.cssName = function(name, target, test){ 
  17.           target = target || document.documentElement.style; 
  18.           for (var i=0, l=prefixes.length; i < l; i++) { 
  19.             test = (prefixes[i] + name).replace(rcap,capfn); 
  20.             if(test in target){ 
  21.               return test; 
  22.             } 
  23.           } 
  24.           return null
  25.         } 
  26.         return dom.cssName(name); 
  27.       } 
  28.       window.onload = function(){ 
  29.         var el = dom("test"), 
  30.         css3transition = dom.cssName("transition"); 
  31.         el.style[css3transition] = "all 5s ease-in" 
  32.         dom("start").onclick = function(){ 
  33.           el.style.width = "400px"
  34.         } 
  35.       } 
  36.     
  37.     </script> 
  38.     <style> 
  39.       #test{ 
  40.         background: red; 
  41.         width:10px; 
  42.         height:30px; 
  43.       } 
  44.     </style> 
  45.   </head> 
  46.   <body> 
  47.     <h3>CSS3 动画 by 司徒正美</h3> 
  48.     <div id="test"
  49.       TEXT 
  50.     </div> 
  51.     <button id="start" type="button">开始测试</button> 
  52.   </body> 
  53. </html> 
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表