vue實(shí)現(xiàn)公告欄文字上下滾動(dòng)效果的示例代碼
本文詳細(xì)的介紹了vue實(shí)現(xiàn)公告欄文字上下滾動(dòng)效果的示例代碼,分享給大家,具體入如下:
代碼實(shí)現(xiàn):
在項(xiàng)目結(jié)構(gòu)的components中新建text-scroll.vue文件
<template> <div class='text-container'> <transition class='' name='slide' mode='out-in'> <p :key='text.id'>{{text.val}}</p> </transition> </div></template><script>export default { name: ’TextScroll’, props: { dataList: { type: Array, default() { return []; }, }, }, data() { return { count: 0, // 當(dāng)前索引 intervalId: null, // 定時(shí)器ID playTime: 2000, // 定時(shí)器執(zhí)行間隔 }; }, computed: { text() { return { id: this.count, val: this.dataList[this.count], }; }, }, created() { this.intervalId = setInterval(() => { // 定義定時(shí)器 this.getText(); }, this.playTime); }, methods: { getText() { const len = this.dataList.length; // 獲取數(shù)組長(zhǎng)度 if (len === 0) { return; // 當(dāng)數(shù)組為空時(shí),直接返回 } if (this.count === len - 1) { // 當(dāng)前為最后一個(gè)時(shí) this.count = 0; // 從第一個(gè)開始 } else { this.count++; // 自增 } }, }, destroyed() { clearInterval(this.intervalId); // 清除定時(shí)器 },};</script><style scoped>.text-container{ font-size: 14px; color: #F56B6B; margin-right: 20px; height: 60px;}.text { text-align: right; margin: auto 0;}.slide-enter-active, .slide-leave-active { transition: all 1s;}.slide-enter{ transform: translateY(40px);}.slide-leave-to { transform: translateY(-40px);}</style>
在header-bar組件使用
<text-scroll :dataList='noticeList'></text-scroll>
分析
transition標(biāo)簽

這里是動(dòng)態(tài)組件
官方文檔:https://cn.vuejs.org/v2/guide/transitions.html
為什么用setInterval,而不是setTimeout
setInterval是循環(huán)執(zhí)行,setTimeout是延遲執(zhí)行。我們這里要的是setTimeout循環(huán)執(zhí)行。通過(guò)嵌套setTimeout可以實(shí)現(xiàn)循環(huán),但是每次都會(huì)注冊(cè)一個(gè)計(jì)時(shí)器,然后時(shí)間上也是需要等當(dāng)前setTimeout執(zhí)行完再延遲比如說(shuō)兩秒執(zhí)行,實(shí)際上就不只2s。
什么情況下setTimeout嵌套可以解決 setInterval 解決不了的問(wèn)題 當(dāng)計(jì)時(shí)器是高耗時(shí)的計(jì)算或者dom操作時(shí),時(shí)間大于延遲時(shí)間
到此這篇關(guān)于vue實(shí)現(xiàn)公告欄文字上下滾動(dòng)效果的示例代碼的文章就介紹到這了,更多相關(guān)vue 公告欄文字上下滾動(dòng) 內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. idea導(dǎo)入maven項(xiàng)目的方法2. IntelliJ IDEA設(shè)置自動(dòng)提示功能快捷鍵的方法3. IntelliJ IDEA安裝插件的方法步驟4. Docker 部署 Prometheus的安裝詳細(xì)教程5. idea重置默認(rèn)配置的方法步驟6. 通過(guò)Django Admin+HttpRunner1.5.6實(shí)現(xiàn)簡(jiǎn)易接口測(cè)試平臺(tái)7. idea打開多個(gè)窗口的操作方法8. idea設(shè)置代碼格式化的方法步驟9. idea給項(xiàng)目打war包的方法步驟10. IntelliJ IDEA設(shè)置背景圖片的方法步驟

網(wǎng)公網(wǎng)安備