這次給大家帶來Css3的之形狀總結,使用Css3形狀的注意事項有哪些,下面就是實戰案例,一起來看一下。
一、自適應橢圓
border-radius特性:
範例程式碼:
.wrap{ border-radius: 50% / 30%; width: 60px; height: 80px; background: yellow; } .wrap02{ width: 60px; height: 80px; background: yellow; border-radius: 50% / 100% 100% 0 0; } .wrap03{ width: 60px; height: 80px; background: yellow; border-radius: 100% 0 0 0; }
二、平形四邊形
.wrap{ width: 80px; height: 40px; transform: skewX(-45deg); background: yellow; } .wrap>p{ transform: skewX(45deg); } .btn{ position: relative; padding: 10px; } .btn:before{ content: ''; position: absolute; top: 0px; left: 0px; right: 0px; bottom: 0px; z-index: -1; background: #85a; transform: skewX(-45deg); }
三、菱形
應用元素的嵌套,外層與內層元素相互roate旋轉,實現八角形;
設計內部元素max-width的寬度為100%,讓整個圖片最大隻能充滿整個外層的p;
scale屬性控制其圖片的放大倍數,預設為中心點為放大原點(會用到勾股定理,不要額外指定transform-origin)。
.wrap{ width: 200px; transform: rotate(-45deg); overflow: hidden; } .wrap > img{ transform: rotate(45deg) scale(1.42); max-width: 100%; }
四、切角效果
.wrap{ width: 200px; height: 100px; background: #58a; background: linear-gradient(-135deg, transparent 15px, #58a 0px) top right, linear-gradient(135deg,transparent 15px, #655 0px) top left, linear-gradient(-45deg, transparent 15px, #58a 0px) bottom right, linear-gradient(45deg, transparent 15px, #655 0px) bottom left; background-size: 50% 50%; background-repeat: no-repeat; }
可以利用border-image實現切角,設定border-image-slice(圖片邊框向內位移)的值;
border-image用svg來做圖片
border設定寬度+透明,再加上border-image-slice向內位移就造就了邊框切角邊框;
background-clip:要設定為padding-box,不然背景會延伸到邊框上。
.wrapSvg{ border:15px solid transparent; border-image: 1 url('data:image/svg+xml, <svg xmlns="http://www.w3.org/2000/svg" width="3" height="3" fill="%2358a"><polygon points="0,1 1,0 2,0 3,1 3,2 2,3 1,3 0,2"/></svg>'); margin-top: 50px; width: 200px; height: 100px; background: #58a; background-clip: padding-box; }
其他方案
五、梯形圖案
瞭解transform的基本原理
a和d表示縮放且不能為0;c和b控制傾斜;e和f控制位移
translate(位移):matrix(1,0,0,1,x,y)
scale(縮放):matrix(x,0,0,y,0,0);
skew(傾斜):matrix(1,tany,tanx,1,0,0),由於輸入的是deg(角度),需要將角度轉換為弧度值
rotate(旋轉):matrix(cosN,sinN,-sinN,cosN,0,0),角度轉換為弧度
上述值的應用都與transform-origin的值有關係,他是定位元素旋轉的原點,可以是top,bottom,center等,可以指定x,y,z三種座標系
perpective:透視,不可以負數,0或百分比,只能是數值;
六、簡單的餅圖
動畫餅圖,效果如下:
實現步驟如下:
畫出一個yellowgreen的圓,並利用linear-gradient設定background-image的值,實現兩種顏色各顯示一半的功能:
然後加入一個虛擬元素,繼承父級(真實元素)的背景色,然後用rotate旋轉即可
動畫展示代碼如下:
@keyframes spin{ to{ transform: rotate(.5turn); } } @keyframes bg{ 50%{ background-color: #655; } } .wrap{ width: 100px; height: 100px; border-radius: 50%; background: yellowgreen; background-image: linear-gradient(to right, transparent 50%, #655 0); } .wrap::before{ content: ''; display: block; margin-left: 50%; background-color: inherit; height: 100%; border-radius: 0 100% 100% 0 / 50%; transform-origin: left; animation:spin 3s linear infinite, bg 6s step-end infinite; }
相信看了本文案例你已經掌握了方法,更多精彩請關注php中文網其它相關文章!
推薦閱讀:
單選、複選樣式美化的圖文詳解
CSS實現一級導覽列
CSS實現書籤圖案的效果