使用animation实现文字无限横向滚动

本文介绍了一个需求,当文字长度超过容器宽度时,利用CSS3和JavaScript实现匀速无缝横向滚动效果。同时,通过监听resize事件并结合防抖函数throttle,确保在窗口大小变化时能自动调整滚动状态。

需求

  1. 若文字长度不超过容器宽度,则不滚动, 反之滚动(复制一遍文字,让滚动无缝衔接)。
  2. 匀速滚动(运动时间 = 路程 / 速度)。
  3. 监听resize事件,结合防抖throttle,触发后重新计算是否需要滚动。

实现

<template>
<div>
  <div class="notice" ref="container">
    <p ref="notice">{{notice}}</p>
  </div>
</div>
</template>
<script>
import { throttle } from '@/utils'
export default {
  data () {
    return {
      defaultTxt: '测试环境用于测试测试环境用于测试测试环境用于测试测试环境用于测试测试环境用于测试测试环境用于测试测试环境用于测试测试环境用于测试测试环境用于测试。。。。。测试环境用于测试测试环境用于测试测试环境用于测试测试环境用于测试测试环境用于测试测试环境用于测试测试环境用于测试测试环境用于测试测试环境用于测试。。。。。测试环境用于测试测试环境用于测试测试环境用于测试测试环境用于测试测试环境用于测试测试环境用于测试测试环境用于测试测试环境用于测试测试环境用于测试。。。。。测试环境用于测试测试环境用于测试测试环境用于测试测试环境用于测试测试环境用于测试测试环境用于测试测试环境用于测试测试环境用于测试测试环境用于测试。。。。。测试环境用于测试测试环境用于测试测试环境用于测试测试环境用于测试测试环境用于测试测试环境用于测试测试环境用于测试测试环境用于测试测试环境用于测试。。。。。',
      notice: ''
    }
  },
  mounted () {
    this.getStyle()
    window.addEventListener('resize', throttle(this.getStyle, 1000, true))
  },
  methods: {
    getStyle () {
      this.notice = this.defaultTxt
      this.$nextTick(() => {
        let containerW = this.$refs.container.clientWidth
        let noticeW = this.$refs.notice.clientWidth
        console.log(containerW, noticeW)
        if (noticeW > containerW) {
          this.notice = this.defaultTxt + this.defaultTxt
          this.$refs.notice.className = 'run'
          this.$refs.notice.style.animationDuration = noticeW / 100 + 's' // 匀速
        } else {
          this.notice = this.defaultTxt
          this.$refs.notice.className = ''
          this.$refs.notice.style.animationDuration = 0
        }
      })
    }
  },
  destroyed () {
    window.onresize = null
  }
}
</script>
<style lang="scss" scoped>
.notice {
  position: relative;
  width: 100%;
  overflow: hidden;
  height: 30px;
  line-height: 30px;
  background: rgb(247, 225, 225);
  color: #666;
  @keyframes run {
    from {
      transform: translateX(0);
    }
    to {
      transform: translateX(-50%);
    }
  }
  p {
    position: absolute;
    top: 0;
    left: 0;
    white-space: nowrap;
  }
  .run {
    animation: 20s run linear infinite;
  }
}
</style>

附上防抖throttle代码

export const throttle = (fn, delay = 1000, last = false) => {
  let timer = null;
  let start = new Date();
  return function () {
    last && timer && clearTimeout(timer);
    let now = new Date();
    let context = this;
    let args = arguments;
    if (now - start >= delay) {
      fn.apply(context, args);
      start = now;
    } else {
      if (last) { // 脱离事件后执行最后一次 // 一般用于触底加载之类 // 防止重复提交不需要执行最后一次
        timer = setTimeout(() => {
          fn.apply(context, args);
        }, delay - (now - start));
      }
    }
  }
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值