1. 過渡 transitionjavascript
容許 CSS 的屬性值在必定時間內平滑的過渡,css
在鼠標點擊,鼠標滑過或對屬性改變中觸發,並圓滑的改變 CSS 的屬性值html
簡寫屬性:java
#box { width: 300px; height: 200px; background-color: red; transition: property timing-function duration delay; } /* 沒有順序限制 可是必須先寫 過渡,再寫 延遲 即 先寫 duration , 再寫 delay transition: 1s; 等同於 trasition: 1s all ease 0; */ #box:hover { width: 200px; height: 300px; background-color: blue; }
transition-property: width;
transition-duration: 1s;
transition-duration: 1000ms;
transition-timing-function: cubic-bezier(.17,.67,.82,.35);
#box { width: 300px; height: 200px; background-color: red; transition: 2s 3s width linear, background 2s linear; } /* (面試題) transition: width 3s linear 2s, 2s; // 不會有延遲 2s,馬上執行 3s 勻速過渡動畫 (1)過渡的覆蓋問題,至關於: transition: all 2s ease 0s; */
/************** 幽靈邊框 ***************/ .ghost_border { position: relative; top: 0px; left: 0px; } .ghost_border::before, .ghost_border::after { content:""; position: absolute; display: block; width: 0; height: 20%; background-color: olive; transition: 1s all ease 0s; } .ghost_border::before { top: 0; left: 0; } .ghost_border::after { bottom: 0; right: 0; } body .ghost_border:hover::before, body .ghost_border:hover::after { width: 100%; }
2. 2D 變換面試
(面試題)display: inline 元素不支持 transform:即不支持 2D 變換,也不支持 3D變換app
儘管元素外形發生變化 ,其在文檔流的位置不變。 即不影響周圍元素佈局佈局
#box { width: 200px; height: 200px; transform: rotate(0deg); /* 設置基準點爲元素左上角 */ transform-origin: 0px 0px; /* 默認值 */ transform-origin: center; transform-origin: 100% center; transform-origin: 100%; transform-origin: 100% 100%; transform-origin: 100px center; transform-origin: 100px; transform-origin: 100px 100px; /* 關鍵字 */ transform-origin: top left; transform-origin: top; transform-origin: right; transform-origin: bottom; transform-origin: left; }
/*百分比參照於自身 border-radius: background-size: background-origin: transform: translate(-50%, -50%); */
/* 全部場景可用 */ #box { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); } /* 前提條件: width 和 height 必須已知 */ #box { width: 200px; height: 200px; position: absolute; top: 50%; left: 50%; margin: -100px 0 0 -100px; } #box { width: 200px; height: 200px; position: absolute; top: 50%; left: 50%; margin: -50% 0 0 -50%; }
#box { width: 200px; height: 200px; background-color: olive; // 效果1 移動 200px 再縮小 0.5 transform: translate(200px) scale(0.5); // 效果2 縮小 0.5,移動 100px transform: scale(0.5) translate(200px); /* x至關於 transform: translate(100px) scale(0.5); */ } /* scale 發生縮放時,
會將後面的 translate 的數值也會進行縮放
對於後面的 rotate,skew 無影響
rotate 發生旋轉時,
會將後面的 translate 的 xy 座標軸也旋轉
會將後面的 skew 的扭曲軸進行旋轉
skew 發生扭曲時,
會將後面的 translate 的 xy 座標軸也扭曲 */
3. 瓶體旋轉:
動畫
... ... <style type="text/css"> body { width: 100%; color: #000; background: #fff; font: 14px Helvetica, Arial, sans-serif; } /**** Cola Can Scroll ****/ #can_box img { display: block; /* 解決圖片留白 */ } #can_bg img { float: left; /* 兩張圖片在一行 */ } #can_box { position: relative; margin: 150px auto; overflow: hidden; } #can_bg { position: absolute; top: 10px; left: 0px; } #can_body { position: absolute; top: 0px; left: 0px; } </style> </head> <body> <div id="can_box"> <div id="can_bg"> <img id="cola_bg" src="./img/cola_bg.jpg"/> <img src="./img/cola_bg.jpg"/> </div> <div id="can_body"> <img id="cola_can" src="./img/cola_can.png"/> </div> </div> <!-- javascript 代碼 --> <script type="text/javascript"> window.onload = function(){ var canBox = document.getElementById("can_box"); var canBg = document.getElementById("can_bg"); var colaBg = document.getElementById("cola_bg"); var colaCan = document.getElementById("cola_can"); canBox.style.width = colaCan.offsetWidth+"px"; canBox.style.height = colaCan.offsetHeight+"px"; canBg.style.width = colaBg.offsetWidth* 2+"px"; canBg.style.height = colaCan.offsetHeight+"px"; var canLeft = 0; window.setInterval(function(){ canLeft -= 1; if(canLeft <= -colaBg.offsetWidth){ canLeft = 0; } canBg.style.transform = "translate("+canLeft+"px)"; },20); }; </script> ... ...
4. 動態時鐘spa
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title>Timing Clock</title> <link rel="stylesheet" type="text/css" href="./css/index.css" /> <script type="text/javascript" src="./js/kjfFunctions.js"></script> <script type="text/javascript" src="./js/index.js"></script> <style type="text/css"> body { width: 100%; color: #000; background: #96b377; font: 14px Helvetica, Arial, sans-serif; } /**** Timing clock ****/ #clock_dial { position: relative; width: 300px; height: 300px; margin: 300px auto 0; border-radius: 50%; background: olive; } #clock_second { position: absolute; top: 30px; left: 149px; transform-origin: 1px 120px; width: 2px; height: 120px; background-color: red; } #clock_minute { position: absolute; top: 70px; left: 148px; transform-origin: 2px 80px; width: 4px; height: 80px; background-color: green; } #clock_hour { position: absolute; top: 100px; left: 147px; transform-origin: 3px 50px; width: 6px; height: 50px; background-color: blue; } #clock_core { position: absolute; top: 145px; left: 145px; width: 10px; height: 10px; border-radius: 50%; background-color: pink; } .clock_scale { position: absolute; top: 0px; left: 149px; transform-origin: 1px 150px; width: 2px; height: 5px; background-color: #68ea94; } .clock_scale:nth-child(5n) { transform-origin: 2px 150px; width: 4px; height: 10px; background-color: #68ea94; } </style> </head> <body> <div id="clock_dial"> <div id="clock_second"> </div> <div id="clock_minute"> </div> <div id="clock_hour"> </div> <div id="clock_core"> </div> </div> <!-- javascript 代碼 --> <script type="text/javascript"> /**** 畫刻度 ****/ var clockDial = document.getElementById("clock_dial"); var i = 0; for(i=0; i<=59; i++){ var firstDiv = document.createElement("div"); firstDiv.className = "clock_scale"; clockDial.appendChild(firstDiv); firstDiv.style.transform = "rotate("+ 6*i +"deg)"; } /**** 動起來 ****/ var clockSecond = document.getElementById("clock_second"); var clockMinute = document.getElementById("clock_minute"); var clockHour = document.getElementById("clock_hour"); window.setInterval(function(){ var nowTime = new Date(); s = nowTime.getSeconds(); m = nowTime.getMinutes()+s/60; h = nowTime.getHours()+m/60; clockSecond.style.transform = "rotate("+s*6+"deg)"; clockMinute.style.transform = "rotate("+m*6+"deg)"; clockHour.style.transform = "rotate("+h*30+"deg)"; },1000); </script> </body> </html>