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 1. Bootstrap Grid Layout 2. Ver...
Copy code The code is as follows: <input type=...
This tutorial is only applicable to Windows syste...
1. Framework A browser document window can only d...
1. What is We can split the communication between...
Preface When you install MySQL, you usually creat...
Ubuntu 16.04 builds FTP server Install ftp Instal...
Yum (full name Yellow dog Updater, Modified) is a...
Mainly from two aspects: 1. Highlight/Line Break ...
Table of contents Install Docker on CentOS 8 1. U...
Table of contents 1. Switch between production en...
Table of contents 1. Use the uuid function to gen...
Use OSS to upload pictures or attachments in vue ...
There are always some problems when configuring n...
Getting Started with Data Volumes In the previous...