【问题标题】:jQuery Knob update value with animatejQuery Knob 使用动画更新值
【发布时间】:2013-11-14 21:28:01
【问题描述】:

我正在尝试使用 jQuery Knob 构建时钟。 我的时钟正常工作 (http://jsfiddle.net/Misiu/9Whck/1/),但现在我正在尝试为其添加一些额外功能。
一开始我想将所有旋钮设置为 0,然后使用 animate 我想将它们的值设置为当前时间,然后开始正常的计时器更新。

我的代码如下所示(此处为演示:http://jsfiddle.net/Misiu/cvQED/81/):

$.when(
$('.h').animate({
    value: h
}, {
    duration: 1000,
    easing: 'swing',
    progress: function () {
        $(this).val(Math.round(this.value)).trigger('change');
    }
}),
$('.m').animate({
    value: m
}, {
    duration: 1000,
    easing: 'swing',
    progress: function () {
        $(this).val(Math.round(this.value)).trigger('change');
    }
}),
$('.s').animate({
    value: s
}, {
    duration: 1000,
    easing: 'swing',
    progress: function () {
        $(this).val(Math.round(this.value)).trigger('change');
    }
})).then(function () {
    myDelay();
});

function myDelay() {
    var d = new Date(),
        s = d.getSeconds(),
        m = d.getMinutes(),
        h = d.getHours();
    $('.h').val(h).trigger("change");
    $('.m').val(m).trigger("change");
    $('.s').val(s).trigger("change");
    setTimeout(myDelay, 1000)
}

when 中,我必须分别为每个旋钮调用 animate,但我想使用 data-targetValue 并在其中只有一个动画。

这个可以吗?

【问题讨论】:

    标签: javascript jquery jquery-knob


    【解决方案1】:

    如果你想使用 data-targetValue 你需要像这样改变你的 js

    $('.h').data('targetValue', h);//$('.h').attr('targetValue', h);
    $('.m').data('targetValue', m);
    $('.s').data('targetValue', s);    
    //...    
    $.when(
    $('.knob').each(function(){//each .knob
        $(this).animate({//animate to data targetValue
            value: $(this).data('targetValue')
        }, {
            duration: 1000,
            easing: 'swing',
            progress: function () {
                $(this).val(Math.round(this.value)).trigger('change')
            }
        });
    })
    ).then(function () {
        myDelay();
    });    
    

    http://jsfiddle.net/cvQED/83/
    或者没有 .each

    $.when(
    $('.knob').animate({
        value: 100
    }, {
        duration: 1000,
        easing: 'swing',
        progress: function () {
            $(this).val(Math.round(this.value/100*$(this).data('targetValue'))).trigger('change')
        }
    })
    ).then(function () {
        myDelay();
    });    
    

    http://jsfiddle.net/cvQED/84/

    【讨论】:

    • 我都在尝试,但没有任何运气,非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-01
    • 2013-05-28
    • 1970-01-01
    相关资源
    最近更新 更多