跑马灯效果展示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>个人界面</title>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id="text">
<input type="button" value="开始效果" :title="beginTitle" @click="begin">
<input type="button" value="停止效果" :title="endTitle" @click="ends">
<br>
{{msg}}
</div>
<script>
const vm = new Vue({
el: "#text",
data: {
msg: "跑马灯效果展示...",
beginTitle: "开始展示效果",
endTitle: "停止效果展示",
intervalID: null
},
methods: {
begin: function () {
/*
* 使用箭头函数,不需要考虑 this 问题
* 删除原来的 function ,在 function 后面的括号后面加上 => 即可改为箭头函数
* 设置定时器
*/
if (this.intervalID == null) {
this.intervalID = setInterval(() => {
this.msg = this.msg.substring(1) + this.msg.substring(0, 1);
}, 300);
}
},
/*可以简化函数*/
ends() {
/*停止定时器*/
clearInterval(this.intervalID);
this.intervalID = null;
this.msg = "跑马灯效果展示...";
}
}
})
;
</script>
</body>
</html>

评论

Your browser is out-of-date!

Update your browser to view this website correctly.&npsb;Update my browser now

×