gpt4 book ai didi

javascript - JavaScript 中的动画问题

转载 作者:太空宇宙 更新时间:2023-11-04 00:56:51 25 4
gpt4 key购买 nike

我正在尝试用 JavaScript 完成动画,但一切都运行良好,除了最后一个阶段,我已经输入了内部框向左移动的代码,但它仍然上升,开箱即用.请帮助指出出了什么问题。问题出在最后一个函数 moveleftagain(); 上。但是我已经明确指示它向左走,它仍然向左走。请帮忙。

var ctr = 0;
var ctr2 = 0;
var inner = document.getElementById('in');
var t = setInterval(moveright, 10);

function moveright() {
if (ctr == 150) {
clearInterval(t);
} else {
ctr += 1;
inner.style.left = ctr + "px";
}
}
var t2 = setInterval(movedown, 10);

function movedown() {
if (ctr == 150) {
if (ctr2 == 150) {
clearInterval(t2);
} else {
ctr2 += 1;
inner.style.top = ctr2 + "px";
}
}
}
var t3 = setInterval(moveleft, 10);

function moveleft() {
if (ctr2 == 150) {
if (ctr == 0) {
clearInterval(t3);
} else {
ctr -= 1;
inner.style.left = ctr + "px";
}
}
}
var t4 = setInterval(moveup, 10);
var check = false;
var checkdiag = false;

function moveup() {
if (ctr == 0) {
if (ctr2 == 0) {
clearInterval(t4);
check = true;
} else {
ctr2 -= 1;
inner.style.top = ctr2 + "px";
}
}
}
var t5 = setInterval(movediagonalright, 10);

function movediagonalright() {
if (check == true) {
if (ctr == 150) {
clearInterval(t5);
checkdiag = true;
} else {
ctr += 1;
ctr2 += 1;
inner.style.left = ctr + "px";
inner.style.top = ctr2 + "px";
}
}
}
var t6 = setInterval(moveleft2, 10);
var checkdiagleft = false;

function moveleft2() {
if (checkdiag == true) {
if (ctr == 0) {
clearInterval(t6);
checkdiagleft = true;
} else {
ctr -= 1;
inner.style.left = ctr + "px";
}
}
}

var t7 = setInterval(movediagonalrightup, 10);
var checkdiagright = false;

function movediagonalrightup() {
if (checkdiagleft == true) {
if (ctr == 150) {
clearInterval(t5);
checkdiagright = true;
} else {
ctr += 1;
ctr2 -= 1;
inner.style.left = ctr + "px";
inner.style.top = ctr2 + "px";
}
}
}
var t7 = setInterval(moveleftagain, 10);

function moveleftagain() {
if (checkdiagright == true) {
if (ctr == 0) {
clearInterval(t7);
} else {
ctr -= 1;
inner.style.left = ctr + "px";
}
}
}
<html>

<head>
<title></title>
<style type="text/css">
#out {
height: 200px;
width: 200px;
background: #FF0000;
position: relative;
}

#in {
height: 50px;
width: 50px;
background: #000000;
position: absolute;
}
</style>
</head>

<body>

<div id="out">
<div id="in"></div>
</div>

</body>

</html>

最佳答案

movediagonalrightup 继续运行并在 moveleftagain 中尝试将 ctr 递减 1

您可能想在movediagonalrightupclearInterval(t7);。还有这个 var t7 = setInterval(moveleftagain, 10); 应该是 var t8 = setInterval(moveleftagain, 10);你有两个 t7

这是一个有效的 fiddle所以你可以玩

关于javascript - JavaScript 中的动画问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54494652/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com