zl程序教程

您现在的位置是:首页 >  IT要闻

当前栏目

【404】你访问的页面需要关灯后查看!

2023-02-18 16:32:26 时间

前言

今天在掘金首页刷到一篇文章,就是那种文字根据不同的色块显示不同的颜色,我想着能不能做一个探照灯似的 404 页面呢。毕竟也可以根据不同的白色光照来改变文字颜色的。

为了酷炫一点,先来个背景

? 背景相对来说比较简单了,就是一些纯粹的漂浮点

<div>
    <div class="starsec"></div>
    <div class="starthird"></div>
    <div class="starfourth"></div>
    <div class="starfifth"></div>
</div>

? 为了显得与众不同,我们就用四个不同的 div 元素来写样式

.starsec {
    content: " ";
    position: absolute;
    width: 3px;
    height: 3px;
    background: transparent;
    box-shadow: 571px 173px #00BCD4, 1732px 143px #00BCD4, 1745px 454px #FF5722, 234px 784px #00BCD4, 1793px 1123px #FF9800, 1076px 504px #03A9F4, 633px 601px #FF5722, 350px 630px #FFEB3B, 1164px 782px #00BCD4, 76px 690px #3F51B5, 1825px 701px #CDDC39, 1646px 578px #FFEB3B, 544px 293px #2196F3, 445px 1061px #673AB7, 928px 47px #00BCD4, 168px 1410px #8BC34A, 777px 782px #9C27B0, 1235px 1941px #9C27B0, 104px 1690px #8BC34A, 1167px 1338px #E91E63, 345px 1652px #009688, 1682px 1196px #F44336, 1995px 494px #8BC34A, 428px 798px #FF5722, 340px 1623px #F44336, 605px 349px #9C27B0, 1339px 1344px #673AB7, 1102px 1745px #3F51B5, 1592px 1676px #2196F3, 419px 1024px #FF9800, 630px 1033px #4CAF50, 1995px 1644px #00BCD4, 1092px 712px #9C27B0, 1355px 606px #F44336, 622px 1881px #CDDC39, 1481px 621px #9E9E9E, 19px 1348px #8BC34A, 864px 1780px #E91E63, 442px 1136px #2196F3, 67px 712px #FF5722, 89px 1406px #F44336, 275px 321px #009688, 592px 630px #E91E63, 1012px 1690px #9C27B0, 1749px 23px #673AB7, 94px 1542px #FFEB3B, 1201px 1657px #3F51B5, 1505px 692px #2196F3, 1799px 601px #03A9F4, 656px 811px #00BCD4, 701px 597px #00BCD4, 1202px 46px #FF5722, 890px 569px #FF5722, 1613px 813px #2196F3, 223px 252px #FF9800, 983px 1093px #F44336, 726px 1029px #FFC107, 1764px 778px #CDDC39, 622px 1643px #F44336, 174px 1559px #673AB7, 212px 517px #00BCD4, 340px 505px #FFF, 1700px 39px #FFF, 1768px 516px #F44336, 849px 391px #FF9800, 228px 1824px #FFF, 1119px 1680px #FFC107, 812px 1480px #3F51B5, 1438px 1585px #CDDC39, 137px 1397px #FFF, 1080px 456px #673AB7, 1208px 1437px #03A9F4, 857px 281px #F44336, 1254px 1306px #CDDC39, 987px 990px #4CAF50, 1655px 911px #00BCD4, 1102px 1216px #FF5722, 1807px 1044px #FFF, 660px 435px #03A9F4, 299px 678px #4CAF50, 1193px 115px #FF9800, 918px 290px #CDDC39, 1447px 1422px #FFEB3B, 91px 1273px #9C27B0, 108px 223px #FFEB3B, 146px 754px #00BCD4, 461px 1446px #FF5722, 1004px 391px #673AB7, 1529px 516px #F44336, 1206px 845px #CDDC39, 347px 583px #009688, 1102px 1332px #F44336, 709px 1756px #00BCD4, 1972px 248px #FFF, 1669px 1344px #FF5722, 1132px 406px #F44336, 320px 1076px #CDDC39, 126px 943px #FFEB3B, 263px 604px #FF5722, 1546px 692px #F44336;
    animation: animStar 150s linear infinite;
}

? 颜色阴影部分都是一样的,不一样的地方就在于宽高和动画时长。

? 大家可以根据自己的想法去修改不同的宽高和时长哦

? 动画效果需要额外写一下的哦

@keyframes animStar {
    0% {
        transform: translateY(0px);
    }

    100% {
        transform: translateY(-2000px);
    }
}

画灯杆(电线)

? 一般探照灯都是在顶上的,所以就需要用一根电线连接在顶部

<div class="lamp__wrap">
    <div class="lamp">
        <div class="cable"></div>
    </div>
</div>
  • 后面的灯元素相关内容都会在 lamp 样式标签下面哦!
