CSS實現鏤空效果

sex_squirrel發表於2018-12-22

效果原理

主要利用css漸變實現一些不需要切圖的背景鏤空

優惠券樣式

CSS實現鏤空效果
.mixinsTicket(@width, @height, @r, @left, @lcolor, @rcolor) { 
width: @width;
height: @height;
background: radial-gradient(circle at top right, transparent @r, @lcolor 0) -(@width - @left) top ~'/' 100% 51% no-repeat, radial-gradient(circle at bottom right, transparent @r, @lcolor 0) -(@width - @left) bottom ~'/' 100% 51% no-repeat, radial-gradient(circle at top left, transparent @r, @rcolor 0) @left 0 ~'/' 100% 51% no-repeat, radial-gradient(circle at bottom left, transparent @r, @rcolor 0) @left bottom ~'/' 100% 51% no-repeat;

}複製程式碼
CSS實現鏤空效果
.mixinsTicket1(@width, @height, @r, @top, @color) { 
width: @width;
height: @height;
background: radial-gradient(circle at bottom left, transparent @r, @color 0) left (@top - @height) ~'/' 51% 100% no-repeat, radial-gradient(circle at top left, transparent @r, @color 0) left @top ~'/' 51% 100% no-repeat, radial-gradient(circle at bottom right, transparent @r, @color 0) right (@top - @height) ~'/' 51% 100% no-repeat, radial-gradient(circle at top right, transparent @r, @color 0) right @top ~'/' 51% 100% no-repeat;
&
::after{
content: '';
display: block;
position: absolute;
width: calc(100% - 2 * @r);
left: @r;
top: @top;
border-top: 1px dashed #fff;
transform: translateY(.5);

}
}複製程式碼

切角效果

CSS實現鏤空效果

ps: 鋸齒跟裝置的顯示有關係

.mixinFlag(@width, @height, @bg) when(default()) { 
width: @width;
height: @height;
background: linear-gradient(45deg, transparent sqrt(pow(@width/2, 2)/2), @bg 0) right, linear-gradient(-45deg, transparent sqrt(pow(@width/2, 2)/2), @bg 0) left;
background-size: 50% 100%;
background-repeat: no-repeat;

} .mixinFlag(@width, @height, @bg) when(@width >
@height) {
width: @width;
height: @height;
background: linear-gradient(-45deg, transparent sqrt(pow(@height/2, 2)/2), @bg 0) top left, linear-gradient(-135deg, transparent sqrt(pow(@height/2, 2)/2), @bg 0) bottom left;
background-size: 100% 50%;
background-repeat: no-repeat;

}複製程式碼
CSS實現鏤空效果
.mixinsMark(@width, @height, @bg) { 
width: @width;
height: @height;
background: linear-gradient(-45deg, transparent sqrt(pow(@height/2, 2)/2), @bg 0) bottom left, linear-gradient(-135deg, transparent sqrt(pow(@height/2, 2)/2), @bg 0) top left;
background-size: 100% 50%;
background-repeat: no-repeat;

}複製程式碼

ps:以上效果四個方向都可以實現,其他方向程式碼沒有貼出,原理相同

格子布

CSS實現鏤空效果
css { 
width: 510px;
height: 128px;
background: #FFF;
background-image: linear-gradient(rgba(182, 128, 102, .8) 8px, transparent 0), linear-gradient(90deg, rgba(182, 128, 102, .8) 8px, transparent 0);
background-size: 8px 14px, 14px 8px;

}複製程式碼

焦點

CSS實現鏤空效果
.mask { 
position: fixed;
top: 0;
left: 0;
z-index: 100;
width: 100vw;
height: 100vh;
background: radial-gradient(closest-side at 50% 278rpx, transparent 140rpx, rgba(0, 0, 0, .12) 180rpx, rgba(0, 0, 0, .22) 200rpx, rgba(0,0,0,.3) 220rpx, rgba(0,0,0, .4)) no-repeat;

}複製程式碼

總結

每一層漸變都可被當做一張背景圖,也就是說每一層漸變都可以指定其position、size、repeat。玩過PS的的同學應該知道圖層的概念,我們們的背景圖層疊原理也類似(當然,漸變也可以當背景圖),控制漸變的大小,哪裡需要鏤空,以及需要顯示的位置、是否平鋪,就可以實現大多數場景下的基本效果,當然,一張切圖來的更快,但是有時候切圖並不能適應所有的場景。
掌握background的書寫順序可以幫助在腦海中構思自己想要的效果:

background: bg-color || bg-image || bg-position [ / bg-size]? || bg-repeat || bg-attachment || bg-origin || bg-clip

參考書籍

  • 《CSS揭祕》-css一姐寫的書

更多

希望能幫助有需要的小夥伴

來源:https://juejin.im/post/5c1da8bc6fb9a049c2326a55

相關文章