简体   繁体   English

jQuery和Knob动画问题

[英]jQuery and Knob animate issue

I am trying to animate a number so that it rolls into the number when the page loads. 我试图动画一个数字,以便它在页面加载时滚动到数字。 I am using another library to display a dial ( http://anthonyterrien.com/knob/ ). 我正在使用另一个库来显示一个表盘( http://anthonyterrien.com/knob/ )。 The issue I am having is that the number seems to be different every time I run it. 我遇到的问题是,每次运行它时,数字似乎都不同。 It should be a consistent number ending on 19420. However sometimes it is lower and there doesn't seem to be any particular pattern. 它应该是一个以19420结尾的一致数字。然而,有时它会更低,似乎没有任何特定的模式。

My JS code looks like this: 我的JS代码如下所示:

$(function() {
    $('#dial').knob({
        min: '0',
        max: '25000',
        readOnly: true
    });

    $({
        value: 0
    }).animate({
        value: 19420 
    }, {
        duration: 950,
        easing: 'swing',
        step: function() {
            $('#dial').val(Math.round(this.value)).trigger('change');
        }
    });
});

The fiddle can be found here: http://jsfiddle.net/ND5Sf/ 小提琴可以在这里找到: http//jsfiddle.net/ND5Sf/

What have I done wrong or is there anything I've missed out? 我做错了什么或者我错过了什么? If not, are these 2 libraries not compatible? 如果没有,这两个库是不兼容的吗?

The issue is because you are using step function instead of progress . 问题是因为您使用的是step功能而不是progress

Step: 步:

A function to be called for each animated property of each animated element. 为每个动画元素的每个动画属性调用的函数。 This function provides an opportunity to modify the Tween object to change the value of the property before it is set. 此函数提供了修改Tween对象的机会,以便在设置之前更改属性的值。

Progress: 进展:

A function to be called after each step of the animation, only once per animated element regardless of the number of animated properties. 在动画的每个步骤之后调用的函数,每个动画元素只调用一次,而不管动画属性的数量。 (version added: 1.8) (版本增加:1.8)

Code: 码:

$(function () {
    $('#dial').knob({
        min: '0',
        max: '25000',
        readOnly: true
    });

    $({
        value: 0
    }).animate({
        value: 19420
    }, {
        duration: 950,
        easing: 'swing',
        progress: function () {
            $('#dial').val(Math.round(this.value)).trigger('change');
        }
    });
});

Docs: http://api.jquery.com/animate/ 文档: http//api.jquery.com/animate/

Demo: http://jsfiddle.net/IrvinDominin/JW2gP/ 演示: http//jsfiddle.net/IrvinDominin/JW2gP/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM