javascript - Bootstrap 进度的 Jquery 动画宽度百分比不起作用

标签 javascript jquery html css twitter-bootstrap

实际上我在元素中有 2 个表单,如果用户完成一个表单并单击提交,他将被带到下一个表单,所以我使用顶部的进度条显示表单进度。

如果用户完成第一个表单,进度会将宽度设为 50%,如果用户完成第二个表单,进度会将宽度设为 100%。

问题是 50% 的宽度动画工作正常,但是 100% 的宽度动画会抛出一些更高的范围值,如 315.***%,这使得进度条无法正确动画。

下面是 fiddle 的链接,其中 button1(50%) 用于第一种形式,button2(100%) 用于第二种形式。

https://jsfiddle.net/mohamedmusthafac/yeaht53q/

下面是 jquery 代码:-

$(document).ready(function(){
$("#but1").on("click", function(){
$(".progress-bar").stop().animate({width:'50%'}, 1500);
});
$("#but2").on("click", function(){
$(".progress-bar").stop().animate({width:'100%'},1500).stop();
});
});

最佳答案

您已经给出了解决方案,但看起来行为可能是 jQuery 动画函数的行为方式。我想我会提供一个答案来解释这种行为。我不确定这是一个错误还是预期的行为。对我来说,它看起来更像是一个错误。

当您调用 animate 函数时,它会在内部调用一个 adjustCSS 函数。调用 adjustCSS 的调用栈很长 (animate() -> dequeue() -> doAnimation() -> Animation() -> jquery.map() -> createTween() -> Animation.Tweeners.*0 > adjustCSS ()) 以防万一你对流程感兴趣。

在 adjustCSS 中,代码执行路径在进度条为 0% 时采用不同的一组路径,在进度条为 50% 时采用不同的一组路径。

如果你看到 adjustCSS

function adjustCSS( elem, prop, valueParts, tween ) {
var adjusted,
    scale = 1,
    maxIterations = 20,
    currentValue = tween ?
        function() {
            return tween.cur();
        } :
        function() {
            return jQuery.css( elem, prop, "" );
        },
    initial = currentValue(),
    unit = valueParts && valueParts[ 3 ] || ( jQuery.cssNumber[ prop ] ? "" : "px" ),

    // Starting value computation is required for potential unit mismatches
    initialInUnit = ( jQuery.cssNumber[ prop ] || unit !== "px" && +initial ) &&
        rcssNum.exec( jQuery.css( elem, prop ) );

if ( initialInUnit && initialInUnit[ 3 ] !== unit ) {

    // Trust units reported by jQuery.css
    unit = unit || initialInUnit[ 3 ];

    // Make sure we update the tween properties later on
    valueParts = valueParts || [];

    // Iteratively approximate from a nonzero starting point
    initialInUnit = +initial || 1;

    do {

        // If previous iteration zeroed out, double until we get *something*.
        // Use string for doubling so we don't accidentally see scale as unchanged below
        scale = scale || ".5";

        // Adjust and apply
        initialInUnit = initialInUnit / scale;
        jQuery.style( elem, prop, initialInUnit + unit );

    // Update scale, tolerating zero or NaN from tween.cur()
    // Break the loop if scale is unchanged or perfect, or if we've just had enough.
    } while (
        scale !== ( scale = currentValue() / initial ) && scale !== 1 && --maxIterations
    );
}

if ( valueParts ) {
    initialInUnit = +initialInUnit || +initial || 0;

    // Apply relative offset (+=/-=) if specified
    adjusted = valueParts[ 1 ] ?
        initialInUnit + ( valueParts[ 1 ] + 1 ) * valueParts[ 2 ] :
        +valueParts[ 2 ];
    if ( tween ) {
        tween.unit = unit;
        tween.start = initialInUnit;
        tween.end = adjusted;
    }
}
return adjusted;
}

点击第一个按钮时,initialInUnit为0,不通过if条件

if ( initialInUnit && initialInUnit[ 3 ] !== unit ) {

直接跳到

if ( valueParts ) {

但是当您第二次单击该按钮时,它会通过上面的第一个 if 条件,因为进度条已经达到其 50% 的宽度。现在,使百分比达到 315% 或出现的任何数字的原因是奇怪的行为所在。

当它经过if条件时,有一个语句:

        jQuery.style( elem, prop, initialInUnit + unit );

这里 jQuery 将元素的 width 属性设置为 initialInUnit 中的值,这是进度条的宽度,以像素为单位,它附加了以 % 为单位的单位。因此,它不是将宽度从 50% 动画化到 100%,而是将宽度从 315% 动画化到 100%。 315% 可以根据进度条的宽度而改变。

事实上,您甚至不需要第二个按钮来复制行为,如果您第二次单击按钮 1,您可以看到它正在从 315%(或其他)到 50% 进行动画处理。

所以我不确定它是否是一个错误(虽然它看起来像一个)。 adjustedCSS 可能适用于其他条件。可能值得向 jQuery 团队提出一个问题,以检查 adjustCSS 函数中 initialInUnit 值分配的预期行为。

adjustCSS 函数是 jQuery 代码的一部分,如 here 所示

关于javascript - Bootstrap 进度的 Jquery 动画宽度百分比不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41996384/

相关文章:

javascript - 为什么 `style.webKitTransition` 和 `style.webkitTransform` 在 Chrome 中不再起作用?

jquery - Bootstrap 从祖 parent 那里继承网格/CSS 宽度选项

javascript - 如何在 django 1.6.5 中执行 JavaScript

javascript - 用于在页面上打印元素内容的跨浏览器解决方案是什么?

jquery - IE8 和 jQuery 选择器

javascript - 使用 Featherlight.js 禁用右键单击

javascript - Angular-gettext 不会更新代码中生成的字符串

javascript - 无法弄清楚我在 JavaScript 中的 if 语句有什么问题 [9 月 1 日更新 看来我的 for 循环运行了两次,我该如何解决?]

javascript - 所有选项卡均已显示,但我希望在任何给定时间仅显示一个选项卡

javascript - 如何在 Ember.js 中从 Mirage 获取数据?