這篇文章主要講解了Vue如何實現簡單的跑馬燈,內容清晰明了,對此有興趣的小伙伴可以學習一下,相信大家閱讀完之后會有幫助。
內容不多,直接看代碼吧
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="../../資料/js/vue.js"></script> <!-- 引入Vue-->
</head>
<style>
*{
text-align: center;
}
</style>
<body>
<div id="app">
<h2>{{txt}}</h2>
<button @click="run">開始</button>
<button @click="stop">停止</button>
</div>
</body>
</html>
<script>
new Vue({
el:'#app',
data:{
txt:"吾疑君馭車而飚之,然苦于無證以示眾。",
timer:null
},
methods:{
run(){
if(this.timer != null){
return;
}
this.timer = setInterval(()=>{
let start = this.txt.substring(0,1);//截取下標為0的字符串
let end = this.txt.substring(1);//截取從下標為1到結束的字符串
this.txt = end + start;
},300);
},
stop(){
clearInterval(this.timer)
}
}
})
</script>效果如下圖:

看完上述內容,是不是對Vue如何實現簡單的跑馬燈有進一步的了解,如果還想學習更多內容,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。