gpt4 book ai didi

JavaScript 振荡

转载 作者:行者123 更新时间:2023-11-28 20:26:41 25 4
gpt4 key购买 nike

我有以下动画:

<!DOCTYPE HTML>
<html>
<head>
<style>
.example_path {
position: relative;
overflow: hidden;
width: 530px;
height: 30px;
border: 3px solid #000;
}

.example_path .example_block {
position: absolute;
background-color: blue;
width: 30px;
height: 20px;
padding-top: 10px;
text-align: center;
color: #fff;
font-size: 10px;
white-space: nowrap;
}
</style>
<script>
function move(elem) {

var left = 0

function frame() {

left+=10 // update parameters

elem.style.left = left + 'mm' // show frame

if (left == 10000) // check finish condition
clearInterval(id)
}

var id = setInterval(frame, 1) // draw every 1ms
}
</script>
</head>

<body>
<div onclick="move(this.children[0])" class="example_path">
<div class="example_block"></div>
</div>
</body>
</html>

正如你所看到的,如果蓝色 block 穿过矩形,它就会移出矩形。我如何让蓝色 block 围绕矩形边框来回摆动,并始终保持速度恒定......

(在我的例子中,速度为 10 m/s 又名 10 mm/ms)

最佳答案

您需要将代码更新为:Here is working JSfiddle

function move(elem) {

var left = 0
var fwdMove = true;

function frame() {
if (left < 0) {
fwdMove = true;
} else if (left > 520) {
fwdMove = false;
}

fwdMove?left += 10:left -= 10
elem.style.left = left + 'px' // show frame
}
var id = setInterval(frame, 1) // draw every 1ms
}

关于JavaScript 振荡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17252683/

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