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 class="text" :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ì)注冊(cè)一個(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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于vue組件實(shí)現(xiàn)猜數(shù)字游戲
這篇文章主要為大家詳細(xì)介紹了基于vue組件實(shí)現(xiàn)猜數(shù)字游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11
Vue(element ui)中操作row參數(shù)的使用方式
這篇文章主要介紹了Vue(element ui)中操作row參數(shù)的使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-04-04
Element樹形控件整合帶圖標(biāo)的下拉菜單(tree+dropdown+input)
Element UI 官網(wǎng)提供的樹形控件包含基礎(chǔ)的、可選擇的、自定義節(jié)點(diǎn)內(nèi)容的、帶節(jié)點(diǎn)過濾的以及可拖拽節(jié)點(diǎn)的樹形結(jié)構(gòu),本文實(shí)現(xiàn)了樹形控件整合帶圖標(biāo)的下拉菜單,感興趣的可以了解一下2021-07-07
Element-ui table中過濾條件變更表格內(nèi)容的方法
下面小編就為大家分享一篇Element-ui table中過濾條件變更表格內(nèi)容的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-03-03

