Reputation: 34158
Hello everybody i have variable(widthPercent) where i store percent for example: 67.33%
when i trying to change width with jquery animation it does not works :
$(this).animate({
width: widthPercent,
}, 2500);
});
but changing width with css working good:
$(this).css('width', widthPercent);
does anyone have idea what is my problem?
Upvotes: 1
Views: 500
Reputation: 3862
Is this what you mean:
jQuery:
$('#foo').click(function(){
var widthPercent = '66%';
$(this).animate({
'width': widthPercent,
}, 2500);
});
CSS:
#foo{
width:300px;
background:#ff0000;
height:100px;
}
Upvotes: 0
Reputation: 2134
http://api.jquery.com/animate/
var widthPercent = "35%";
$(this).animate({
width: "'"+widthPercent+"'" // YOU ALSO HAD AN EXTRA COMMA HERE MEANING IT WAS EXPECTING ANOTHER ARGUMENT -- extra quotes may or may not be necessary depending on what your actual variable looks like
}, 2500);
});
also be sure to use quotes when using a % sign otherwise it may be treated as an inline mod operation
Upvotes: 1
Reputation: 1556
maybe you need to quote the widthPercent
this worked for me
$(document).ready(
function(){
var widthPercent = "35%";
$("#btn").click(
function(){
$(this).animate({
width: widthPercent,
}, 2500);
});
}
);
the problem should be in your widthPercent definition.
Upvotes: 2
Reputation: 6131
You need to add a '%' character to the width value, like this:
$(this).css('width', widthPercent + '%')
Here's a jsfiddle illustrating it.
Upvotes: 0