国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網站 > WEB開發(fā) > 正文

通過javascript操作CSS3屬性實現動畫

2024-04-27 15:19:52
字體:
來源:轉載
供稿:網友

   CSS3提供兩種方式來實現動畫,transition與animation。animation涉及自定義一種為“@keyframes”的東西,這個需要動用到insertRule太復雜了,因此本文跳過它。有人它為transform也算一種,但它是靜態(tài)的,需要結合transition才能變成動態(tài),因此也跳過。

  transition主要就是以下四個屬性,后面跟著的是它們的初始值

  transition-property: all;

  transition-duration: 0s;

  transition-timing-function: ease;

  transition-delay: 0s;

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

  當前可進行補間的CSS屬性(比MDC上的少,去掉許多私有屬性與比較罕見的屬性)

 

  transition-duration,動畫的持續(xù)時間,其值為一個帶單位的數值,單位可以為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> 
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 铁岭县| 建始县| 西乡县| 商城县| 克什克腾旗| 招远市| 中卫市| 什邡市| 上饶县| 织金县| 临朐县| 依兰县| 舞阳县| 蓝田县| 普洱| 大洼县| 井研县| 乌海市| 仁布县| 南充市| 巨鹿县| 保康县| 门头沟区| 滨海县| 东丰县| 内黄县| 军事| 平江县| 九寨沟县| 张家口市| 福鼎市| 平泉县| 交城县| 商城县| 贞丰县| 富阳市| 武隆县| 洪洞县| 太康县| 石渠县| 丹阳市|