整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:

用css3实现惊艳面试官的背景即背景动画(高级附源码

用css3实现惊艳面试官的背景即背景动画(高级附源码)

们传统的前端更多的是用javascript实现各种复杂动画,自从有了Css3 transition和animation以来,前端开发在动画这一块有了更高的自由度和格局,对动画的开发也越来越容易。这篇文章就让我们汇总一下使用Css3实现的各种特效。这篇文章参考《css揭秘》这本书,并作出了自己的总结,希望能让大家更有收获,也强烈推荐大家看看这本书,你值得拥有。

我们将学到

  • Css3
  • outline
  • radial-gradient
  • linear-gradient
  • box-shadow

1.实现内部虚线边框

知识点:outline

核心代码.dash-border{
 width: 200px;
 height: 100px;
 line-height: 100px;
 outline: 1px dashed #fff;
 outline-offset: -10px;
}
复制代码

2.边框内圆角的实现

知识点:box-shadow

核心代码.radius-border{
 margin-top: 20px;
 width: 180px;
 height: 80px;
 box-shadow: 0 0 0 10px gray;
}
复制代码

3.实现条纹背景与进度条

知识点:linear-gradient,repeating-linear-gradient

核心代码/* 上 */
background: linear-gradient(to right,#fb3 50%,#58a 0);
background-size: 40px 100%;
box-shadow: inset 0 0 3px #555;
/* 中 */
background: linear-gradient(45deg,#fb3 25%,#58a 0,#58a 50%,#fb3 0,#fb3 75%,#58a 0);
background-size: 40px 40px;
/* 下 (可以实现任意角度的渐变,45°时显示效果最好) */
background: repeating-linear-gradient(60deg,#fb3,#fb3 15px,#58a 0,#58a 30px);
复制代码

4.复杂的背景图案

知识点:linear-gradient,repeating-linear-gradient,radial-gradient

由于第二个图会有复杂的随机动画,建议大家可以亲自尝试看看效果,核心代码

<style>
.bg-grid{
 margin-top: 20px;
 width: 200px;
 height: 200px;
 background-image: linear-gradient(rgba(255,255,255,1) 2px,transparent 0),
 linear-gradient(to right,rgba(255,255,255,1) 2px,transparent 0),
 linear-gradient(rgba(255,255,255,.2) 1px,transparent 0),
 linear-gradient(to right,rgba(255,255,255,.2) 1px,transparent 0);
 background-position: -50px -50px;
 background-size: 100px 100px,100px 100px, 100% 10px, 10px 100%;
}
.animate-grid{
 animation: move-grid 6s linear infinite;
}
@keyframes move-grid{
 0%{
 background-position: -50px -50px;
 }
 30%{
 background-position: -100px -100px;
 }
 60%{
 background-position: -100px -150px;
 }
 100%{
 background-position: -50px -50px;
 }
}
</style>
<div class="bg-grid black-theme"></div>
<div class="bg-grid black-theme animate-grid"></div>
复制代码

红绿灯以及红路灯随机运动动画

利用css3多背景和position实现红绿灯和背景色块移动核心代码

<style>
.bg-dot{
 margin-top: 20px;
 width: 200px;
 height: 50px;
 background-image: radial-gradient(circle,#0cf 15px,transparent),
 radial-gradient(circle,red 15px,transparent),
 radial-gradient(circle,yellow 15px,transparent),
 radial-gradient(circle,green 15px,transparent);
 background-repeat: no-repeat;
 background-position: 0 0, 50px 0, 100px 0, 150px 0, 200px 0;
 background-size: 50px 50px;
}
.animate-dot{
 animation: move-dot 8s linear infinite;
}
.animate-dot2{
 animation: move-dot2 6s linear infinite;
}
@keyframes move-dot{
 0%{
 background-position: 0 0, 50px 0, 100px 0, 150px 0;
 }
 30%{
 background-position: 50px 0, 0 0, 100px 0, 150px 0;
 }
 60%{
 background-position: 50px 0, 100px 0, 0 0, 150px 0;
 }
 100%{
 background-position: 50px 0, 100px 0, 150px 0, 0 0;
 }
}
@keyframes move-dot2{
 0%{
 background-image: radial-gradient(circle,#0cf 15px,transparent),
 radial-gradient(circle,gray 15px,transparent),
 radial-gradient(circle,gray 15px,transparent),
 radial-gradient(circle,gray 15px,transparent);
 }
 30%{
 background-image: radial-gradient(circle,gray 15px,transparent),
 radial-gradient(circle,red 15px,transparent),
 radial-gradient(circle,gray 15px,transparent),
 radial-gradient(circle,gray 15px,transparent);
 }
 60%{
 background-image: radial-gradient(circle,gray 15px,transparent),
 radial-gradient(circle,gray 15px,transparent),
 radial-gradient(circle,yellow 15px,transparent),
 radial-gradient(circle,gray 15px,transparent);
 }
 100%{
 background-image: radial-gradient(circle,gray 15px,transparent),
 radial-gradient(circle,gray 15px,transparent),
 radial-gradient(circle,gray 15px,transparent),
 radial-gradient(circle,green 15px,transparent);
 }
}
</style>
<div class="bg-dot black-theme animate-dot"></div>
<div class="bg-dot black-theme animate-dot2"></div>
复制代码

棋盘背景以及棋盘背景随机动画

利用背景渐变实现棋盘图案核心代码

<style>
.bg-qi{
 margin-top: 20px;
 width: 200px;
 height: 200px;
 background-color: #eee;
 background-image: linear-gradient(45deg,rgba(0,0,0,.25) 25%,transparent 0,transparent 75%,rgba(0,0,0,.25) 0),
 linear-gradient(45deg,rgba(0,0,0,.25) 25%,transparent 0,transparent 75%,rgba(0,0,0,.25) 0);
 background-position: 0 0, 20px 20px;
 background-size: 40px 40px;
 box-shadow: 2px 2px 4px rgba(0,0,0,.4);
}
</style>
<div class="bg-qi black-theme"></div>
复制代码

伪随机背景

利用背景渐变,keyframe动画,实现复杂的伪随机动画核心代码

<style>
.bg-line-rand{
 margin-top: 20px;
 width: 480px;
 height: 60px;
 background-color: #eee;
 background-image: linear-gradient(90deg,#fb3 11px, transparent 0),
 linear-gradient(90deg,#ab4 23px, transparent 0),
 linear-gradient(90deg,#655 41px, transparent 0);
 background-size: 41px 100%, 61px 100%, 83px 100%;
 box-shadow: 2px 2px 4px rgba(0,0,0,.4);
}
.bg-dot-rand{
 margin-top: 20px;
 width: 200px;
 height: 200px;
 background-color: #eee;
 background-image: radial-gradient(circle,#fb3 5px, transparent 0),
 radial-gradient(circle,#ab4 13px, transparent 0),
 radial-gradient(circle,#655 31px, transparent 0);
 background-repeat: no-repeat;
 background-size: 101px 203px, 147px 60px, 373px 201px;
 box-shadow: 2px 2px 4px rgba(0,0,0,.4);
}
.animate1{
 animation: move 4s linear infinite;
}
.animate2{
 animation: move2 4s linear infinite;
}
@keyframes move{
 0%{
 background-position: 0 0, 0 0, 0 0;
 }
 50%{
 background-position: -10px 0, 20px 0, 30px 0;
 }
 100%{
 background-position: 0 0, 30px 0, 10px 0;
 }
}
@keyframes move2{
 0%{
 background-position: 0 0, 0 0, 0 0;
 }
 50%{
 background-position: -10px 30px, 20px 0, -40px 80px;
 }
 100%{
 background-position: 0 20px, 60px -20px, 10px 30px;
 }
}
</style>
<div class="bg-line-rand black-theme"></div>
<div class="bg-line-rand black-theme animate1"></div>
<div class="bg-dot-rand black-theme animate2"></div>
复制代码

5.折角效果

知识点:linear-gradient

核心代码

<style>
.fold{
 margin-left: auto;
 margin-right: auto;
 margin-bottom: 20px;
 width: 200px;
 height: 80px;
 color: #fff;
 line-height: 80px;
 text-align: center;
 background: linear-gradient(to left bottom,transparent 50%,rgba(0,0,0,.5) 0) no-repeat 100% 0 / 2em 2em,
 linear-gradient(-135deg,transparent 1.4em,#06c 0);
}
.linear{
 background: linear-gradient(to left bottom,transparent 50%,rgba(0,0,0,.5)) no-repeat 100% 0 / 2em 2em,
 linear-gradient(-135deg,transparent 1.4em,#06c);
}
</style>
<div class="fold">折角效果哦</div>
<div class="fold linear">折角效果哦</div>
复制代码

2.内阴影圆折角效果

核心代码

<style>
.fold-1{
 margin-left: auto;
 margin-right: auto;
 margin-bottom: 20px;
 position: relative;
 width: 200px;
 height: 80px;
 border-radius: .5em;
 color: #fff;
 line-height: 80px;
 text-align: center;
 background: linear-gradient(-150deg,transparent 1.5em, #58a 0);
}
.fold-1::before{
 content: "";
 position: absolute;
 top: 0;
 right: 0;
 background: linear-gradient(to left bottom,transparent 50%,rgba(0,0,0,.2) 0,rgba(0,0,0,.4)) 100% 0 no-repeat;
 width: 1.73em;
 height: 3em;
 transform: translateY(-1.3em) rotate(-30deg);
 transform-origin: bottom right;
 border-bottom-left-radius: inherit;
 box-shadow: -.2em .2em .3em -.1em rgba(0,0,0,.15);
}
</style>
<div class="fold-1">折角效果哦</div>
复制代码

6.自适应文本的条纹背景

知识点:linear-gradient,line-height,background-origin

核心代码

<style>
.stripe-bg{
 padding: .5em;
 line-height: 1.5em;
 background: beige;
 background-size: auto 3em;
 background-origin: content-box;
 background-image: linear-gradient(rgba(0,0,0,.2) 50%, transparent 0);
}
.stripe-bg > p{
 margin: 0;
}
</style>
<div class="stripe-bg">
 <p>hello you</p>
 <p>hello you</p>
 <p>hello you</p>
 <p>hello you</p>
 <p>hello you</p>
</div>
复制代码

7.自定义的下划线实现

知识点linear-gridient

小伙伴们注意到了吗?默认的下划线会将文字穿过,而上面的不会呦!

核心代码

<style>
.my-line{
 line-height: 1.4em;
 background: linear-gradient(gray,gray) no-repeat;
 background-size: 100% 1px;
 background-position: 0 1.15em;
 text-shadow: .05em 0 #fff, -.05em 0 #fff;
}
</style>
<div class=""><span class="my-line">i have your big apple.you have a too? hi hi hi.</span></div>
复制代码

最后

笔者3天后将推出开源的CMS系统,技术架构:

  • 后台Node+Koa+redis+JsonSchema
  • 管理后台界面 vue-cli3 + vue + ts + vuex + antd-vue + axios
  • 客户端前台 react + antd + react-hooks + axios

平时开发中,有时候会碰到下面这种“弧形”样式,主要分为“内凹”和“外凸”两种类型,如下

该如何实现呢?或者想一下,有哪些 CSS 属性和“弧形”有关?下面介绍 3 种方式,一起看看吧

一、border-radius

通常情况下,我们用border-radius都是这样

div{
  border-radius: 20px;
}


这样表示 4 个角都是圆角,并且是标准的正圆

其实,border-radius还支持斜杠的写法,比如

div{
  border-radius: 20px / 10px;
}


这表示,圆角是一个x半径为20px,y半径为10px的椭圆,如下

放大来看,其实是这样的

进一步,我们将x半径设置成50%,这样就能得到一个完整的弧形了

div{
  border-radius: 50% / 20px;
}


效果如下

这样就得到了“外凸”的弧形了,是不是很容易?

至于“内凹”弧形,单纯的border-radius表示无能为力,可以看接下来的方式

二、伪元素+border-radius

这个其实大多数同学都能想到的方式,一个矩形和一个椭圆组合起来,不就是一个弧形卡片了吗?原理非常简单

用代码实现就是

div{
  position: relative;
}
div::after{
  content: '';
  position: absolute;
  width: 100%;
  height: 20px;
  border-radius: 100%;
  background: inherit; /* 继承父级背景 */
  bottom: 0;
  left: 50%;
  transform: translate(-50%,50%);
}


效果如下(虚线表示伪元素的边缘)

用伪元素的好处是,可以随意控制弧度的大小位置,这个是border-radius所不能比的

通过overflow:hidden裁剪多余部分,可以得到一个边缘比较“锋利”的弧形,如下所示

另外,用伪元素还能实现“内凹”的效果,不过这需要反过来思考,什么意思呢?之前是给伪元素加的背景,现在需要加在伪元素的外围,这里用box-shadow实现,原理如下

用代码实现就是

div{
  background: none;
  overflow: hidden;
}
div::after{
  content: '';
  background: none;
  box-shadow: 0 0 0 9999vh #FFE8A3; /*足够大的阴影*/
  z-index: -1;
}


效果如下(虚线表示伪元素的边缘)

三、径向渐变

提到圆弧,还可以想到径向渐变,没错,这里通过径向渐变也能轻易实现弧形卡片效果。

先来看“外凸”的,其实思路和伪元素一样,先拆分,一个椭圆和一个矩形,对应的就是径向渐变(radial-gradient)和线性渐变(linear-gradient),如下

用代码实现就是

div{
  background: 
    radial-gradient(closest-side, #9747FF 100%,transparent 0) center bottom/100% 20px no-repeat,
    linear-gradient(#FFE8A3, #FFE8A3) 0 0/100% calc(100% - 10px) no-repeat;
}


效果如下(紫色部分是径向渐变)

再来看“内凹”的弧形,其实也和伪元素思路类似,只不过这里需要绘制一个足够大的渐变,从透明到纯色的径向渐变,示意如下

调整好渐变中心点,用代码实现就是

div{
  background: radial-gradient(50% 10px at center bottom, transparent 100%,#FFE8A3 0) center bottom;
}


效果如下(全部都是径向渐变绘制)

当然你也可以随意改变径向的弧度和大小,来实现各种不同的效果。

以上所有demo都可以查看以下在线链接

  • CSS radius layout (codepen.io)

四、优缺点总结

以上就是本文的全部内容了,共介绍了 3 种不同的方式,下面总结一下各自优缺点

  1. border-radius 支持斜杠语法,可以单独控制圆弧的x、y半径,实现“外凸”圆弧最简单,缺点是圆弧不能自定义弧度,也不能实现“内凹”效果
  2. 伪元素最符合常规思维,可以解决以上问题,缺点是需要占用伪元素,略微麻烦
  3. 渐变实现和伪元素拼接思路类似,缺点是语法复杂,需要熟练掌握渐变语法


作者:XboxYan
链接:https://juejin.cn/post/7308434314777788426

order 盒子边框

复合属性。设置对象边框的特性。

盒子边框三要素:

① 边框粗细

② 边框样式

③ 边框颜色

语法:border: border-width | border-style | border-color ;

边框四边的粗细、样式、颜色,以及上下左右每个位置的样式属性都是可以单独调整的。

边框的颜色不是必要的,如果不指定颜色,默认颜色为黑色,但必须为盒子指定宽高。


初始 HTML

 <!DOCTYPE html>
 <html>
 <head>
     <meta charset="UTF-8">
     <meta name="viewport" content="width=device-width, initial-scale=1.0">
     <title>Document</title>
     <style>
         div{
             width: 500px;
             height: 50px;
         }
     </style>
 </head>
 <body>
     <div></div>
 </body>
 </html>




边框样式

使用 border-style 可为盒子边框设置样式,以下示例为实线

 /* CSS代码 */
 div{
     width: 500px;
     height: 50px;
     border-style: solid; 
 }

效果:


可单独针对某一方向设置边框样式

示例 CSS 代码

  • 上边:border-top-style: double; (双线)
  • 右边:border-right-style: solid; (实线)
  • 下边:border-bottom-style: dashed; (虚线)
  • 左边:border-left-style: dotted; (点线)


border-style 说明

  • 如果提供全部四个参数值,将按上、右、下、左的顺序作用于四边。
  • 如果只提供一个,将用于全部的四边。
  • 如果提供两个,第一个用于上、下,第二个用于左、右。
  • 如果提供三个,第一个用于上,第二个用于左、右,第三个用于下。
  • 如果border-width等于0,本属性将失去作用。

如果需要设置不同方向的样式属性,可以写在一句 CSS 代码里,比如说下面这段代码,上下实线,左右虚线。

 /* CSS代码 */
 div{
     width: 500px;
     height: 50px;
     border-style: solid dashed;
 }

效果:


border-style 样式属性值

属性值解释none无轮廓。 border-color将被忽略,border-width计算值为0,除非边框轮廓为图像,即border-image。hidden隐藏边框。IE7及以下尚不支持dotted点状轮廓。IE6下显示为dashed效果dashed虚线轮廓solid实线轮廓double双线轮廓。两条单线与其间隔的和等于指定的border-width值groove3D凹槽轮廓ridge3D凸槽轮廓inset3D凹边轮廓outset3D凸边轮廓




边框粗细

使用 border-width 可为盒子边框设置粗细,以下示例边框为 5px 粗细

 /* CSS代码 */
 div{
     width: 500px;
     height: 50px;
     border-style: solid;
     border-width: 5px;
 }

效果


可单独针对某一方向设置边框粗细

示例 CSS 代码

  • 上边:border-top-width: 10px; (双线)
  • 右边:border-right-width: 10px; (实线)
  • 下边:border-bottom-width: 10px; (虚线)
  • 左边:border-left-width: 10px; (点线)


border-width 说明

  • 如果提供全部四个参数值,将按上、右、下、左的顺序作用于四边。
  • 如果只提供一个,将用于全部的四边。
  • 如果提供两个,第一个用于上、下,第二个用于左、右。
  • 如果提供三个,第一个用于上,第二个用于左、右,第三个用于下。
  • 如果border-style设置为none或hidden,border-width的使用值将为0。

如果需要设置不同方向的边框粗细,可以写在一句 CSS 代码里。

比如说下面这段代码,上下2px,右2px,左5px。

 /* CSS代码 */
 div{
     width: 500px;
     height: 50px;
     border-style: solid;
     border-width:2px 2px 2px 5px;
 }

效果:




边框颜色

可直接输入

颜色的英文名称

rgb值

十六进制

使用 border-color 可为盒子边框设置颜色,以下示例边框颜色为红色。

 /* CSS代码 */
 div{
     width: 500px;
     height: 50px;
     border-style: solid;
     border-width:2px 2px 2px 5px;
     border-color: red;
 }

效果:


可单独针对某一方向设置边框颜色

示例 CSS 代码

  • 上边:border-top-color: 10px;
  • 右边:border-right-color: 10px;
  • 下边:border-bottom-color: 10px;
  • 左边:border-left-color: 10px;


border-color 说明

  • 如果提供全部四个参数值,将按上、右、下、左的顺序作用于四边。
  • 如果只提供一个,将用于全部的四边。
  • 如果提供两个,第一个用于上、下,第二个用于左、右。
  • 如果提供三个,第一个用于上,第二个用于左、右,第三个用于下。
  • 如果border-width等于0或border-style设置为none,本属性将被忽略。

上面有两个示例讲述如何设置不同方向的属性,border-color 也是相同使用方法,此处就不做示例了。




border 是复合属性

如果你需要同时设置盒子的粗细、样式、颜色,那么你可以将他们的样式表写在同一行代码里。

例如:

 /* CSS代码 */
 div{
     border-top: 5px solid red;
 }

这段代码指定了上边框的三个属性:粗细、样式、颜色

border-top 包含了:

  • border-top-width: 5px;
  • border-top-style: solid;
  • border-top-color: red;

其他同理




教你用 CSS 画个三角形

先来看一个示例

 /* CSS代码 */
 div{
     width: 100px;
     height: 100px;
     border-top: 50px solid red;
     border-right: 50px solid blue;
     border-bottom: 50px solid green;
     border-left: 50px solid pink;
 }

效果:

细心的你,一定发现了 border 的边框四条边交接处是斜角。


此刻我们把盒子的宽高设置为 0

 /* CSS代码 */
 div{
     width: 0;
     height: 0;
     border-top: 50px solid red;
     border-right: 50px solid blue;
     border-bottom: 50px solid green;
     border-left: 50px solid pink;
 }

效果:

是不是完完全全像四个三角形一样。

我们只需要把上边和左右两边的三角形隐藏起来,它不就是一个三角形了。

为 border-color 指定 transparent 值,使盒子边框颜色变透明

 /* CSS代码 */
 div{
     width: 0;
     height: 0;
     border-top: 50px solid transparent;
     border-right: 50px solid transparent;
     border-bottom: 50px solid green;
     border-left: 50px solid transparent;
 }

效果:

把另外三条边透明之后,就只剩一个三角形了。





部分资料引用自:

  • http://caibaojian.com/css3/properties/border/index.htm