CSS实现镂空效果的示例代码

2020-04-26 07:17:50易采站长站整理

效果原理

主要利用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;
}


.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);
}
}

切角效果

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;
}