Vue3實(shí)現(xiàn)跑馬燈效果
本文實(shí)例為大家分享了Vue3實(shí)現(xiàn)跑馬燈效果的具體代碼,供大家參考,具體內(nèi)容如下
先看效果:

html部分代碼
<div class="app">
?? ??? ??? ?<p :class="{tabcolor:color}">{{str}}</p>
?? ??? ??? ?<button @click="play">開(kāi)始</button>
?? ??? ??? ?<button @click="stop">停止</button>
</div>注意: :class="{tabcolor:color}" 是給<p></p>標(biāo)簽內(nèi)的文字加上一個(gè)顏色,當(dāng)我們點(diǎn)擊開(kāi)始按鈕的時(shí)候。
CSS部分代碼
.tabcolor {
?? ??? ??? ??? ?color: cornflowerblue;
?? ??? ??? ?}CSS部分的代碼很簡(jiǎn)單,就是給了一個(gè)添加顏色的類。
Vue部分代碼
Vue.createApp({
?? ??? ??? ?data() {
?? ??? ??? ??? ?return {
?? ??? ??? ??? ??? ?str: "你好啊,我是穩(wěn)重聰頭~",
?? ??? ??? ??? ??? ?id: null,
?? ??? ??? ??? ??? ?color: false,
?? ??? ??? ??? ?}
?? ??? ??? ?},
?? ??? ??? ?methods: {
?? ??? ??? ??? ?play() {
?? ??? ??? ??? ??? ?clearInterval(this.id);
?? ??? ??? ??? ??? ?this.color = !this.color;
?? ??? ??? ??? ??? ?this.id = setInterval(() => {
?? ??? ??? ??? ??? ??? ?this.str = this.str.slice(1) + this.str.slice(0, 1)
?? ??? ??? ??? ??? ?}, 800)
?? ??? ??? ??? ?},
?? ??? ??? ??? ?stop() {
?? ??? ??? ??? ??? ?clearInterval(this.id);
?? ??? ??? ??? ??? ?this.color = false;
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ?}).mount(".app")分析:
1、data上定義一個(gè)字符串,這個(gè)字符串就是要在<p></p>標(biāo)簽里進(jìn)行滾動(dòng)的。
2、給開(kāi)始和關(guān)閉按鈕,綁定事件:v-on; @cliick就是v-on的簡(jiǎn)寫。
3、在按鈕的事件函數(shù)中,寫相關(guān)的業(yè)務(wù)邏輯代碼:拿到str字符串,然后 調(diào)用字符串 slice 來(lái)進(jìn)行字符串的截取操作,把第一個(gè)字符截取出來(lái),放到最后一個(gè)位置即可。
4、為了實(shí)現(xiàn)最終結(jié)果,自動(dòng)截取的功能,需要把步驟三的代碼放到一個(gè)定時(shí)器中去。
最后在送上完整代碼
<!DOCTYPE html>
<html lang="en">
?
?? ?<head>
?? ??? ?<meta charset="UTF-8">
?? ??? ?<meta http-equiv="X-UA-Compatible" content="IE=edge">
?? ??? ?<meta name="viewport" content="width=device-width, initial-scale=1.0">
?? ??? ?<title>Document</title>
?? ??? ?<script src="./js/vue.js"></script>
?? ??? ?<style type="text/css">
?? ??? ??? ?.tabcolor {
?? ??? ??? ??? ?color: cornflowerblue;
?? ??? ??? ?}
?? ??? ?</style>
?? ?</head>
?
?? ?<body>
?? ??? ?<div class="app">
?? ??? ??? ?<p :class="{tabcolor:color}">{{str}}</p>
?? ??? ??? ?<button @click="play">開(kāi)始</button>
?? ??? ??? ?<button @click="stop">停止</button>
?? ??? ?</div>
?? ?</body>
?? ?<script>
?? ??? ?Vue.createApp({
?? ??? ??? ?data() {
?? ??? ??? ??? ?return {
?? ??? ??? ??? ??? ?str: "你好啊,我是穩(wěn)重聰頭~",
?? ??? ??? ??? ??? ?id: null,
?? ??? ??? ??? ??? ?color: false,
?? ??? ??? ??? ?}
?? ??? ??? ?},
?? ??? ??? ?methods: {
?? ??? ??? ??? ?play() {
?? ??? ??? ??? ??? ?clearInterval(this.id);
?? ??? ??? ??? ??? ?this.color = !this.color;
?? ??? ??? ??? ??? ?this.id = setInterval(() => {
?? ??? ??? ??? ??? ??? ?this.str = this.str.slice(1) + this.str.slice(0, 1)
?? ??? ??? ??? ??? ?}, 800)
?? ??? ??? ??? ?},
?? ??? ??? ??? ?stop() {
?? ??? ??? ??? ??? ?clearInterval(this.id);
?? ??? ??? ??? ??? ?this.color = false;
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ?}).mount(".app")
?? ?</script>
</html>以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- vue實(shí)現(xiàn)可以快進(jìn)后退的跑馬燈組件
- vue+animation動(dòng)畫實(shí)現(xiàn)跑馬燈效果
- Vue實(shí)現(xiàn)跑馬燈樣式文字橫向滾動(dòng)
- Vue實(shí)現(xiàn)跑馬燈簡(jiǎn)單效果
- vue實(shí)現(xiàn)簡(jiǎn)單的跑馬燈效果
- vue實(shí)現(xiàn)簡(jiǎn)單跑馬燈效果
- Vue實(shí)現(xiàn)簡(jiǎn)單的跑馬燈
- Vue實(shí)現(xiàn)跑馬燈效果
- Vue 使用計(jì)時(shí)器實(shí)現(xiàn)跑馬燈效果的實(shí)例代碼
- Vue實(shí)現(xiàn)列表跑馬燈效果
相關(guān)文章
Vue.js3.2響應(yīng)式部分的優(yōu)化升級(jí)詳解
這篇文章主要為大家介紹了Vue.js3.2響應(yīng)式部分的優(yōu)化升級(jí)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07
vue2封裝webSocket的實(shí)現(xiàn)(開(kāi)箱即用)
在Vue2中,可以使用WebSocket實(shí)時(shí)通信,本文主要介紹了vue2封裝webSocket的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-08-08
ssm+vue前后端分離框架整合實(shí)現(xiàn)(附源碼)
這篇文章主要介紹了ssm+vue前后端分離框架整合實(shí)現(xiàn)(附源碼),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
Vue簡(jiǎn)單封裝axios網(wǎng)絡(luò)請(qǐng)求的方法
這篇文章主要介紹了Vue簡(jiǎn)單封裝axios網(wǎng)絡(luò)請(qǐng)求,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,對(duì)Vue封裝axios網(wǎng)絡(luò)請(qǐng)求相關(guān)知識(shí)感興趣的朋友一起看看吧2022-11-11
vue中PC端地址跳轉(zhuǎn)移動(dòng)端的操作方法
最近小編接到一個(gè)項(xiàng)目pc端和移動(dòng)端是兩個(gè)獨(dú)立的項(xiàng)目,兩個(gè)項(xiàng)目項(xiàng)目中的內(nèi)容基本相同,鏈接組合的方式都有規(guī)律可循,接到的需求便是在移動(dòng)端訪問(wèn)pc端的URL連接時(shí),重定向至移動(dòng)端對(duì)應(yīng)頁(yè)面,下面小編給大家分享實(shí)現(xiàn)過(guò)程,一起看看吧2021-11-11
使用vite構(gòu)建vue3項(xiàng)目的方法步驟
本文主要介紹了使用vite構(gòu)建vue3項(xiàng)目的方法步驟,vite支持性肯定比傳統(tǒng)的打包工具好,下面我們就來(lái)介紹一下使用vite構(gòu)建vue3項(xiàng)目,感興趣的可以參考一下2023-05-05
Vue實(shí)現(xiàn)路由跳轉(zhuǎn)和嵌套
本篇文章主要介紹了Vue實(shí)現(xiàn)路由跳轉(zhuǎn)和嵌套,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-06-06

