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ù)組長度 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í)行。通過嵌套setTimeout可以實(shí)現(xiàn)循環(huán),但是每次都會(huì)注冊一個(gè)計(jì)時(shí)器,然后時(shí)間上也是需要等當(dāng)前setTimeout執(zhí)行完再延遲比如說兩秒執(zhí)行,實(shí)際上就不只2s。
什么情況下setTimeout嵌套可以解決 setInterval 解決不了的問題 當(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)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. 前端html+css實(shí)現(xiàn)動(dòng)態(tài)生日快樂代碼2. XML入門的常見問題(四)3. XML基本概念XPath、XSLT與XQuery函數(shù)介紹4. CSS3實(shí)例分享之多重背景的實(shí)現(xiàn)(Multiple backgrounds)5. 不要在HTML中濫用div6. 關(guān)于html嵌入xml數(shù)據(jù)島如何穿過樹形結(jié)構(gòu)關(guān)系的問題7. vue實(shí)現(xiàn)復(fù)制文字復(fù)制圖片實(shí)例詳解8. el-input無法輸入的問題和表單驗(yàn)證失敗問題解決9. XML入門的常見問題(三)10. WML的簡單例子及編輯、測試方法第1/2頁
