【发布时间】:2020-08-11 15:35:19
【问题描述】:
基本上试图创建一个用户必须单击页面顶部布局的特定单词的游戏,我需要编写 4 个按钮 html 元素以在 div 容器内反弹但是,我的 html 转换间隔不工作。
var upperLimitY = 360;
var lowerLimitY = 0;
var upperLimitX = 520;
var lowerLimitX = 0;
var upperVelocity = 10;
var lowerVelocity = 2;
var velocity = 5;
var wordStore = document.getElementsByClassName("word1")[0];
function startGame() {
setInterval(moveWord, 10);
}
function moveWord() {
if (lowerLimitX < wordStore.style.transform.x < upperLimitX && lowerLimitY <
wordStore.style.transform.y < upperLimitY) {
wordStore.style.transform = "translate(" + velocity + "px ," + velocity + ")";
velocity += velocity;
} else {
velocity *= -1;
}
};
<div class="wordGameContainerHeader">
<h1>Word Wizard</h1>
<h4>WORD TO FIND</h4>
</div>
<div class="wordGameContainer">
<button class="word1">WORD 1</button>
<button class="word2">WORD 2</button>
<button class="word3">WORD 3</button>
<button class="word4">WORD 4</button>
</div>
<div>
<button onclick="startGame()" class="playButton">PLAY</button>
</div>
按钮嵌入在大小为 600px x 300px 的 div 中,元素的宽度为 80px,高度为 40px,这就是为什么我将 x 的上限设置为 600px - 80px,反之亦然限制。正在测试代码的按钮根本不动。
【问题讨论】:
-
可能是因为没有
style.transform.x。你的情况也不会好。
标签: javascript html css