這篇文章主要介紹了Vue如何替代marquee標簽超出寬度文字橫向滾動效果,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
成都創新互聯公司專注于企業全網整合營銷推廣、網站重做改版、肥鄉網站定制設計、自適應品牌網站建設、H5高端網站建設、商城開發、集團公司官網建設、成都外貿網站制作、高端網站制作、響應式網頁設計等建站業務,價格優惠性價比高,為肥鄉等各大城市提供網站開發制作服務。一、npm 安裝
如果你想安裝插件(自己寫的)
安裝
# install dependencies npm i marquee-components
使用
在main.js引入
import marquee from 'marquee-components' Vue.use(marquee );
在頁面使用
<template> <div id="app"> <marquee :val="msg"></marquee> </div> </template> <script> export default { name: 'app', data () { return { msg: 'vuevuevuevuevuevuevuevuevuevuevuevuevuevuevuevuevue' } } } </script>
val后加文字即可,當超過文本容器長度時,觸動橫向滾動效果。
二、直接引入組件
marquee組件
<template> <div class="marquee-wrap"> <div class="scroll"> <p class="marquee">{{text}}</p> <p class="copy"></p> </div> <p class="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 = document.querySelector('.marquee-wrap').clientWidth let width = document.querySelector('.getWidth').scrollWidth if (width <= maxWidth) return let scroll = document.querySelector('.scroll') let copy = document.querySelector('.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: 16px; } p { word-break:keep-all; white-space: nowrap; font-size: 16px; font-family: "微軟雅黑 Light"; } .scroll { display: flex; } .getWidth { word-break:keep-all; white-space:nowrap; position: absolute; opacity: 0; top: 0; } </style>
其他頁面引入marquee組件
<template> <div class="container"> <marquee :val="title"></marquee> </div> </template> <script> import marquee from './marquee' name: 'index', components: { marquee }, data () { return { title: '' } }, </script>
感謝你能夠認真閱讀完這篇文章,希望小編分享的“Vue如何替代marquee標簽超出寬度文字橫向滾動效果”這篇文章對大家有幫助,同時也希望大家多多支持創新互聯,關注創新互聯行業資訊頻道,更多相關知識等著你來學習!
網站題目:Vue如何替代marquee標簽超出寬度文字橫向滾動效果-創新互聯
文章位置:http://vcdvsql.cn/article0/ccsjoo.html
成都網站建設公司_創新互聯,為您提供標簽優化、Google、ChatGPT、服務器托管、App設計、網站導航
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