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

html部分代碼
<div class="app">
?? ??? ??? ?<p :class="{tabcolor:color}">{{str}}</p>
?? ??? ??? ?<button @click="play">開始</button>
?? ??? ??? ?<button @click="stop">停止</button>
</div>注意: :class="{tabcolor:color}" 是給<p></p>標(biāo)簽內(nèi)的文字加上一個顏色,當(dāng)我們點(diǎn)擊開始按鈕的時候。
CSS部分代碼
.tabcolor {
?? ??? ??? ??? ?color: cornflowerblue;
?? ??? ??? ?}CSS部分的代碼很簡單,就是給了一個添加顏色的類。
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上定義一個字符串,這個字符串就是要在<p></p>標(biāo)簽里進(jìn)行滾動的。
2、給開始和關(guān)閉按鈕,綁定事件:v-on; @cliick就是v-on的簡寫。
3、在按鈕的事件函數(shù)中,寫相關(guān)的業(yè)務(wù)邏輯代碼:拿到str字符串,然后 調(diào)用字符串 slice 來進(jìn)行字符串的截取操作,把第一個字符截取出來,放到最后一個位置即可。
4、為了實(shí)現(xiàn)最終結(jié)果,自動截取的功能,需要把步驟三的代碼放到一個定時器中去。
最后在送上完整代碼
<!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">開始</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>以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Vue.js3.2響應(yīng)式部分的優(yōu)化升級詳解
這篇文章主要為大家介紹了Vue.js3.2響應(yīng)式部分的優(yōu)化升級詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07
vue2封裝webSocket的實(shí)現(xiàn)(開箱即用)
在Vue2中,可以使用WebSocket實(shí)時通信,本文主要介紹了vue2封裝webSocket的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-08-08
ssm+vue前后端分離框架整合實(shí)現(xiàn)(附源碼)
這篇文章主要介紹了ssm+vue前后端分離框架整合實(shí)現(xiàn)(附源碼),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
Vue簡單封裝axios網(wǎng)絡(luò)請求的方法
這篇文章主要介紹了Vue簡單封裝axios網(wǎng)絡(luò)請求,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,對Vue封裝axios網(wǎng)絡(luò)請求相關(guān)知識感興趣的朋友一起看看吧2022-11-11
Vue實(shí)現(xiàn)路由跳轉(zhuǎn)和嵌套
本篇文章主要介紹了Vue實(shí)現(xiàn)路由跳轉(zhuǎn)和嵌套,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06

