国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 双流县| 云安县| 遂平县| 安康市| 宁陵县| 肥城市| 嘉峪关市| 石城县| 鹰潭市| 霍城县| 陆丰市| 秦皇岛市| 宜良县| 通城县| 广水市| 绍兴市| 克山县| 右玉县| 广元市| 岳西县| 丽江市| 兴业县| 施甸县| 三亚市| 吴忠市| 延津县| 玛曲县| 马尔康县| 紫阳县| 玛纳斯县| 都兰县| 彰化市| 崇礼县| 漾濞| 伊吾县| 新密市| 子长县| 保靖县| 洛隆县| 襄垣县| 山东|