gpt4 book ai didi

javascript - 使用javascript动态更新div的宽度

转载 作者:行者123 更新时间:2023-11-28 03:08:28 30 4
gpt4 key购买 nike

在一个骰子游戏中,我创建了一个简单的进度条,用来动态显示玩家的进度。这是函数:

function gameProgress(myScore) {
let myProgress = document.getElementById('progress');
let id = setInterval(frame, 50);

function frame() {
myProgress.style.width = myScore + 'px';
}
}

该函数接受游戏分数并应该将其反射(reflect)在进度条上。实际发生的是 div 的 style="width:"不断来回跳动显示 2px、7px、17px、22px、32px 等等。有什么办法可以解决这个问题?我希望元素仅显示最新更新的宽度而不是所有值。

最佳答案

如果您只更新一次宽度,则不必使用 setInterval。

function gameProgress(myScore) {
let myProgress = document.getElementById('progress');
prg.style.width = myScore + 'px'; // this should be a percentage of the actual total width of the div.

}

关于javascript - 使用javascript动态更新div的宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45839015/

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