之前在網上看到一些用純CSS3實現的酷炫效果,以為實現起來比較困難,於是想看看具體是怎么實現的。
一、笑臉貓動畫
實現效果如下:
這個實現起來確實比較麻煩,很多地方需要花時間,有耐心地調整。
1.先看下頁面結構:
<body> <div class="container"> <!-- 臉 --> <div class="face"> <!-- 頭發 --> <div class="hair"> <div></div> </div> <!-- 眼睛 --> <div class="eye-wrap"> <div class="eye left"> <div class="eye-circle"> <div class="eye-core"></div> </div> <div class="eye-bottom"></div> <div class="eye-red"></div> </div> <div class="eye right"> <div class="eye-circle"> <div class="eye-core"></div> </div> <div class="eye-bottom"></div> <div class="eye-red"></div> </div> </div> <!-- 鼻子 --> <div class="nose"> </div> <!-- 嘴巴 --> <div class="mouth-wrap"> <!-- <div class="mouth-top"></div> --> <div class="mouth left"></div> <div class="mouth right"></div> </div> <!-- 胡子 --> <div class="mustache-wrap"> <div class="mustache left"> <div></div> <div></div> <div></div> <div></div> <div></div> </div> <div class="mustache right"> <div></div> <div></div> <div></div> <div></div> <div></div> </div> </div> </div> <!-- 耳朵 --> <div class="ear-wrap"> <div class="ear left"> </div> <div class="ear right"> </div> </div> </div> </body>
2.再看css部分
1.先看臉部face:
.face { /* top: 100px; */ left: 50%; top: 50%; position: absolute; width: 400px; height: 340px; margin-left: -200px; margin-top: -170px; border-radius: 50% 50% 35% 35%; border: 2px solid #000; z-index: 10; background: #f3f3f3; overflow: hidden; }
主要是要畫出橢圓形,width和height設置的值要注意,臉比較寬。然后關鍵就是border-radius的設置了:
border-radius全部設置為0時,就是一個長方形,然后把它的四個角對應的設置下,就可以出現我們想要的效果。
2.接下來看耳朵的設置:
耳朵看起來其實也是一個橢圓,通過設置border-radius,調出想要的效果。不過需要被遮住一部分,所以在HTML結構中,我把它放在face的外面了。

/* 耳朵 */ .ear-wrap { position: absolute; width: 400px; top: 100px; left: 50%; margin-left: -200px; } .ear { width: 160px; height: 200px; position: absolute; top: -22px; left: 0; border: 2px solid #000; background: #f3f3f3; transform: rotate(-15deg); -ms-transform: rotate(-15deg); -moz-transform: rotate(-15deg); -webkit-transform: rotate(-15deg); -o-transform: rotate(-15deg); border-radius: 4% 80% 0% 50%; transition: all 1s; } .ear-wrap .right { left: auto; right: 0; border-radius: 80% 4% 50% 0%; transform: rotate(15deg); -ms-transform: rotate(15deg); -moz-transform: rotate(15deg); -webkit-transform: rotate(15deg); -o-transform: rotate(15deg); }
這是豎起來的耳朵,通過 transform:rotate(15deg); 旋轉一點,耳朵就塌下來了。
3.然后是頭發:
有了上述的實踐,知道頭發也好弄了,還是橢圓

/* 頭發 */ .hair { position: absolute; width: 180px; height: 160px; left: 50%; margin-left: -90px; background: #8d8d8d; overflow: hidden; border-radius: 0 0 50% 50%; } .hair div { width: 90px; height: 160px; background: #f0ac6b; }
另外一個顏色,只需要在里面加一個子div,然后給不同的顏色就ok。
4.再來看眼睛:
眼睛比較復雜,拆分四個小部件,眼眶,眼珠,下眼線,眼紅(笑起來露出的表情),對應的效果設置為:

/* 眼睛 */ .eye-wrap { position: absolute; width: 300px; height: 60px; top: 200px; left: 50%; margin-left: -150px; overflow: hidden; } .eye-wrap .eye { height: 100px; width: 100px; position: absolute; } .eye-wrap .eye-circle { width: 100px; height: 100px; border: 2px solid #000; overflow: hidden; position: absolute; border-radius: 50%; box-sizing: border-box; } .eye-wrap .eye-core { height: 100px; width: 30px; /* margin: 0 auto; */ background: #000; position: absolute; left: 50%; margin-left: -15px; transition: all 1s; } .eye-wrap .eye-bottom { height: 50px; width: 160px; border-radius: 50%; position: absolute; /* background: #000; */ margin-top: 50px; border-top: 2px solid #000; left: -30px; background: #f6f7f2; transition: all 1s; } .eye-wrap .right { left: auto; right: 0px; } .eye-red { position: absolute; height: 28px; width: 70px; background: red; top: 34px; /*top: 64px;*/ left: 18px; border-radius: 50% 50% 50% 50%; background-image: -moz-radial-gradient( 50% 50%, rgba(253,214,240,0.8) 0%, rgba(253,224,244,0.8) 66%, rgba(253,234,247,0.8) 100%); background-image: -webkit-radial-gradient( 50% 50%, rgba(253,214,240,0.8) 0%, rgba(253,224,244,0.8) 66%, rgba(253,234,247,0.8) 100%); background-image: -ms-radial-gradient( 50% 50%, rgba(253,214,240,0.8) 0%, rgba(253,224,244,0.8) 66%, rgba(253,234,247,0.8) 100%); opacity: 0.0; /* transition: all 1.5s; */ transition: all 0.1s ease-in 0.2s; }
注意左右兩只眼睛效果大致是一樣,只是位置不一樣,這是我們只需要設置右邊的 right: 0px; 就可以(因為設置了position: absolute)
5.鼻子:
/* 鼻子 */ .nose { width: 30px; height: 10px; /* background: #000; */ border-bottom: 8px solid #000; border-radius: 0% 0% 50% 50%; top: 250px; left: 50%; margin-left: -15px; position: absolute; }
這里特別注意,height:10px; border-bottom: 8px solid #000;的設置,我本來想只用height:10px;然后設置border-radius,可是效果卻長這樣:
沒有半圓的效果,是高度設置不夠?變成18px,這貨長這樣
,也不對,所以就加上border-bottom
6.嘴巴:

/* 嘴巴 */ .mouth-wrap { position: absolute; top: 268px; width: 100px; left: 50%; margin-left: -50px; height: 20px; overflow: hidden; } .mouth { width: 50px; height: 40px; border-bottom: 4px solid #000; border-right: 4px solid #000; border-radius: 0% 40% 50% 20%; margin-top: -26px; position: absolute; left: 0; transition: all 1s; } .mouth-wrap .right { border-bottom: 4px solid #000; border-right: none; border-left: 4px solid #000; border-radius: 40% 0% 20% 50%; position: absolute; left: auto; right: 0; }
算是比較簡單,設置border-bottom,border-left或border-right就可以。嘴巴上翹的效果是改變其border-radius值
7.兩邊的胡子:

/* 胡子 */ .mustache-wrap { height: 80px; width: 380px; position: absolute; top: 190px; z-index: 20; left: 50%; margin-left: -190px; } .mustache > div:first-child { width: 30px; height: 10px; border-top: 6px #E53941 solid; border-radius: 30% 50% 20% 50%; transform: rotate(25deg); -ms-transform: rotate(25deg); -moz-transform: rotate(25deg); -webkit-transform: rotate(25deg); -o-transform: rotate(25deg); margin-left: 20px; } .mustache > div:nth-child(2) { width: 20px; height: 6px; background-color: #E53941; border-radius: 50% 50% 50% 50%; transform: rotate(25deg); -ms-transform: rotate(25deg); -moz-transform: rotate(25deg); -webkit-transform: rotate(25deg); -o-transform: rotate(25deg); margin-left: 20px; } .mustache > div:nth-child(3) { /*margin-top:10px;*/ width: 32px; height: 10px; border-bottom: 4px #E53941 solid; border-radius: 30% 0% 50% 30%; transform: rotate(25deg); -ms-transform: rotate(25deg); -moz-transform: rotate(25deg); -webkit-transform: rotate(25deg); -o-transform: rotate(25deg); margin-left: 8px; } .mustache > div:nth-child(4) { margin-top: 20px; width: 26px; height: 20px; border-bottom: 4px #E53941 solid; border-radius: 30% 0% 50% 30%; transform: rotate(30deg); -ms-transform: rotate(30deg); -moz-transform: rotate(30deg); -webkit-transform: rotate(30deg); -o-transform: rotate(30deg); margin-left: 28px; } .mustache > div:last-child { width: 22px; height: 10px; border-bottom: 3px #E53941 solid; border-radius: 0% 0% 50% 50%; transform: rotate(-15deg); -ms-transform: rotate(-15deg); -moz-transform: rotate(-15deg); -webkit-transform: rotate(-15deg); -o-transform: rotate(-15deg); margin-left: 40px; margin-top: -8px; } /*右邊胡子*/ .mustache-wrap .right { transform: rotateY(180deg); -webkit-transform: rotateY(180deg); /* Safari 和 Chrome */ -moz-transform: rotateY(180deg); /* Firefox */ margin-top: -85px; }
雖然比較麻煩,不過也算是簡單的圖形,只需要把height,width,border-top,border-radius,transform: rotate這些值設置恰當就好。
8.鼠標hover樣式:

/*鼠標hover樣式*/ .container:hover .ear-wrap > div:first-child { /* left: -10px; */ transform: rotate(0deg); -ms-transform: rotate(0deg); -moz-transform: rotate(0deg); -webkit-transform: rotate(0deg); -o-transform: rotate(0deg); border-radius: 4% 80% 0% 60%; transition: all 1s; /*transition: transform 1s,left 1s;*/ } .container:hover .ear-wrap > div:last-child { /* right: -10px; */ transform: rotate(0deg); -ms-transform: rotate(0deg); -moz-transform: rotate(0deg); -webkit-transform: rotate(0deg); -o-transform: rotate(0deg); border-radius: 80% 4% 60% 0%; transition: all 1s; /*transition: transform 1s,right 1s;*/ } .container:hover .eye-bottom { margin-top: 30px; transition: all 1s; } .container:hover .eye-red { opacity: 1; transition: all 2.5s; } .container:hover .mouth { border-radius: 50%; transition: all 1s; } .container:hover .eye-core { width: 40px; margin-left: -20px; transition: all 1s; }
耳朵,嘴巴,眼睛等地方需要修改transform,border-radius,width等值的屬性。
3.線上效果及源碼
線上效果:點我呀
源碼:戳我看看
參考資料:再看看
二:立方體旋轉
效果:
HTML部分:
<body class="body"> <div class="rect-wrap"> <!-- //舞台元素,設置perspective,讓其子元素獲得透視效果。 --> <div class="container"> <!-- //容器,設置transform-style: preserve-3d,讓其子元素在3D空間呈現 --> <div class="top slide">1</div> <!-- //立方體的六個面 --> <div class="bottom slide">2</div> <div class="left slide">3</div> <div class="right slide">4</div> <div class="front slide">5</div> <div class="back slide">6</div> </div> </div> </body>
CSS:

<style> .rect-wrap { position: relative; perspective: 2000px; } .container { width: 400px; height: 400px; transform-style: preserve-3d; transform-origin: 50% 50% 100px; /* //設置3d空間的原點在平面中心再向Z軸移動200px的位置 */ /* left: 50%; margin-left: -200px; */ top: 100px; } .slide { width: 200px; height: 200px; position: absolute; background: #000; line-height: 200px; text-align: center; color: #fff; font-size: 30px; font-weight: bold; } .top { left: 100px; top: -100px; transform: rotateX(-90deg); transform-origin: bottom; background: red; } .bottom { left: 100px; bottom: -100px; transform: rotateX(90deg); transform-origin: top; background: grey; } .left { left: -100px; bottom: 100px; transform: rotateY(90deg); transform-origin: right; background: green; } .right { left: 300px; bottom: 100px; transform: rotateY(-90deg); transform-origin: left; background: yellow; } .front { left: 100px; top: 100px; transform: translateZ(200px); background: black; } .back { left: 100px; top: 100px; transform: translateZ(0);; background: blue; } @keyframes rotate-frame { 0% { transform: rotateX(0deg) rotateY(0deg); } 10% { transform: rotateX(0deg) rotateY(180deg); } 20% { transform: rotateX(-180deg) rotateY(180deg); } 30% { transform: rotateX(-360deg) rotateY(180deg); } 40% { transform: rotateX(-360deg) rotateY(360deg); } 50% { transform: rotateX(-180deg) rotateY(360deg); } 60% { transform: rotateX(90deg) rotateY(180deg); } 70% { transform: rotateX(0) rotateY(180deg); } 80% { transform: rotateX(90deg) rotateY(90deg); } 90% { transform: rotateX(90deg) rotateY(0); } 100% { transform: rotateX(0) rotateY(0); } } .container{ animation: rotate-frame 30s linear infinite; } </style>
1.3維空間圖
電腦屏幕中心為原點,橫向為X軸,縱向為Y軸,人臉的方向為Z軸;
translate(x,y)、translateX(x)、translateY(y)、translateZ(z)、translate3d(x,y,z):定義位置的
移動距離
rotate(angle)、rotateX(a)、rotateY(a)、rotateZ(a)、rotate3d(x,y,z,angle):定義元素的
旋轉角度
。
2.perspective屬性
perspective 屬性定義 3D 元素距視圖的距離,以像素計。該屬性允許您改變 3D 元素查看 3D 元素的視圖。
當為元素定義 perspective 屬性時,其子元素會獲得透視效果,而不是元素本身。
注釋:perspective 屬性只影響 3D 轉換元素。
提示:請與 perspective-origin 屬性一同使用該屬性,這樣您就能夠改變 3D 元素的底部位置。
3.transform-style屬性
transform-style: flat|preserve-3d; 默認值為flat,表示子元素以2D平面呈現;perserve-3d表示子元素以3D平面呈現
4.transform-origin屬性
transform-origin 屬性允許您改變被轉換元素的位置(可以理解為元素以哪個位置為旋轉原點)。
語法:
transform-origin: x-axis y-axis z-axis;
屬性演示效果: 戳我
默認值為: 50% 50% 0
5.線上效果及源碼
線上效果:點我呀
源代碼:戳我看看
參考資料:再看看
三、酷炫button
效果:(渣渣像素(/ □ \))
看下Twitter 做的 立體按鈕效果,類似翻蓋的動作是怎么做的。
HTML部分:
<body class="body"> <section> <div class="button"> hello, welcome to the new world! </div> <div class="cover"> <div class="innie"></div> <div class="spine"></div> <div class="outie"></div> </div> <div class="shadow"></div> </section> </body>
CSS部分:

<style> section, section div { transition-duration: .6s; } * { box-sizing: border-box; } html, body { height: 100%; } body { display: flex; flex-direction: column; justify-content: center; align-items: center; background-image: -webkit-radial-gradient(center top, circle farthest-corner, #FFFFFF 0%, #D8DFE9 100%); background-image: radial-gradient(circle farthest-corner at center top, #FFFFFF 0%, #D8DFE9 100%); overflow: hidden; } section, .button { transition-timing-function: ease; } section { display: inline-block; position: relative; padding: .375rem .375rem 0; height: 2.5rem; background: #A9ADB6; border-radius: .25rem; perspective: 300; box-shadow: 0 -1px 2px #fff, inset 0 1px 2px rgba(0,0,0,.2), inset 0 .25rem 1rem rgba(0,0,0,.1); } .button { opacity: 0; } .cover { position: absolute; top: 0; right: 0; bottom: 0; left: 0; transform-origin: center bottom; transform-style: preserve-3d; font: 1.25em/2 "icon"; color: white; text-align: center; pointer-events: none; z-index: 100; } .innie, .outie, .spine, .shadow { position: absolute; width: 100%; } .innie, .outie { height: 100%; background-image: -webkit-linear-gradient(top, transparent 0%, rgba(0,0,0,.1) 100%); border-radius: .25rem; } .innie:after, .outie:after { content:"t"; } .innie { background-color: #67E2FE; text-shadow: 0 -2px 4px rgba(0,0,0,.2); } .spine { top: .25rem; background: #20C7F3; height: .25rem; transform: rotateX(90deg); transform-origin: center top; } .shadow { top: 100%; left: 0; height: 3.5rem; transform-origin: center top; transform: rotateX(90deg); opacity: 0; z-index: 0; background-image: -webkit-linear-gradient(top, rgba(0,0,0,.6) 0%, transparent 100%); background-image: linear-gradient(to bottom, rgba(0,0,0,.6) 0%, transparent 100%); border-radius: .4rem; } .outie { background-color: #2EC8FA; transform: translateZ(.25rem); text-shadow: 0 2px 4px rgba(0,0,0,.2); } section:hover { background: #EBEFF2; } section:hover .button { opacity: 1; } section:hover .cover, section:hover .innie, section:hover .spine, section:hover .outie, section:hover .spine { transition-timing-function: cubic-bezier(.2,.7,.1,1.1); } section:hover .cover { transform: rotateX(-120deg); } section:hover .innie { background-color: #3ADAFC; } section:hover .spine { background-color: #52B1E0; } section:hover .outie { background-color: #2174A0; color: rgba(255,255,255,0); } section:hover .shadow { opacity: 1; transform: rotateX(45deg) scale(.95); } </style>
主要是一些效果的疊加,然后注意 transform-style: preserve-3d;設置3D效果。
線上效果及源碼
線上效果:點我呀
源代碼:戳我看看
四:3D照片牆
效果:
HTML部分:
<body class="body"> <div class="photo-wrap"> <!-- 舞台 --> <div class="container"> <!-- 容器 --> <div class="img">我是中心</div> <div class="img img01">1</div> <div class="img img02">2</div> <div class="img img03">3</div> <div class="img img04">4</div> <div class="img img05">5</div> <div class="img img06">6</div> <div class="img img07">7</div> <div class="img img08">8</div> <div class="img img09">9</div> </div> </div> </body>
CSS部分:

<style> @keyframes rotate-frame { 0% { transform: rotateX(-20deg) rotateY(0deg); } 10% { transform: rotateX(-20deg) rotateY(36deg); } 20% { transform: rotateX(-20deg) rotateY(72deg); } 30% { transform: rotateX(-20deg) rotateY(108deg); } 40% { transform: rotateX(-20deg) rotateY(144deg); } 50% { transform: rotateX(-20deg) rotateY(180deg); } 60% { transform: rotateX(-20deg) rotateY(216deg); } 70% { transform: rotateX(-20deg) rotateY(252deg); } 80% { transform: rotateX(-20deg) rotateY(288deg); } 90% { transform: rotateX(-20deg) rotateY(324deg); } 100% { transform: rotateX(-20deg) rotateY(360deg); } } body { background: #f9f9f9; } .photo-wrap { perspective: 800px; width: 800px; } .container { width: 800px; height: 500px; margin: 0 auto; position: relative; transform-style: preserve-3d; transform:rotateX(-10deg); animation: rotate-frame 10s linear infinite; } .img { width: 200px; height: 118px; line-height: 118px; text-align: center; position: absolute; top: 160px; left: 300px; box-shadow: 0 0 20px rgba(0, 0, 0, 0.9) inset; -webkit-box-reflect:below 3px -webkit-linear-gradient(top,rgba(0,0,0,0) 40%,rgba(0,0,0,0.5)); background: pink; background:-moz-linear-gradient(top, pink, rgba(0, 0, 255, 0.5)); background:-webkit-gradient(linear, 0 0, 0 bottom, from(#ccccff), to(rgba(174, 221, 129, 0.5))); background:-o-linear-gradient(top, pink, rgba(0, 0, 255, 0.5)); transform: rotateY(0deg); } .img01 { transform: rotateY(0deg) translateZ(300px); } .img02 { transform: rotateY(40deg) translateZ(300px); } .img03 { transform: rotateY(80deg) translateZ(300px); } .img04 { transform: rotateY(120deg) translateZ(300px); } .img05 { transform: rotateY(160deg) translateZ(300px); } .img06 { transform: rotateY(200deg) translateZ(300px); } .img07 { transform: rotateY(240deg) translateZ(300px); } .img08 { transform: rotateY(280deg) translateZ(300px); } .img09 { transform: rotateY(320deg) translateZ(300px); } </style>
1.倒影的實現
基本語法:
img { -webkit-box-reflect: below; }
offset屬性值定義圖片和倒影影像之間的間距:
img { -webkit-box-reflect: below 3px; }
給倒影增加消影效果:
-webkit-box-reflect:below 3px -webkit-linear-gradient(top,rgba(0,0,0,0) 40%,rgba(0,0,0,0.5));
2.線上效果及源碼
線上效果:點我呀
源代碼:戳我看看
補充:沒想到這篇文章不僅上了最多推薦,還上了編輯推薦,有點受寵若驚,自己感覺寫得還不夠用心,感謝大家的點贊,博主以后會繼續努力。