gpt4 book ai didi

jquery - 如何在动画时查看元素的宽度?

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

我使用简单的动画对象:

$("#progress").animate({width:400},2000)

我也想这样看它的宽度

$("#loadingpro").html(pro+"%");

pro = Math.round(procent$("#progress").width()/4);

但是当我这样做时,它开始延迟整个 cpu,以至于根本不可能这样做。

有什么想法可以尝试以其他方式做到这一点吗?

最佳答案

你可以使用 setInterval() clearInterval() 。调整间隔以使其平滑。 ~20 微秒对我有用:

$(function() {
var timer = setInterval(function() {
$("#loadingpro").html(Math.round($("#progress").width()/4) + "%");
},20);
$("#progress").animate({width:400},2000, function() {
clearInterval(timer);
});
});​

jsFiddle example


.setInterval()是一个不错的选择,因为您可以使用 clearInterval() 停止它在 animate() 完成所有计算后回调。

关于jquery - 如何在动画时查看元素的宽度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3850552/

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