css3 烟 蚊香_前端每日实战:48# 视频演示如何用纯 CSS 创作一盘传统蚊香

效果预览

按下右侧的“点击预览”按钮可以在当前页面预览,点击链接可以全屏预览。

可交互视频教程

此视频是可以交互的,你可以随时暂停视频,编辑视频中的代码。

请用 chrome, safari, edge 打开观看。

源代码下载

每日前端实战系列的全部源代码请从 github 下载:

代码解读

定义 dom,容器中包含 8 个子元素:

居中显示:

body {

margin: 0;

height: 100vh;

display: flex;

align-items: center;

justify-content: center;

background: radial-gradient(circle at center, midnightblue, black);

}

画出纹香盘要用的框线:

.coil {

position: relative;

display: flex;

justify-content: center;

}

.coil span {

position: absolute;

width: calc((var(--n) * 2 - 1) * 1em);

height: calc((var(--n) - 0.5) * 1em);

border: 1em solid darkgreen;

}

.coil span:nth-child(1) {

--n: 1;

}

.coil span:nth-child(2) {

--n: 2;

}

.coil span:nth-child(3) {

--n: 3;

}

.coil span:nth-child(4) {

--n: 4;

}

.coil span:nth-child(5) {

--n: 5;

}

.coil span:nth-child(6) {

--n: 6;

}

.coil span:nth-child(7) {

--n: 7;

}

.coil span:nth-child(8) {

--n: 8;

}

把一半框线放置到上方:

.coil span:nth-child(odd) {

align-self: flex-end;

}

删除掉上方框线的下边框,和下方框线的上边框:

.coil span:nth-child(odd) {

border-bottom: none;

}

.coil span:nth-child(even) {

border-top: none;

}

对齐上下边框:

.coil span:nth-child(even) {

transform: translateX(-1em);

}

把边框改为曲线:

.coil span:nth-child(odd) {

border-radius: 50% 50% 0 0 / 100% 100% 0 0;

}

.coil span:nth-child(even) {

border-radius: 0 0 50% 50% / 0 0 100% 100%;

}

用伪元素画出蚊香最中间的部分:

.coil::before {

content: '';

position: absolute;

width: 1em;

height: 1em;

background-color: darkgreen;

border-radius: 50%;

left: -1.5em;

top: -0.5em;

}

用伪元素画出蚊香的燃点:

.coil::after {

content: '';

position: absolute;

width: 1em;

height: 1em;

border-radius: 50%;

top: -0.5em;

background-color: darkred;

left: -9.5em;

z-index: -1;

transform: scale(0.9);

box-shadow: 0 0 1em white;

}

最后,为燃点增加闪动的效果:

.coil::after {

animation: blink 1s linear infinite alternate;

}

@keyframes blink {

to {

box-shadow: 0 0 0 white;

}

}

大功告成!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值