Dan Man
Dan Man

Reputation: 3

how do i add a custom animate duration for every time i call this function in jquery

$(document).ready(function(){   

  var el = $("#box");

  animate(el,300,function(){

  });


});


function animate(el,leftVal,callback){

el.animate({
    left : leftVal
},{
    duration : 2000,
    queue    : false,
    easing   : "swing",
    complete : function(){
        //alert("finished");
        if(callback){
            callback();
        }
    }
});

}

Upvotes: 0

Views: 174

Answers (1)

Purag
Purag

Reputation: 17061

Add a duration parameter to your function:

function animate(el, leftVal, callback, duration) {

Then call the parameter in the duration part of the function:

{
    duration: duration,
    ...
}

When you're calling the function, define the last parameter (which is now the duration):

$(document).ready(function() {
    var el = $("#box");
    animate(el, 300, function() {
    },1000); // 1 second duration (1000 milliseconds)
});

Upvotes: 1

Related Questions