.lamp__wrap {
    max-height: 100vh;
    overflow: hidden;
    max-width: 100vw;
}
.lamp {
    position: absolute;
    left: 0px;
    right: 0px;
    top: 0px;
    margin: 0px auto;
    width: 300px;
    display: flex;
    flex-direction: column;
    align-items: center;
    transform-origin: center top;
    animation-timing-function: cubic-bezier(0.6, 0, 0.38, 1);
    animation: move 5.1s infinite;
}
  • 在处理动画的时候,使用了一个 cubic-bezier 方法,它是用来定义贝塞尔曲线的
@keyframes move {
    0% {
        transform: rotate(40deg);
    }

    50% {
        transform: rotate(-40deg);
    }

    100% {
        transform: rotate(40deg);
    }
}
  • 动画效果就是将灯杆旋转不同的角度

注意一下,动画效果是在整个灯的样式中完成的,所以后面的都只需要写各自的样式就行了,不需要补充动画效果。

.cable {
    width: 8px;
    height: 248px;
    background-image: linear-gradient(rgb(32 148 218 / 70%), rgb(193 65 25)), linear-gradient(rgba(0, 0, 0, 0.7), rgba(0, 0, 0, 0.7)), linear-gradient(rgba(0, 0, 0, 0.7), rgba(0, 0, 0, 0.7));
}
  • 灯杆给了一个渐变色的样式效果

画灯罩

? 灯杆已经有了,那就加一个灯罩就行了

<div class="cover"></div>
.cover {
    width: 200px;
    height: 80px;
    background: #0bd5e8;
    border-top-left-radius: 50%;
    border-top-right-radius: 50%;
    position: relative;
    z-index: 200;
}
  • 灯罩是通过不同的 border-radius 的效果画出来的

画灯泡

? 灯泡也是比较简单的样式,一个半圆加一部分阴影即可

<div class="in-cover">
    <div class="bulb"></div>
</div>
.in-cover {
    width: 100%;
    max-width: 200px;
    height: 20px;
    border-radius: 100%;
    background: #08ffff;
    position: absolute;
    left: 0px;
    right: 0px;
    margin: 0px auto;
    bottom: -9px;
    z-index: 100;
}

.in-cover .bulb {
    width: 50px;
    height: 50px;
    background-color: #08fffa;
    border-radius: 50%;
    position: absolute;
    left: 0px;
    right: 0px;
    bottom: -20px;
    margin: 0px auto;
    -webkit-box-shadow: 0 0 15px 7px rgba(0, 255, 255, 0.8), 0 0 40px 25px rgba(0, 255, 255, 0.5), -75px 0 30px 15px rgba(0, 255, 255, 0.2);
    box-shadow: 0 0 25px 7px rgb(127 255 255 / 80%), 0 0 64px 47px rgba(0, 255, 255, 0.5), 0px 0 30px 15px rgba(0, 255, 255, 0.2);
}

来一束追光效果吧

? 追光就是通过一个边框线画出来的

<div class="light"></div>
.light {
    width: 200px;
    height: 0px;
    border-bottom: 900px solid rgb(44 255 255 / 24%);
    border-left: 50px solid transparent;
    border-right: 50px solid transparent;
    position: absolute;
    left: 0px;
    right: 0px;
    top: 270px;
    margin: 0px auto;
    z-index: 1;
    border-radius: 90px 90px 0px 0px;
}
  • 给边框的宽度和背景透明色就可以看出追光的效果了。

文字

? 文字通过定位居中之后,刚好显示在灯光动画效果范围之内

<section class="error">
    <div class="error__content">
        <div class="error__message message">
            <h1 class="message__title">掘金错误页面</h1>
            <p class="message__text">不好意思,你访问的页面不存在,请关灯后重新尝试</p>
        </div>
    </div>
</section>

? 文字颜色和背景色一致之后,通过灯光的透明度效果就可以实现文字显隐了。

.error {
    min-height: 100vh;
    position: relative;
    padding: 240px 0;
    box-sizing: border-box;
    width: 100%;
    height: 100%;
    text-align: center;
    margin-top: 70px;
}

.error__overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    overflow: hidden;
}

.error__content {
    position: absolute;
    top: 50%;
    left: 50%;
    width: 100%;
    -webkit-transform: translate(-50%, -50%);
    transform: translate(-50%, -50%);
}

.error__message {
    text-align: center;
    color: #181828;
}

.message__title {
    font-family: 'Montserrat', sans-serif;
    font-weight: 900;
    text-transform: uppercase;
    letter-spacing: 5px;
    font-size: 5.6rem;
    padding-bottom: 40px;
    max-width: 960px;
    margin: 0 auto;
}

.message__text {
    font-family: 'Montserrat', sans-serif;
    line-height: 42px;
    font-size: 18px;
    padding: 0 60px;
    max-width: 680px;
    margin: auto;
}