简体   繁体   中英

jQuery Knob update value with animate

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?

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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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