【问题标题】:How to view width of element while animating?动画时如何查看元素的宽度?
【发布时间】:2010-10-03 16:20:13
【问题描述】:

我简单地使用动画对象:

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

我也想像那样查看它的宽度

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

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

但是当我这样做时,它开始延迟整个 cpu,以至于根本无法做到。

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

【问题讨论】:

    标签: jquery html width jquery-animate


    【解决方案1】:

    您可以使用 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() 是一个不错的选择,因为您可以在所有计算完成后在 animate() 回调中使用 clearInterval() 停止它。

    【讨论】:

    • @Drink86 - 不客气;)如果它解决了您的问题,请不要忘记接受我的回答(选票旁边的复选标记)。
    猜你喜欢
    • 2017-08-24
    • 2022-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-01
    • 2013-02-22
    相关资源
    最近更新 更多