jQuery Knob update value with animate

泄露秘密 提交于 2019-11-30 20:59:37

问题


I'm trying to build clock using jQuery Knob. My clock is working (http://jsfiddle.net/Misiu/9Whck/1/), but right now I'm trying to add some extras to it.
At beginning I want to have all knobs set to 0, then using animate I want to animate their value to current time and then start normal timer update.

My code looks like this (demo here: 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)
}

In when I must call animate for every knob separately, but I would like to use data-targetValue and to have only one animate inside when.

Can this be done?


回答1:


if you want to use data-targetValue you need to change your js like this

$('.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/
or without .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/



来源:https://stackoverflow.com/questions/19988768/jquery-knob-update-value-with-animate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!