手把手教你用VUE封裝一個文本滾動組件
一、引言
項目有文本滾動展示的需求,一開始使用marquee標(biāo)簽來實現(xiàn)需求,但是看一下MDN對該標(biāo)簽的描述:

這個標(biāo)簽已經(jīng)不再推薦使用了,那就自己封裝一個類似的組件來用。
二、實現(xiàn)思路
1.如何讓文本滾動起來?
通過動畫我們設(shè)置過渡transform:translateX(a) => transfrom:translateX(b)來實現(xiàn)向左或向右的滾動效果。transform:translateY(c) => transfrom:translateY(d)來實現(xiàn)向下或向上的滾動效果。
2.組件需要哪些配置?
(1)滾動的方向:上右下左
(2)滾動的速度:這里我以px/s作為單位
三、實現(xiàn)過程
1.html

2.css

最外層的div為滾動的可視區(qū),里面的div為文本滾動區(qū)。可視區(qū)寬度高度均為100%使用時大小由外部容器決定,overflow設(shè)為hidden,防止文本滾動區(qū)滾動出可視區(qū)外仍可見。文本滾動區(qū)設(shè)置寬高都為fit-content使大小隨內(nèi)容自適應(yīng),內(nèi)部設(shè)置插槽使用組件時插入滾動內(nèi)容。
3.動畫
動畫應(yīng)設(shè)置在文本滾動區(qū)上
需要上右下左四個方向滾動的動畫,我們只需要定義上左兩個方向,另外兩個方向直接反轉(zhuǎn)即可。先來想想向上滾動的動畫怎么寫,向上滾動的話文本滾動區(qū)的起點應(yīng)設(shè)置在可視區(qū)外正下方,所以動畫的起點應(yīng)為 transform: translateY(可視區(qū)的高度),終點應(yīng)設(shè)置在可視區(qū)外正上方應(yīng)為transform: translateY(-100%)。

向上向左動畫即為:

--text-scroll-height、--text-scroll-width兩個css變量的值是可視區(qū)的高度和寬度,由于可視區(qū)的寬高不確定,所以需要通過js獲取并設(shè)置這兩個css變量。
注意: 動畫樣式不能加上scoped,否則不生效!
4.js
(1)組件配置

通過props傳入組件配置
(2)計算得到滾動動畫

scrollLength為一次動畫的實際滾動的長度,time為一次動畫的持續(xù)時間。我們可以算出scrollLength應(yīng)為:可視區(qū)的寬或高加上文本滾動區(qū)的寬或高(根據(jù)滾動的方向來判斷是寬還是高)。time應(yīng)為 scrollLegnth / 組件配置的滾動速度。
(3)生命周期
在mounted里調(diào)用上面的方法初始化組件。在updated里同樣調(diào)用該方法,當(dāng)組件寬高改變或插槽內(nèi)容變動重新計算動畫樣式。

三、完整代碼
<template>
<!-- 文本滾動 -->
<div class="text-scroll" ref="textScroll">
<div class="content" ref="content" :style="scrollAnimation">
<!-- 默認插槽,插入滾動內(nèi)容 -->
<slot></slot>
</div>
</div>
</template>
<script>
export default {
name: "TextScroll",
props: {
/* 滾動方向
* value: up、down、left、right
*/
direction: {
default: "up",
type: String,
},
//滾動速度 單位px/s
speed: {
default: 60,
type: Number,
},
},
data() {
return {
//滾動動畫
scrollAnimation: {},
};
},
methods: {
getScrollAnimation() {
//獲取文本滾動實際顯示寬度高度,設(shè)為css變量,用于設(shè)置動畫開始起始位置
let height = this.$refs.textScroll.offsetHeight;
let width = this.$refs.textScroll.offsetWidth;
this.$refs.content.style.setProperty(
"--text-scroll-height",
`${height}px`
);
this.$refs.content.style.setProperty("--text-scroll-width", `${width}px`);
//滾動長度、時間
let scrollLength, time;
//根據(jù)滾動方向來設(shè)置不同的滾動動畫
switch (this.direction) {
case "up":
scrollLength =
this.$refs.content.offsetHeight +
height;
time = scrollLength / this.speed;
this.scrollAnimation.animation = `up-scroll linear ${time}s infinite`;
break;
case "down":
scrollLength =
this.$refs.content.offsetHeight +
height;
time = scrollLength / this.speed;
this.scrollAnimation.animation = `up-scroll linear ${time}s infinite reverse`;
break;
case "left":
scrollLength =
this.$refs.content.offsetWidth + width;
time = scrollLength / this.speed;
this.scrollAnimation.animation = `left-scroll linear ${time}s infinite`;
break;
case "right":
scrollLength =
this.$refs.content.offsetWidth + width;
time = scrollLength / this.speed;
this.scrollAnimation.animation = `left-scroll linear ${time}s infinite reverse`;
break;
}
},
},
async mounted() {
//設(shè)置文本滾動動畫
this.getScrollAnimation();
},
updated() {
//當(dāng)插槽內(nèi)容更新重新計算滾動動畫
this.getScrollAnimation();
},
};
</script>
<style scoped lang="scss">
.text-scroll {
width: 100%;
height: 100%;
overflow: hidden;
.content {
height: fit-content;
width: fit-content;
}
}
</style>
<style lang="scss">
.text-scroll {
.content {
@keyframes up-scroll {
0% {
transform: translateY(var(--text-scroll-height));
}
100% {
transform: translateY(-100%);
}
}
@keyframes left-scroll {
0% {
transform: translateX(var(--text-scroll-width));
}
100% {
transform: translateX(-100%);
}
}
}
}
</style>總結(jié)
到此這篇關(guān)于用VUE封裝一個文本滾動組件的文章就介紹到這了,更多相關(guān)VUE封裝文本滾動組件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue中ElementUI結(jié)合transform使用時如何修復(fù)el-select彈框定位不準確問題
這篇文章主要介紹了Vue中ElementUI結(jié)合transform使用時如何修復(fù)el-select彈框定位不準確問題,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2024-01-01
element中el-table表頭通過header-row-style設(shè)置樣式
有些時候需要給element-ui表頭設(shè)置不同樣式,本文主要介紹了element中el-table表頭通過header-row-style設(shè)置樣式,具有一定的參考價值,感興趣的可以了解一下2024-01-01
vue-router?導(dǎo)航完成后獲取數(shù)據(jù)的實現(xiàn)方法
這篇文章主要介紹了vue-router?導(dǎo)航完成后獲取數(shù)據(jù),通過使用生命周期的 created() 函數(shù),在組件創(chuàng)建完成后調(diào)用該方法,本文結(jié)合實例代碼給大家講解的非常詳細需要的朋友可以參考下2022-11-11

