سؤال

This is the peice of code that I want to run to animate any css property with the same function.

.box {
    width: 100px;
    height: 100px;
    background-color: grey;
}

<div class='box'></div>

function animateDimension(el, dimension, value, callback) {
    var $el = $(el);
    callback = callback || function() {};
    $el.animate({
    dimension : value,
    },1000, callback);
}

Calling this function -

animateDimension($('.box'),'width',0, function(){
    $('.box').hide();
});

The box hides after one second but the width does not change. Can someone help me out???

هل كانت مفيدة؟

المحلول

Your code should be:

function animateDimension($el, dimension, value, callback) {
    var properties = {};
    properties[dimension] = value;
    callback = callback || $.noop;
    $el.animate(properties, 1000, callback);
}

http://jsfiddle.net/7afFH/ (Thx @vjs for jsfiddle!)

نصائح أخرى

Another way to change width is

function animateDimension(el, dimension, value, callback) { var $el = $(el); callback = callback || function() {}; $el.animate({ width : value, },1000, callback); }

animateDimension($('.box'),'width','0px', function(){ $('.box').hide(); alert($('.box').css('width')) });

Here is demo http://jsfiddle.net/SiddharthShah/M3Kzh/

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top