Search code examples
javascriptjqueryjquery-knob

jQuery Knob hover animation


I want to animate the Knob circle that it fills up on hovering. I'm new to Knob so i have no idea where my error is or if i even have the right direction. Right now it does not even show a circle :(

Basically i just want to have a circle around an icon that fills up on hovering. Maybe i can achieve that easier?

This is the sollution of it plus a little fix that i will start and stop at the right values, so you can interupt the animation without breaking it

the HTML:

<input type="text" value="0" id="circle" />

the Javascript:

$(function() {
$('.circle').knob({
    min: '0',
    max: '100',
    readOnly: true,
    displayInput: false
});

$('.circle').parent().hover( function() {console.log("hover");
                $({ value: $('.circle').val() }).animate(
                    { value: 100 }, 
                    {   duration: 300,
                        easing: 'swing',
                        progress: function() {
                          $('.circle').val(Math.round(this.value)).trigger('change');
                        }
                     });
             }, function() {
                $({ value: $('.circle').val() }).animate(
                    { value: 0 }, 
                    {
                        duration: 300,
                        easing: 'swing',
                        progress: function() {
                            $('.circle').val(Math.round(this.value)).trigger('change');
                        }
                     });
                });
});

Here is the JSFiddle


Solution

  • you need to change the hover handler to the parent of #circle or change displayInput to true

    $(function() {
    $('#circle').knob({
        min: '0',
        max: '100',
        readOnly: true,
        displayInput: false
    });
    //$('#circle').parent() is the new div that contains the input and the canvas
    $('#circle').parent().hover( function() {
                    $({ value: 0 }).animate(
                        { value: 100 }, 
                        {   duration: 1000,
                            easing: 'swing',
                            progress: function() {
                              $('#circle').val(Math.round(this.value)).trigger('change');
                            }
                         });
                 }, function() {
                    $({ value: 100 }).animate(
                        { value: 0 }, 
                        {
                            duration: 1000,
                            easing: 'swing',
                            progress: function() {
                                $('#circle').val(Math.round(this.value)).trigger('change');
                            }
                         });
                    });
    });//you need to close with ');'    
    

    you need to include the knob.js in the fiddle or else you get a '404 Not Found' error and include jquery or else you get this error 'Uncaught ReferenceError: $ is not defined'
    http://jsfiddle.net/dWsuP/1/