这是我想运行的代码片段,用于为具有相同功能的任何css属性设置动画。
.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);
}
调用此功能 -
animateDimension($('.box'),'width',0, function(){
$('.box').hide();
});
盒子在一秒钟后隐藏,但宽度不会改变。有人可以帮我吗???
答案 0 :(得分:3)
您的代码应为:
function animateDimension($el, dimension, value, callback) {
var properties = {};
properties[dimension] = value;
callback = callback || $.noop;
$el.animate(properties, 1000, callback);
}
http://jsfiddle.net/7afFH/(对于jsfiddle来说是@vjs!)
答案 1 :(得分:0)
另一种改变宽度的方法是
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'))
});