// CSS代碼 @keyframes rotateAnimate { from { transform: rotate(0deg) skew(-30deg) } to { transform: rotate(360deg) skew(-30deg) } } .fan-wrapper { overflow: hidden; position: relative; margin: 100px; width: 200px; height: 200px; border-radius: 50%; background: red; } .fan { position: absolute; right: 0; animation: rotateAnimate 2s linear infinite; /* 這一行很重要,設置左下角爲旋轉點 */ transform-origin: 0% 100%; width: 100px; height: 100px; background: blue; } // HTML代碼 <div class="fan-wrapper"> <div class="fan"></div> </div>
// CSS代碼 .circles { position: relative; margin: 50px; width: 200px; height: 200px; } .circle { position: absolute; width: 20px; height: 20px; border-radius: 50%; background: black; } // HTML <div class="circles"> <div class="circle circle1"></div> <div class="circle circle2"></div> <div class="circle circle3"></div> <div class="circle circle4"></div> <div class="circle circle5"></div> <div class="circle circle6"></div> <div class="circle circle7"></div> <div class="circle circle8"></div> </div>
/** * R:大圓半徑,2*R = 外部正方形的邊長 * r:在大圓邊上等距排列的小圓的半徑 * counts: 圓的數量 * 返回值: * [ * [x1,y1], * [x2,y2], * ... * ] */ function calcXYs(R, r, counts) { // 當前度數 let deg = 0; // 單位度數,兩小圓和圓心的夾角 const pDeg = 360 / counts; // 存放返回結果 const arr = []; for (let i = 0; i < counts; i++) { // 度數以單位度數遞增 deg = pDeg * i; // Math.sin接收的參數以 π 爲單位,須要根據360度 = 2π進行轉化 const proportion = Math.PI / 180; // 之外部DIV左下角爲原點,計算小圓圓心的橫縱座標 let Y = R + R * Math.sin(proportion * deg); let X = R + R * Math.cos(proportion * deg); // 存放結果 arr.push([X, Y, deg]); } return arr; }
/** * R,r,counts:含義同上 * selector: 獲取全部小圓的標誌符 * 做用:根據上一步的座標計算結果,調整絕對定位的小圓的位置 */ function resizeCircles(selector, R, r, counts) { // 獲取全部小圓NodeList的選擇器 let list = document.querySelectorAll(selector); //調用calcXYs方法 const XYs = calcXYs(R, r, counts); // 遍歷每一個小圓的XY座標 for (let i = 0; i < list.length; i++) { const [X, Y] = XYs[i]; const e = list[i]; // 修改小圓距離外部DIV底部和左邊的距離 e.style.left = X + "px"; e.style.bottom = Y + "px"; } }
resizeCircles(".circle", 60, 20, 8);
@keyframes k { from { opacity: 1; } to { opacity: 0; } } .circle1 { animation: k 1s ease 0s alternate infinite; } .circle2 { animation: k 1s ease 0.2s alternate infinite; } .circle3 { animation: k 1s ease 0.4s alternate infinite; } // circle4 ~ circle8同理,delay以0.2s遞增
transition-timing-function: cubic-bezier(0.1, 0.7, 1.0, 0.1);
// HTML <div id="outer"> <div id="inner"></div> </div> <button id='btn'>拋物線效果</button>
// CSS #outer { transition: all 1.5s linear; } #inner { width: 30px; height: 30px; border-radius: 50%; background: red; transition: all 1.5s cubic-bezier(.54, .11, .95, .68); } .outer-active { transform: translateX(300px); } .inner-active { transform: translateY(300px) scale(0.3); }
JS
document.getElementById("btn").onclick = function() { document.getElementById("outer").classList.add("outer-active"); document.getElementById("inner").classList.add("inner-active"); };
// HTML <div class="marquee"> <p>ABCDEFGHIJKLMN</p> </div> // CSS @keyframes marquee { from { transform: translateX(-200px) } to { transform: translateX(200px) } } .marquee { overflow: hidden; margin: 100px; width: 200px; } .marquee p { animation: marquee 3s linear infinite; }
// HTML <div id="img-wrapper"> <img src='./timg.jpg' id='img1' class="img disable-img1" /> <img src='./timg2.jpg' id='img2' class="img" /> </div> // CSS #img-wrapper { perspective: 1200px; position: relative; height: 479px; } #img1, #img2 { position: absolute; transition: all 1s linear; backface-visibility: hidden; } #img1 { transform: rotateY(-180deg); } #img-wrapper:hover #img1 { transform: rotateY(-360deg); } #img-wrapper:hover #img2 { transform: rotateY(-180deg); }