如何用纯 CSS 创作 404 文字变形为 NON 文字的交互特效

简介: 效果预览在线演示按下右侧的“点击预览”按钮可以在当前页面预览,点击链接可以全屏预览。https://codepen.io/comehope/pen/ZoxjXm可交互视频教程此视频是可以交互的,你可以随时暂停视频,编辑视频中的代码。

效果预览

在线演示

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

https://codepen.io/comehope/pen/ZoxjXm

可交互视频教程

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

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

https://scrimba.com/p/pEgDAM/cmQwKAa

源代码下载

本地下载

请从 github 下载。

https://github.com/comehope/front-end-daily-challenges/tree/master/018-stroke-morphing-404-effects

代码解读

定义 dom,容器中包含 3 个 <p>,每个 <p> 代表 1 个数字;每个 p 标签包含若干 <span>,每个 <span> 代表 1 个笔划:

&lt;section class="four-zero-four"&gt;
    &lt;p class="four"&gt;
        &lt;span&gt;&lt;/span&gt;
        &lt;span&gt;&lt;/span&gt;
        &lt;span&gt;&lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="zero"&gt;
        &lt;span&gt;&lt;/span&gt;
        &lt;span&gt;&lt;/span&gt;
        &lt;span&gt;&lt;/span&gt;
        &lt;span&gt;&lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="four"&gt;
        &lt;span&gt;&lt;/span&gt;
        &lt;span&gt;&lt;/span&gt;
        &lt;span&gt;&lt;/span&gt;
    &lt;/p&gt;
&lt;/section&gt;

居中显示:

html, body {
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    background: linear-gradient(gray, silver);
}

整体布局:

.four-zero-four p {
    width: 10em;
    height: 10em;
    border: 1px dashed white;
    display: inline-block;
    margin: 1em;
    position: relative;
}

设置笔划共有属性:

.four-zero-four p span {
    position: absolute;
    box-sizing: border-box;
    filter: opacity(0.8);
}

画出数字 4 的笔划:

.four span:nth-child(1) {
    width: 20%;
    height: 80%;
    left: 10%;
}

.four span:nth-child(2) {
    width: 100%;
    height: 20%;
    bottom: 30%;
}

.four span:nth-child(3) {
    width: 20%;
    height: 100%;
    right: 10%;
}

画出数字 0 的笔划:

.zero span:nth-child(1) {
    width: 20%;
    height: 100%;
    left: 10%;
}

.zero span:nth-child(2) {
    width: 100%;
    height: 20%;
    top: 10%;
}

.zero span:nth-child(3) {
    width: 20%;
    height: 100%;
    right: 10%;
}

.zero span:nth-child(4) {
    width: 100%;
    height: 20%;
    bottom: 10%;
}

给笔划上色:

.four span:nth-child(1) {
    background-color: yellowgreen;
}

.four span:nth-child(2) {
    background-color: turquoise;
}

.four span:nth-child(3) {
    background-color: pink;
}

.zero span:nth-child(1) {
    background-color: skyblue;
}

.zero span:nth-child(2) {
    background-color: plum;
}

.zero span:nth-child(3) {
    background-color: lightcoral;
}

.zero span:nth-child(4) {
    background-color: peachpuff;
}

设置划过数字时笔划的变化效果:

.four-zero-four p:hover span {
    border: 1px solid black;
    background-color: transparent;
    filter: opacity(1);
    transition: 0.3s;
}

设置划过数字时笔划的偏移量:

.four:hover span:nth-child(1) {
    left: 0;
}

.four:hover span:nth-child(2) {
    bottom: 0;
}

.four:hover span:nth-child(3) {
    right: 0;
}

.zero:hover span:nth-child(1) {
    left: 0;
}

.zero:hover span:nth-child(2) {
    top: 0;
}

.zero:hover span:nth-child(3) {
    right: 0;
}

.zero:hover span:nth-child(4) {
    bottom: 0;
}

最后,设置缓动时长:

.four-zero-four p span {
    transition: 0.3s;
}

.four-zero-four p:hover span {
    transition: 0.3s;
}

大功告成!

知识点

原文地址:https://segmentfault.com/a/1190000014818274

相关文章
|
1月前
|
前端开发 JavaScript UED
过渡效果的艺术:CSS transition 让网页交互更平滑(上)
过渡效果的艺术:CSS transition 让网页交互更平滑(上)
|
1月前
|
前端开发
CSS3的几个变形案例……
CSS3的几个变形案例……
17 0
|
1月前
|
Web App开发 弹性计算 前端开发
纯 CSS 实现多行文字截断
纯 CSS 实现多行文字截断
21 1
|
1月前
|
Web App开发 前端开发 JavaScript
过渡效果的艺术:CSS transition 让网页交互更平滑(下)
过渡效果的艺术:CSS transition 让网页交互更平滑(下)
|
2月前
|
前端开发 容器
css 中可以让文字在垂直和水平方向上重叠的两个属性是什么?
css 中可以让文字在垂直和水平方向上重叠的两个属性是什么?
|
2月前
|
前端开发 开发者
深入了解 CSS 伪类:优化页面样式与交互
深入了解 CSS 伪类:优化页面样式与交互
58 2
|
3月前
|
前端开发
html+css+js实现自动敲文字效果
html+css+js实现自动敲文字效果
23 0
css3文字阴影和盒子阴影
css3文字阴影和盒子阴影
|
4月前
|
前端开发 JavaScript
JS与CSS交互
JS与CSS交互
39 0
|
4月前
|
前端开发
css文字环绕png图片
css文字环绕png图片
38 1