分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 代码编程

如何用纯 CSS 创作一个 3D 文字跑马灯特效

发布时间:2023-09-06 02:23责任编辑:胡小海关键词:CSS

效果预览

在线演示

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

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>

<p>居中显示:</p>

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

<p>设置容器的尺寸和文字样式:</p>

.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;
}

<p>配色:</p>

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

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

<p>设置 3d 效果:</p>

.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);
}

<p>定义动画效果:</p>

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

to { ???left: -100%;}

}

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

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

.box .inner {
overflow: hidden;
}

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

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

大功告成!

知识点

  • transform-origin https://developer.mozilla.org/en-US/docs/Web/CSS/transform-origin
  • perspective https://developer.mozilla.org/en-US/docs/Web/CSS/perspective
  • rotateY() https://developer.mozilla.org/en-US/docs/Web/CSS/transform-function/rotateY
  • animation-delay https://developer.mozilla.org/en-US/docs/Web/CSS/animation-delay

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

如何用纯 CSS 创作一个 3D 文字跑马灯特效

原文地址:https://www.cnblogs.com/lalalagq/p/9986420.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved