前端每日实战:7# 视频演示如何用纯 CSS 创作一个 3D 文字跑马灯特效

图片描述

效果预览

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

https://codepen.io/zhang-ou/pen/GdrrZq

可交互视频教程

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

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

https://scrimba.com/c/cWknNUR

源代码下载

请从 github 下载。

https://github.com/comehope/front-end-daily-challenges/tree/master/007-3d-text-marquee-effects

代码解读

定义 dom,包含2组重复的文字:

<div class="box">
    <div class="inner">
        <span>Hello World</span>
    </div>
    <div class="inner">
        <span>Hello World</span>
    </div>
</div>

居中显示:

html,
body {
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
}

设置容器的尺寸和文字样式:

.box {
    display: flex;
}

.box .inner {
    width: 200px;
    height: 100px;
    line-height: 100px;
    font-size: 32px;
    font-family: sans-serif;
    font-weight: bold;
    white-space: nowrap;
}

配色:

.box .inner:first-child {
    background-color: indianred;
    color: darkred;
}

.box .inner:last-child {
    background-color: lightcoral;
    color: antiquewhite;
}

设置 3d 效果:

.box .inner:first-child {
    transform-origin: left;
    transform: perspective(300px) rotateY(-67.3deg);
}

.box .inner:last-child {
    transform-origin: right;
    transform: perspective(300px) rotateY(67.3deg);
}

定义动画效果:

@keyframes marquee {
    from {
        left: 100%;
    }

    to {
        left: -100%;
    }
}

把动画效果应用到文字上,并隐藏容器外的内容:

.box .inner span {
    position: absolute;
    animation: marquee 5s linear infinite;
}

.box .inner {
    overflow: hidden;
}

让左侧的文字延迟运动,模拟出2组文字连贯运动的效果:

.box .inner:first-child span {
    animation-delay: 2.5s;
    left: -100%;
}

大功告成!

知识点

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值