使用网络技术,我仅使用HTML和CSS(不使用Javascript)开发了一个骰子。这是一个简单的系统:一系列单选按钮和标签,它们通过更改其位置(带有z-index)来模拟骰子,因此每次单击骰子时,都会有一个“随机”数字。
z-index
这是代码的最低版本:
@keyframes changeOrder { from { z-index: 6;} to { z-index: 1; } } @-webkit-keyframes changeOrder { from { z-index: 6; } to { z-index: 1; } } label { display: block; position: absolute; display: block; width: 50px; height: 50px; line-height:50px; background: #eeeeee; text-align: center; animation: changeOrder 1.2s infinite; } label:nth-of-type(1) { animation-delay: 0s; } label:nth-of-type(2) { animation-delay: -0.2s; } label:nth-of-type(3) { animation-delay: -0.4s; } label:nth-of-type(4) { animation-delay: -0.6s; } label:nth-of-type(5) { animation-delay: -0.8s; } label:nth-of-type(6) { animation-delay: -1.0s; }
1 2 3 4 5 6
即使在骰子滚动时,问题也发生在,即使当骰子滚动时,label单击时它也不总是采取措施,并且label未激活与关联的单选按钮。有时确实如此,有时却没有。
label
我以为可能是因为我使用了动画,并且(不成功地)与时俱进,看能否解决问题……但是基本上还是一样。我注意到,如果我延长时间,该问题将“消失”(即,将时间更改为3s并延迟0.5s或更高)。但是,如果这样做,它是可以预测的(目标不是使其完美,而是至少模拟一些伪随机性)。
为什么会这样呢?我该如何解决?
正如您已经注意到的那样,问题出在动画的速度上。更改要比单击速度快,因为单击是两个操作:mousedown和mouseup都应在同一元素上完成。
mousedown
mouseup
这是一个更好的问题说明,您永远无法通过单击标签来检查输入:
label { display: block; position: absolute; display: block; width: 50px; height: 50px; line-height:50px; background: #eeeeee; text-align: center; } label:active { background:red; z-index:-1; }
1 2