Kros的博客 Kros的博客
首页
  • CSS
  • 工具
  • Vue
  • js
  • Vue3
  • 算法
  • 折腾笔记
一言
  • 分类
  • 标签
  • 归档
码云

Kros

凡心所向,素履以往,生如逆旅,一苇以航
首页
  • CSS
  • 工具
  • Vue
  • js
  • Vue3
  • 算法
  • 折腾笔记
一言
  • 分类
  • 标签
  • 归档
码云
  • CSS

    • CSS绘制三角形
    • CSS排版上下文
    • CSS伪类
    • CSS伪元素
    • CSS实现列表元素分割线
    • CSS使用skew
    • CSS让物体居中
    • CSS绘制太极图
    • CSS使用实体替代预留符号
    • 动态修改after或before的content
    • CSS旋转border
    • CSS绘制空心五角星
    • CSS时钟
    • CSS图片手风琴伸缩效果
    • CSS的选择器
    • CSS波浪border
    • CSS内切圆角
    • CSS文字环绕
    • CSS outline使用详解
    • CSS使用transform缩放scale不生效
    • CSS使用linear-gradient或生成虚线
    • CSS 图片srcset属性
    • CSS object-fit属性详解
    • CSS 使用filter对颜色就行处理
    • CSS选择器的优先级
    • css与scss文件引入问题
    • CSS实现圆角梯形
    • 使用filter实现聚光灯效果
    • 利用backface-visibility hidden实现3d翻转双面card
    • 正方体3d旋转
    • 修改浏览器自动填充的背景颜色
    • CSS修改选中文字颜色
    • CSS实现打字机效果
    • marquee文字弹跳
    • logo文字图片倒影
    • 纯CSS实现popup弹窗
    • ie兼容inline-block
    • 浏览器默认样式
    • CSS效果-文字
    • CSS效果-按钮
    • css3 transition属性对linear-gradient渐变色无效
    • 不同预处理器下的样式穿透
    • 属性顺序
    • CSS重绘与回流
    • img标签与before和after
    • CSS实现文本两端对齐
    • CSS filter的contrast属性和blur属性奇特的结合
    • CSS不常用属性记录
    • CSS sprite雪碧图制作与使用
  • JavaScript

  • 工具

  • Vue

  • antdv踩坑记录

  • Vue3

  • 前端
  • CSS
kros
2025-08-05

marquee文字弹跳

最进在mdn中发现一个有意思的标签marquee ,该组件可以实现基础的内容滚动弹跳,效果如下:

marquee

<marquee>该文本将从右向左滚动</marquee>

<marquee direction="up">该文本将从下往上滚动</marquee>

<marquee
  direction="down"
  width="250"
  height="200"
  behavior="alternate"
  style="border:solid">
  <marquee behavior="alternate">该文本将弹跳</marquee>
</marquee>
1
2
3
4
5
6
7
8
9
10
11
12

不过该组件已被移除web标准不被推荐使用,想要实现该标签的效果我们可以使用CSS或javascript实现。

# js实现

  • 使用requestAnimationFrame实现平滑动画。
  • 计算x和y的位置变化,并在碰到边界时反转方向(speed *= -1)。
  • 动态计算box.clientWidth和text.clientWidth确保文字不会超出容器。

js-marquee

<div class="box">
  <div class="text">文本弹跳</div>
</div>

<script>
const text = document.querySelector('.text');
const box = document.querySelector('.box');

let x = 0, y = 0;
let xSpeed = 1, ySpeed = 1;

function marqueeAnim() {
  x += xSpeed;
  y += ySpeed;

  // 检测水平边界并反转方向
  if (x <= 0 || x >= box.clientWidth - text.clientWidth) {
    xSpeed *= -1;
  }

  // 检测垂直边界并反转方向
  if (y <= 0 || y >= box.clientHeight - text.clientHeight) {
    ySpeed *= -1;
  }

  text.style.left = x + 'px';
  text.style.top = y + 'px';

  requestAnimationFrame(marqueeAnim);
}

marqueeAnim();
</script>

<style>
.box {
  position: relative;
  width: 250px;
  height: 200px;
  border: 1px solid;
}

.text {
  position: absolute;
}
</style>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46

# css实现

  • 使用animation: 3s linear infinite alternate实现x、y方向位置弹跳动画,x、y设置不同的播放时间实现不同角度的弹跳。
  • 固定跳动元素的宽高确保文字不超过边框

css-marquee

<div class="box">
	<div class="text">css文本弹跳</div>
</div>

<style>
.box {
  width: 250px;
  height: 200px;
  border: 1px solid;
  position: relative;
  overflow: hidden;
}

.text {
  position: absolute;
  width: 100px;
  left: 20px;
  line-height: 32px;
  text-align: center;
  white-space: nowrap;
  animation: 
    bounceX 2.5s linear infinite alternate,
    bounceY 3s linear infinite alternate;
}

@keyframes bounceX {
  0% { left: 0; }
  100% { left: calc(100% - 100px); }
}

@keyframes bounceY {
  0% { top: 0; }
  100% { top: calc(100% - 32px); }
}
</style>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
上次更新: 2025/09/05, 8:09:00
CSS实现打字机效果
logo文字图片倒影

← CSS实现打字机效果 logo文字图片倒影→

最近更新
01
Find the next perfect square
09-05
02
Regex validate PIN code
09-05
03
Find the odd int
09-05
更多文章>
Theme by Vdoing | Copyright © 2020-2025 kros king
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式
icon-heart-o icon-heart icon-infinity icon-pause icon-play link next prev