css 蚂蚁行军边框效果
接下来我们要实现这样的效果:
HTML 代码:
<div class="ants-border"> Lorem ipsum, dolor sit amet consectetur adipisicing elit. Laudantium, debitis vel quam tempora ut nisi voluptatibus rem ab. Quas, cum. Itaque, dignissimos! Quos, eos placeat eveniet quasi amet eius officiis! </div>
CSS 代码:
.ants-border{ width: 500px; padding: 1em; border: 1px solid transparent; background-image: linear-gradient(white, white), repeating-linear-gradient( -45deg, #000 0%, #000 25%, #fff 0%, #fff 50% ); background-size: 10px 10px; background-clip: padding-box, border-box; animation: ants 10s linear infinite; } @keyframes ants { to { background-position: 100%; } }
绘制步骤:
1、给 div 加一层黑白色条纹背景。
.ants-border{ width: 500px; background-image: repeating-linear-gradient(-45deg, #000 0%, #000 25%, #fff 0%, #fff 50%); background-size: 10px 10px; /*控制条纹的宽度*/ /*默认背景图像会 repeat 铺满全屏*/ }
2、再加一层白色背景。当一个元素设置多个background-image
时,如果位置重叠,则写在前面的背景会覆盖写在后面的背景。此时背景为白色。
.ants-border{ ... background-image: linear-gradient(white, white),repeating-linear-gradient(-45deg, #000 0%, #000 25%, #fff 0%, #fff 50%); background-size: 10px 10px; }
3、背景有一个属性background-clip
,初始值是border-box
,意味着背景会铺满 border-box。
为了让条纹背景露出来形成蚂蚁边框,我们将两层背景的background-clip
分别设为padding-box
和border-box
。
.ants-border{ ... background-image: linear-gradient(white, white),repeating-linear-gradient(-45deg, #000 0%, #000 25%, #fff 0%, #fff 50%); background-size: 10px 10px; background-clip: padding-box, border-box; }
此时白色背景铺满padding-box
,条纹背景铺满border-box
,此时依然时白色背景,因为没有设置 border 时,这两个 box 的范围是重叠的。所以设置一个 1px 的 border,颜色为透明色。
.ants-border{ ... border: 1px solid transparent; }
4、通过动画不断改变背景的位置,形成移动的“蚂蚁”
@keyframes ants { to { background-position: 100%; } }
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
码云笔记 » css 蚂蚁行军边框效果
码云笔记 » css 蚂蚁行军边框效果