javascript - setInterval 在 VueJs 中更改样式时仅工作一次
问题描述
我试图运行这个用按钮启动效果的简单代码。Effect 应该在每个新的 setInterval 刻度上交替使用“highlight”或“shrink”类(将相应的类附加到下面 id 为“effect”的 div),这里是 html:
<script src="https://npmcdn.com/vue/dist/vue.js"></script>
<div id="exercise">
<!-- 1) Start the Effect with the Button. The Effect should alternate the "highlight" or "shrink" class on each new setInterval tick (attach respective class to the div with id "effect" below) -->
<div>
<button @click="startEffect">Start Effect</button>
<div id="effect" :class="effectClasses"></div>
</div>
这是CSS:
#effect {
width: 100px;
height: 100px;
border: 1px solid black;
}
.highlight {
background-color: red;
width: 200px !important;
}
.shrink {
background-color: gray;
width: 50px !important;
}
这是JS文件:
new Vue({
el: '#exercise',
data: {
effectClasses :{
highlight:false,
shrink:true}
},
methods: {
startEffect: function() {
var vm = this;
setInterval(function(){
console.log('higlight 1:'+ vm.effectClasses.highlight);
vm.effectClasses.highlight = !vm.effectClasses.highlight; console.log('higlight 2 :'+ vm.effectClasses.highlight);
console.log('shrink 1 :' + vm.effectClasses.shrink);
vm.effectClasses.shrink = !vm.effectClasses.shrink;
console.log('shrink 2 :' +vm.effectClasses.shrink);
}, 1000);
}
}
});
控制台日志工作正常,但样式不会每 1 秒更改一次,这很奇怪
解决方案
推荐阅读
- python - 我对 Python 代码有疑问。执行代码时条目显示 [*]
- python - 如何在我的 Docker Compose 容器中升级到 Python3+
- email - 在一天中的随机时间运行 AppleScript 脚本
- html - 无序列表中缺少图标
- ios - 在 Swift 中以编程方式制作 UIButton 时如何传递函数
- python - discord.py 服务器模板
- python - 绘制具有数据间隔的直方图
- java - 在“finally”子句中使用 try-with-resources/关闭此“PreparedStatement”
- javascript - 使用 Google 登录 cookie 打开一个已经登录的页面(puppeteer)
- r - 由于 R 中的内生性,2SLS 时间序列的 GMM