• 纯 CSS 实现 超长内容滚动播放。


    需求:文案来回在指定区域内水平滚动,并且在头尾处停留一段时间 ​

    以vue为例,封装一个通用组件

    <template>
      <view class="box">
        <view class="scroll-wrap">
          <view class="scroll-item" :style="{color:color,fontWeight:fontWeight, fontSize:`${fontSize}rpx`}">
            {{ text}}
          </view>
        </view>
      </view>
    </template>
    
    <script>
      // 使用需要外层套一个div给盒子宽度
      export default {
        props: {
          // 滚动的文本
          text: {
            type: String,
            default: '',
          },
          // 颜色
          color: {
            type: String,
            default: '#333333',
          },
          // 文字大小
          fontSize: {
            type: String,
            default: '24',
          },
          fontWeight: {
            type: String,
            default: '500',
          }
        },
        data() {
          return {};
        },
        mounted() {},
        methods: {},
      };
    </script>
    
    <style lang="scss" scoped>
      .box {
        width: 100%;
        // border: 1px solid;
      }
    
      .scroll-wrap {
        max-width: 100%;
        display: inline-block;
        vertical-align: top;
        overflow: hidden;
        white-space: nowrap;
    
      }
    
      .scroll-item {
    
        animation: scroll linear 4s alternate infinite;
        float: left;
      }
    
      @keyframes scroll {
        0% {
          margin-left: 0;
          transform: translateX(0);
        }
    
        10% {
          margin-left: 0;
          transform: translateX(0);
        }
    
        90% {
          margin-left: 100%;
          transform: translateX(-100%);
        }
    
        100% {
          margin-left: 100%;
          transform: translateX(-100%);
        }
      }
    </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
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85

    使用组件

    <view style="width:200rpx;">
      <text-scroll :text="我很长啊我很长啊我很长啊我很长啊我很长啊" fontSize="32" fontWeight="600"></text-scroll>
    </view>
    
    • 1
    • 2
    • 3

    简单解释一下当前 animation 的属性

    • animation-name:所使用的 @keyframes 的名称 scroll
    • animation-timing-function:动画的速度曲线,linear 表示匀速
    • animation-duration:规定完成动画所花费的时间为 4s
    • animation-direction:是否应该轮流反向播放动画,alternate 表示在偶数次会反向播放
    • animation-iteration-count:定义动画的播放次数,infinite 表示无限次
  • 相关阅读:
    spring mvc上传文件MultipartHttpServletRequest值为空
    走进JUC的世界
    结构化分析建模的基本步骤
    卷积积分的计算
    使用 MySQL 进行分页
    Ubuntu 20.04下OpenCV的安装
    [UML]类的关系与类图
    可用于高质量回测的 MetaTrader 历史数据导入及转换教程
    强化学习与视觉语言模型之间的碰撞,UC伯克利提出语言奖励调节LAMP框架
    技术应用:使用Spring Boot、MyBatis Plus和Dynamic DataSource实现多数据源
  • 原文地址:https://blog.csdn.net/renlimin1/article/details/136274895