Vue跑马灯简单案列

套在自己的vue模板里面即可

    <div v-if="this.content" class="marquee-container" ref="marqueeContainer">
    <span :class="{ 'marquee-text': true, 'marquee-move': shouldMove }" ref="scrollingText" v-if="this.showResult">
      {
   {
    content }}
    </span>
    </div>

content是显示的内容,shouldMove是否滚动(如果大于了文本框才滚动,否则禁止不懂),showResult是否显示滚动条

  data() {
   
    return {
   
      content: '',
      shouldMove: true,
      showResult: false,
    }
  },
  created() {
   
    this.getSurvey()
  },
  mounted() {
   
  },
  beforeDestroy() {
   
    window.removeEventListener('resize', this.checkOverflow);
  },
  methods: {
   
     checkOverflow() {
   
      const scrollingText = this.$refs.scrollingText;
      const marqueeContainer = this.$refs.marqueeContainer;
      console.log(scrollingText.scrollWidth)
       console.log(marqueeContainer.clientWidth)
      this.shouldMove = scrollingText.scrollWidth > marqueeContainer.clientWidth;

      if (!this.content || this.content.trim() === '') {
   
        this.shouldMove = false;
      }
    },
    async getSurvey(){
   
      let {
    data, errorCode }=await this._get(this.API.queryMarketSurvey(
          this.menuId
      ))
      if(errorCode==0){
   
        console.log(data)
        this.showResult = data.showResult
        this.content = data.content;
        this.$nextTick(()=>{
   
          this.checkOverflow();
          window.addEventListener('resize', this.checkOverflow);
        })

      }
    },
.marquee-container {
   
  background-color: #7a00e6; /* 背景颜色 */
  color:#fff;
  height: 16px;
  line-height: 16px;
  font-size: 12px;
  padding-left:12px;
  width: 100%;
  white-space: nowrap;
  overflow-x: auto;
}

.marquee-text {
   
  display: inline-block; /* 使文本以块级元素显示 */
  font-family: 'YourFont', sans-serif; /* 替换成你的字体 */
}

.marquee-move {
   
  animation: marquee 10s linear infinite; /* 使用 linear 以保持匀速移动 */
}

@keyframes marquee {
   
  0% {
   
    transform: translateX(100%);
  }
  100% {
   
    transform: translateX(-100%);
  }
}

效果
在这里插入图片描述

相关推荐

  1. vuevue马灯vue-marquee-text-component

    2024-02-21 11:18:03       48 阅读
  2. Android 实现马灯效果

    2024-02-21 11:18:03       50 阅读
  3. Python中实现马灯效果

    2024-02-21 11:18:03       43 阅读
  4. Android中,TextView马灯(marquee)问题

    2024-02-21 11:18:03       37 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-21 11:18:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-21 11:18:03       101 阅读
  3. 在Django里面运行非项目文件

    2024-02-21 11:18:03       82 阅读
  4. Python语言-面向对象

    2024-02-21 11:18:03       91 阅读

热门阅读

  1. bash shell实现简易进度条

    2024-02-21 11:18:03       55 阅读
  2. MySQL 的存储引擎(基本介绍)

    2024-02-21 11:18:03       57 阅读
  3. 数据权限设计思考

    2024-02-21 11:18:03       52 阅读
  4. .net 微服务 服务保护 自动重试 Polly

    2024-02-21 11:18:03       52 阅读
  5. C++ 中的单例模式singleton

    2024-02-21 11:18:03       47 阅读
  6. 设计模式----单例模式

    2024-02-21 11:18:03       45 阅读
  7. 指定截至频率的低通滤波器设计

    2024-02-21 11:18:03       48 阅读