Vue implements horizontal scrolling of marquee style text

Vue implements horizontal scrolling of marquee style text

This article shares the specific code for Vue to achieve horizontal scrolling of marquee-style text for your reference. The specific content is as follows

need:

At the top of the Vue project, to achieve text scrolling left and right

step:

1. You can encapsulate a component yourself, write it yourself, or copy the following code
2. After encapsulation is completed, it should be introduced, registered, and used in the required components

Code:

Encapsulate a marquee component specifically used to achieve the marquee effect

<template>
<!-- Marquee component -->
  <div class="marquee-wrap" ref="marquee-wrap">
    <div class="scroll" ref="scroll">
      <p class="marquee">{{text}}</p>
      <p class="copy" ref="copy"></p>
    </div>
    <p class="getWidth" ref="getWidth">{{text}}</p>
  </div>
</template>

<script>
export default {
  name: 'marquee',
  props: ['val'],
  data () {
    return {
      timer: null,
      text: ''
    }
  },
  created () {
    let timer = setTimeout(() => {
      this.move()
      clearTimeout(timer)
    }, 1000)
  },
  mounted () {
    for (let item of this.val) {
    this.text += item
    }
  },
  methods: {
    move () {
    let maxWidth = this.$refs['marquee-wrap'].clientWidth
    let width = this.$refs['getWidth'].scrollWidth
      if (width <= maxWidth) return
    let scroll = this.$refs['scroll']
    let copy = this.$refs['copy']
      copy.innerText = this.text
      let distance = 0 
      this.timer = setInterval(() => {
        distance -= 1
        if (-distance >= width) {
          distance = 16
        }
        scroll.style.transform = 'translateX(' + distance + 'px)'
      }, 20)
    }
  },
  beforeDestroy () {
    clearInterval(this.timer)
  }
}
</script>

<style scoped>
  .marquee-wrap {
    width: 100%;
    overflow: hidden;
    position: relative;
  }
  .marquee{
    margin-right: 0.16rem;
  }
  p {
    word-break:keep-all;
    white-space: nowrap;
    font-size: 0.28rem;
  }
  .scroll {
    display: flex;
  }
  .getWidth {
    word-break:keep-all;
    white-space:nowrap;
    position: absolute;
    opacity: 0;
    top: 0;
  }
</style>

In which component is used, import

// Import the marquee componentimport marquee from "@/components/marquee/marquee.vue";

Cite and register

export default {
  components:
  // Register the marquee component,
  },
 }

After registration is completed, the next step is Html style. Use this component in the template template

<Marquee class="realData">
          <ul class="fa-scroll-cont">
            <li v-for="item in realData" :key="item.name">
              <span class="roll-text">{{ item.city }}</span>
            </li>
          </ul>
</Marquee>

Next is the effect diagram:

I took a few more pictures to make the effect more obvious.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Text Marquee Component Based on Vue (npm Component Package)
  • Vue implements simple marquee effect
  • Vue achieves seamless carousel effect (marquee)
  • Vue implements the marquee effect
  • Vue realizes simple effect of running light
  • Vue example code using timer to achieve marquee effect
  • Vue implements a simple marquee
  • Js and VUE to achieve the marquee effect
  • Vue implements a simple marquee effect
  • Detailed explanation of how to use the Vue marquee component

<<:  Introduction to the role of HTML doctype

>>:  Several methods to modify CSS style to achieve gray web pages (no color, only light black and white)

Recommend

Detailed explanation of Bootstrap grid vertical and horizontal alignment

Table of contents 1. Bootstrap Grid Layout 2. Ver...

Installing MySQL 8.0.12 based on Windows

This tutorial is only applicable to Windows syste...

HTML framework_Powernode Java Academy

1. Framework A browser document window can only d...

Detailed explanation of how components communicate in React

1. What is We can split the communication between...

Example of how to create a local user in mysql and grant database permissions

Preface When you install MySQL, you usually creat...

Tutorial on building an FTP server in Ubuntu 16.04

Ubuntu 16.04 builds FTP server Install ftp Instal...

Install and build a server environment of PHP+Apache+MySQL on CentOS

Yum (full name Yellow dog Updater, Modified) is a...

Docker deploys Mysql, .Net6, Sqlserver and other containers

Table of contents Install Docker on CentOS 8 1. U...

How to generate mysql primary key id (self-increment, unique and irregular)

Table of contents 1. Use the uuid function to gen...

Vue uses OSS to upload pictures or attachments

Use OSS to upload pictures or attachments in vue ...

Detailed explanation of root directory settings in nginx.conf

There are always some problems when configuring n...

Implementation of Docker data volume operations

Getting Started with Data Volumes In the previous...