是一個純CSS實現(xiàn)的Loading特效,包括兩個方塊繞圈的動畫效果。該特效可以被用于網站、應用程序等需要加載時的等待效果。
使用CSS3的@keyframes屬性實現(xiàn)動畫效果。通過調整,可以實現(xiàn)不同的動畫效果。在這個Loading特效中,我們使用了transform屬性來旋轉、移動和縮放方塊,從而使得兩個方塊可以錯開繞圈。
<div class="loading">
<div></div>
<div></div>
</div>
<style>
.loading,
.loading > div {
position: relative;
box-sizing: border-box;
}
.loading {
display: block;
font-size: 0;
color: #000;
}
.loading.la-dark {
color: #333;
}
.loading > div {
display: inline-block;
float: none;
background-color: currentColor;
border: 0 solid currentColor;
}
.loading {
width: 32px;
height: 32px;
}
.loading > div {
position: absolute;
top: 0;
left: 0;
width: 14px;
height: 14px;
margin-top: -7px;
margin-left: -7px;
border-radius: 0;
animation: cube-transition 1.6s 0s infinite ease-in-out;
}
.loading > div:last-child {
animation-delay: -0.8s;
}
.loading.la-sm {
width: 16px;
height: 16px;
}
.loading.la-sm > div {
width: 6px;
height: 6px;
margin-top: -3px;
margin-left: -3px;
}
.loading.la-2x {
width: 64px;
height: 64px;
}
.loading.la-2x > div {
width: 28px;
height: 28px;
margin-top: -14px;
margin-left: -14px;
}
.loading.la-3x {
width: 96px;
height: 96px;
}
.loading.la-3x > div {
width: 42px;
height: 42px;
margin-top: -21px;
margin-left: -21px;
}
@keyframes cube-transition {
25% {
top: 0;
left: 100%;
transform: scale(0.5) rotate(-90deg);
}
50% {
top: 100%;
left: 100%;
transform: scale(1) rotate(-180deg);
}
75% {
top: 100%;
left: 0;
transform: scale(0.5) rotate(-270deg);
}
100% {
top: 0;
left: 0;
transform: scale(1) rotate(-360deg);
}
}
</style>
這個純CSS實現(xiàn)的倆方塊繞圈的Loading特效簡單易用,可以為您的網站或應用程序增添一份動感和趣味。希望這個特效對您有所幫助!
1.先定義html標簽,.container就是底層大盒子,.item就是一個小方塊,后面將通過js快速生成:
<div class="container">
<div class="item"></div>
</div>
2.定義全局css樣式,.container的css樣式:
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.container {
position: fixed;
width: 100vw;
height: 100vh;
background-color: rgb(32, 32, 32);
display: flex;
flex-wrap: wrap;
}
vw (viewport width) vh (viewport height) 是視窗的大小,100vw = 100%視窗寬度 100vh = 100%視窗高度;
position:fixed;固定定位;
display: flex; flex布局;
flex-wrap: wrap; 換行;
3.每個小方塊.item的css樣式,并設置animation動畫效果:
.item {
width: 5vw;
height: 5vh;
background-color: white;
animation: move 1.5s ease-in-out forwards;
opacity: 0;
overflow: hidden;
}
@keyframes move {
0% {
opacity: 0;
transform: scale(0) translateY(1000px);
border-radius: 50%;
}
100% {
opacity: 1;
transform: scale(1) translateY(0);
background-image: url("img/52.jpg");
background-attachment: fixed;
background-size: cover;
border-radius: 0;
}
}
animation: move 1.5s ease-in-out forwards; 其中的forwards表示保持動畫結束的狀態(tài)效果;
opacity:0;透明度為0;
transform: scale(0) translateY(1000px); scale為縮放,translateY為Y軸方向偏移;
background-attachment:fixed;當頁面的其余部分滾動時,背景圖像不會移動;
background-size: cover; 把背景圖像擴展至足夠大,以使背景圖像完全覆蓋背景區(qū)域。
4.js快速生成多個小方塊:
?因為每個小方塊大小是5×5,而.container大小為100×100,所以需要400個小方塊,標簽已有一個,還需399個。
// 獲取元素
var container = document.querySelector(".container");
var items = document.getElementsByClassName("item");
for (let i = 1; i < 400; i++) {
// 創(chuàng)建小方塊div
container.innerHTML += "<div class='item'></div>";
//設置css樣式設置動畫延遲
items[i].style.cssText = `animation-delay: ${i * 0.05}s;`;
}
?注意不能用itemsquerySelectorAll獲取全部小方塊,因為得到的是一個靜態(tài)列表,它不會對dom結構進行動態(tài)查詢,不會再次重新獲取。getElementsByClassName是動態(tài)查詢的過程,會隨著dom結構的變化,得到的結點列表也會發(fā)生變化。
也可以通過以下方法創(chuàng)建小方塊:
var a = document.createElement("div");
a.classList.add("item");
container.appendChild(a);
家好,本篇文章分享小方塊來回旋轉動畫特效,歡迎參考和指正。
效果圖:
小方塊來回旋轉動畫特效
HTML代碼:
<div class="tui-demo-2"></div>
CSS代碼:
<style type="text/css"> .tui-demo-2 { width: 100px; height: 100px; margin: 50px auto; background: #FFB800; animation: tui-demo-2 2.0s infinite; } @keyframes tui-demo-2 { 0% { transform: rotate(0deg); } 50% { transform: rotate(90deg); } 100% { transform: rotate(0deg); } } </style>
知識點:
animation:是CSS3的動畫屬性,這里把animation綁定到tui-demo-2元素上,并指定該動畫需要2.0秒完成,infinite則表示無限次播放該動畫。
@keyframes:該規(guī)則表示動畫可以逐步從一個CSS樣式改變?yōu)榱硪粋€CSS樣式。這里0%是動畫開頭,100%是動畫結束。
transform:該屬性應用于2D元素或3D元素的轉換,允許元素發(fā)生旋轉,縮放,移動,傾斜等效果。rotate定義2D旋轉,可以配置旋轉角度。
最后注意下瀏覽器兼容問題,-webkit-,-ms-或-moz-,有問題可以留言,大家一起學習HTML+CSS基礎入門開發(fā)。
*請認真填寫需求信息,我們會在24小時內與您取得聯(lián)系。