• CSS实现文字跑马灯效果


    CSS实现文字跑马灯效果

    在完成一个任务的时候,要求在表格中固定宽度的其中一个item文字过长需要滚动显示,然后经过多次效果的尝试,实现代码如下所示:

    • 它需要一个外层包围盒,设置定宽、文字不换行以及超过隐藏
    • 子元素为一个p标签,设置width: fit-content;使盒子背景宽度随文字宽度而进行自适应,并设置一个自定义属性text(可自定义)
    • p标签添加伪元素::after ,设置content: attr(text);
    DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Documenttitle>
        <style>
    		* {
    			margin:0;
                padding: 0;
    		}
    		.container {
                /* 最外层盒子,需要三个属性:定宽、文字不换行、超过隐藏 */
    			width: 500px;
    			white-space: nowrap;
    			overflow: hidden;
    			font-size: 20px;
    			color: #65b4ae;
    		}
    		.container_words {
    			position: relative;
                /* 盒子背景宽度将随文字宽度而进行自适应 */
    			width: fit-content;
                /* 添加动画 */
    			animation: move 4s linear infinite;
                /* 让前面的几个文字有一个初始的距离,达到更好的呈现效果 */
                padding-left: 20px;
    		}
    		.container_words::after {
    			position: absolute; 
    			right: -100%;
    			content: attr(text);
    		}
    		@keyframes move {
    			0% {
    				transform: translateX(0);
    			}
    			100% {
    				transform: translateX(-100%);
    			}
    		}
    	style>
    head>
    <body>
        <div class="container">
            <p class="container_words" text="文字文字文字文字文字">
                文字文字文字文字文字
            p>
        div>
    body>
    html>
    
    • 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
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52

    效果如下:

    在这里插入图片描述

  • 相关阅读:
    双目视觉实战--单视图测量方法
    CF1899B 250 Thousand Tons of TNT
    高性能并行计算华为云实验五:PageRank算法实验
    Using Set Processing Examples 使用集合处理示例
    MySQL集群高可用架构之MMM
    TCP网络通信
    记升级springboot1.X 到springboot2.3.5踩的坑
    第十四届蓝桥杯校内模拟赛(第二期)题解分享
    【C语言】三子棋
    uniapp h5+ 拍照、录音功能实现
  • 原文地址:https://blog.csdn.net/qq_40864647/article/details/126242223