html - vue.js 上的红绿灯不工作,如何解决?
问题描述
我使用 vue.js 创建了一个红绿灯,但它不起作用。它应该根据持续时间显示颜色(红色、黄色和绿色)。有没有我错过的问题?
CSS
#screen {
width: 450px;
height: 740px;
background: #222;
border-radius: 12px;
margin: auto;
padding: 23px;
}
.light {
width: 230px;
height: 270px;
display: inline-block;
opacity: 0.2;
border-radius: 100%;
transition: opacity 10s;
margin-bottom: 12px;
}
.active {
opacity: 1;
}
.red {
background: red;
}
.yellow {
background: yellow;
}
.green {
background: green;
}
我的 HTML 我已经创建了 div。
<div id="screen">
<div id="light red" :class="{active: now=='red'}"></div>
<div id="light yellow" :class="{active: now=='yellow'}"></div>
<div id="light green" :class="{active: now=='green'}"></div>
</div>
这是 vue.js 似乎所有内容都在它的位置,并且控制台没有发送任何错误。但我仍然无法理解,为什么它不起作用?
class State {
constructor(name, dur, next){
this.name = name;
this.dur = dur;
this.next = next;
}
}
class Constroller {
trigger(state, callback){
callback(state);
setTimeout(()=>{
this.trigger(state.next, callback);
}, state.dur * 10)
}
}
var app = new Vue({
el: '#screen',
data:{
now: 'red'
},
mounted(){
var constroller = new Constroller();
var red = new State('red', 2);
var yellowRed = new State('yellow', 1);
var yellowGreen = new State('yellow', 1);
var green = new State('green', 3);
red.next = yellowRed;
yellowR.next = green;
green.next = yellowGreen;
yellowG.next = red;
constroller.trigger(red, (state)=>{
this.now = state.name;
});
}
})
我错过了什么吗?我应该重写我的函数吗?
解决方案
好吧,这里有几件事是错误的:
<div id="light red" :class="{active: now=='red'}"></div>
<div id="light yellow" :class="{active: now=='yellow'}"></div>
<div id="light green" :class="{active: now=='green'}"></div>
应该是类而不是 id。
var red = new State('red', 2);
var yellowRed = new State('yellow', 1);
var yellowGreen = new State('yellow', 1);
var green = new State('green', 3);
red.next = yellowRed;
yellowR.next = green;
green.next = yellowGreen;
yellowG.next = red;
你命名yellowRed
,yellowGreen
但随后使用yellowR
和YellowG
无论如何,我在这里做了一个快速的小提琴https://jsfiddle.net/Lo50j4rw/你可以看看,我还调整了一些持续时间的东西。
此外,至少在我的国家,红灯后灯变绿:D
推荐阅读
- javascript - 如何使用 Google 脚本复制数据,写入“-nth”列
- android - 为什么当我向 SQLite 数据库插入值时,这些值设置为 null?
- android - React Native 的 KeyboardAvoidingView keyboardVerticalOffset 与 textinput 上的下划线重叠
- mysql - 即使包含值,MySql“列'数量'不能为空”
- javascript - 使用 jest-image-snapshot 比较图像快照时如何忽略 body 标记中的区域或元素?
- angular - 使用 *ngFor 填充跨 3 个 div 格式化的单选列表
- javafx - JavaFX:需要帮助理解 setControllerFactory
- java - Java/ Talend 根据条件分解字符串并清理数据
- angular - Angular cli 反应形式
- java - Jboss EAP 6 到 EAP 7 升级 - jar 部署失败