1664074923343
Html
<body>
<div id="app">
<p>{{str}}</p>
<button @click="play()">飘</button>
<button @click="stop()">定</button>
</div>
</body>
vue.js
<script src="js/vue.min.js"></script>
<script>
new Vue({
el: "#app",
data() {
return {
str: "大风起兮云飞扬",
timer: null
}
},
//Vue创建完毕,自动会自行play
// created() {
// this.play()
// },
methods: {
play() {
clearInterval(this.timer);
//停止动画
this.timer = setInterval(() => {
//字符串转数组
var temp = this.str.split('');
//获取第一个字符串
var first = temp.shift();
//添加到最后
temp.push(first);
//转换为字符串。赋值个str
this.str = temp.join('')
}, 50);
},
stop() {
clearInterval(this.timer)
}
}
})
</script>
全部代码
<!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>
</head>
<body>
<div id="app">
<p>{{str}}</p>
<button @click="play()">飘</button>
<button @click="stop()">定</button>
</div>
</body>
<script src="js/vue.min.js"></script>
<script>
new Vue({
el: "#app",
data() {
return {
str: "大风起兮云飞扬",
timer: null
}
},
//Vue创建完毕,自动会自行play
// created() {
// this.play()
// },
methods: {
play() {
clearInterval(this.timer);
//停止动画
this.timer = setInterval(() => {
//字符串转数组
var temp = this.str.split('');
//获取第一个字符串
var first = temp.shift();
//添加到最后
temp.push(first);
//转换为字符串。赋值个str
this.str = temp.join('')
}, 50);
},
stop() {
clearInterval(this.timer)
}
}
})
</script>
</html>