十年網(wǎng)站開發(fā)經(jīng)驗 + 多家企業(yè)客戶 + 靠譜的建站團隊
量身定制 + 運營維護+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
這篇文章主要介紹了怎么使用純CSS實現(xiàn)一只紙鶴,具有一定借鑒價值,需要的朋友可以參考下。希望大家閱讀完這篇文章后大有收獲。下面讓小編帶著大家一起了解一下。
https://github.com/comehope/front-end-daily-challenges
定義 dom,容器中包含 6 個元素,分別代表頭、頸、身體側面、翅、尾、胸:
居中顯示:
body { margin: 0; height: 100vh; display: flex; align-items: center; justify-content: center; background-color: dodgerblue; }
定義容器尺寸:
.cranes { width: 52em; height: 50em; font-size: 7px; }
設置紙鶴的顏色為白色:
.cranes { color: white; }
畫出頭部:
.cranes { position: relative; } .head { border-left: 13em solid transparent; border-right: 6em solid transparent; border-bottom: 2em solid; position: absolute; left: 0; top: 21; transform: rotate(-5deg); }
把以上創(chuàng)建三角形的代碼抽象成一個模板,然后數(shù)據(jù)都改為變量,類似于調用函數(shù)的樣子:
.cranes span { border-left: calc(var(--left) * 1em) solid transparent; border-right: calc(var(--right) * 1em) solid transparent; border-bottom: calc(var(--bottom) * 1em) solid; position: absolute; transform: rotate(calc(var(--rotation) * 1deg)); left: calc(var(--x) * 1em); top: calc(var(--y) * 1em); } .head { --left: 13; --right: 6; --bottom: 2; --x: 0; --y: 21; --rotation: -5; }
設置透明度,以便元素疊加處有折紙效果:
.cranes span { filter: opacity(0.6); }
接下來就是逐個調用生成三角形的函數(shù)創(chuàng)建其他三角形:
頸:
.neck { --left: 6; --right: 6; --bottom: 12; --x: 14; --y: 19; --rotation: 75; }
身體側面:
.side { --left: 1.5; --right: 11.5; --bottom: 20; --x: 18.8; --y: 15.1; --rotation: 20; }
翅:
.wing { --left: 18.7; --right: 30; --bottom: 8; --x: 6.7; --y: 9.2; --rotation: -41.9; }
尾:
.tail { --left: 18.6; --right: 7.7; --bottom: 3.9; --x: 19.6; --y: 38.1; --rotation: -126.5; }
胸:
.belly { --left: 6.2; --right: 1.8; --bottom: 11.5; --x: 17.5; --y: 27.8; --rotation: -99; }
至此,紙鶴畫完。
最后,增加一點交互效果,當鼠標懸停時,由等腰直角三角形變形成鶴:
.cranes:hover span { animation: appear 1s ease-in; } @keyframes appear { from { border-left: 3em solid transparent; border-right: 3em solid transparent; border-bottom: 3em solid; position: absolute; transform: rotate(0deg); left: calc((52em - 3em) / 2); top: calc((50em - 3em) / 2); } }
感謝你能夠認真閱讀完這篇文章,希望小編分享怎么使用純CSS實現(xiàn)一只紙鶴內(nèi)容對大家有幫助,同時也希望大家多多支持創(chuàng)新互聯(lián)成都網(wǎng)站設計公司,關注創(chuàng)新互聯(lián)成都網(wǎng)站設計公司行業(yè)資訊頻道,遇到問題就找創(chuàng)新互聯(lián)成都網(wǎng)站設計公司,詳細的解決方法等著你來學習!
另外有需要云服務器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、網(wǎng)站設計器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。