CSS绘制卡券、优惠券样式

一般做商城类的项目,为了商品促销,都会有优惠券的需求,如果有UI组可以切图那能省不少功夫,如果没有,那么我们自己也可使用CSS画出这些仿淘宝、京东的优惠券样式。

radial-gradient()

使用radial-gradient径向渐变函数,可以实现一个圆形镂空的样子,在移动端,radial-gradient的兼容性也是相当完美。

CSS 语法

background: radial-gradient(shape size at position, start-color, ..., last-color);

单个圆形镂空效果如下:

css 复制代码
.base-one-circle {
    width: 100px;
    height: 100px;
    position: relative;
    background: radial-gradient(circle at 0px 50px, transparent 10px, #28A4F2 0) top
  }

圆形的位置也可修改,只需要改"circle at..."后面这个两个数值即可,上,右,下的数值分别是:50px,0、100px,50px、50px,100px。

如果想要一个阴影状,带有立体感,可加上这个代码

css 复制代码
filter: drop-shadow(3px 3px 3px rgba(0,0,0,.3))

完整的卡券样式

利用background: radial-gradient的特性,我们可以组合成更复杂、完整的优惠券样式:

css 复制代码
base-coupons {
    width: 250px;
    height: 100px;
    position: relative;
    background: radial-gradient(circle at right top, transparent 10px,  #28A4F2 0) top left / 60px 51% no-repeat,
      radial-gradient(circle at right bottom, transparent 10px,  #28A4F2 0) bottom left /60px 51% no-repeat,
      radial-gradient(circle at left top, transparent 10px, #eeeeee 0) top right /190px 51% no-repeat,
      radial-gradient(circle at left bottom, transparent 10px, #eeeeee 0) bottom right /190px 51% no-repeat;
    filter: drop-shadow(3px 3px 3px rgba(0,0,0,.3));
  }

如果想要在卡券的拼接处加上虚线效果,我们可以利用伪类去实现:

css 复制代码
.base-coupons::before {
    content: '';
    height: 80px;
    border: 1px dashed #fff;
    position: absolute;
    left: 60px;
    top: 0;
    bottom: 0;
    margin: auto;
  }

那么最终的优惠券样式效果:

linear-gradient()

CSS 语法

background: linear-gradient(direction, color-stop1, color-stop2, ...);

linear-gradient 函数是一个线性渐变函数,我们可以使用这个线性渐变加伪类来实现一个优惠券的锯齿状效果,如下:

css 复制代码
.base-coupons::after {
    content: '';
    position: absolute;
    height: 100%;
    width:5px;
    top: 0;
    right: -5px;
    background-image: linear-gradient(to bottom, #eeeeee 5px, transparent 5px, transparent),
    radial-gradient(10px circle at 5px 10px, transparent 5px, #eeeeee 5px);
    background-size: 5px 15px;
  }
相关推荐
TimelessHaze1 分钟前
【面试考点】从URL输入到页面展示
前端·trae
玲小珑3 分钟前
LangChain.js 完全开发手册(一)AI 应用开发入门
前端·langchain·ai编程
excel3 分钟前
前端必修:从表单基础到富文本编辑,一文吃透 HTML 表单编程与交互
前端
袁煦丞5 分钟前
JuiceSSH你的口袋里的Linux操控台:cpolar内网穿透实验室第530个成功挑战
前端·程序员·远程工作
鹏多多9 分钟前
深入解析vue的transition过渡动画使用和优化
前端·javascript·vue.js
程序员小续21 分钟前
React 源码解读流程:从入口到渲染的全链路揭秘
前端·javascript·面试
江城开朗的豌豆25 分钟前
React key的隐藏技能:key改变时究竟发生了什么?
前端·javascript·react.js
JarvanMo33 分钟前
我用 Ktor 替换了 Retrofit-我的网络代码减少了一半
前端
excel37 分钟前
WebGL 入门到进阶全解析:从 Canvas 上下文到 3D 绘制与 WebGL2 新特性
前端
掘金安东尼1 小时前
用 WebGL + Solid.js 构建混合材质 Shader
前端·webgl