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 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:
|
<<: Introduction to the role of HTML doctype
Table of contents The difference between hash and...
Table of contents Structural inheritance (impleme...
Effect display: Environment preparation controlle...
Table of contents Overview Environment Preparatio...
mysql 8.0.22 winx64 installation and configuratio...
Table of contents 01 Introduction to GTID 02 How ...
Record the problems you solve for others. Problem...
Table of contents Preface Problem Description Cau...
<div class="sideBar"> <div>...
First, let’s take a look at the picture: Today we...
Table of contents 1. Introduction 2. Principle II...
Before starting the main text of this article, yo...
Thanks to the development of the Internet, we can...
Today, my colleague encountered a very strange pr...
Table of contents What are Refs 1. String type Re...