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 Vue's hash jump principle

Table of contents The difference between hash and...

Several ways to implement inheritance in JavaScript

Table of contents Structural inheritance (impleme...

MySQL 8.0.22 winx64 installation and configuration graphic tutorial

mysql 8.0.22 winx64 installation and configuratio...

MySQL GTID comprehensive summary

Table of contents 01 Introduction to GTID 02 How ...

Causes and solutions for MySQL data loss

Table of contents Preface Problem Description Cau...

CSS to achieve floating customer service effect

<div class="sideBar"> <div>...

Example code for Html layered box-shadow effect

First, let’s take a look at the picture: Today we...

An article to understand what is MySQL Index Pushdown (ICP)

Table of contents 1. Introduction 2. Principle II...

Sample code for converting video using ffmpeg command line

Before starting the main text of this article, yo...

Do you know the common MySQL design errors?

Thanks to the development of the Internet, we can...

Summary of some common uses of refs in React

Table of contents What are Refs 1. String type Re...