[英]How can I set this variable(number) to percent?
I have div(box2) which has 0 width. 我有div(box2)具有0宽度。
<div class="box">
<div class="box2"></div>
</div>
I would like to set its width in percent with javascript/jQuery. 我想使用javascript / jQuery将其宽度设置为百分比。 Since I have that function below:
由于我具有以下功能:
var a = 50;
var b = 100;
function percentage(x,y){
return a * 100 / b;
}
var box2Width = percentage(a,b);
$('.box2').animate({
width: box2Width
},400);
I get the percentage of box2 width(50) but in numbers as you can see.. How can I convert this in percent so that box2 can get 50% percent width? 我得到box2宽度(50)的百分比,但是可以看到数字。.如何将其转换为百分比,以便box2可以得到50%的宽度?
Here it is: http://jsfiddle.net/Rd9rE/ 它在这里: http : //jsfiddle.net/Rd9rE/
Thanx! 感谢名单!
You are accessing a and b in percentage instead of parameter passed, change it otherwise it would cause unexpected result later. 您正在访问百分比的a和b而不是传递的参数,请对其进行更改,否则稍后将导致意外结果。
Or Return with percentage sign from function Here is Demo : 或者从函数中返回带有百分号的符号这里是Demo :
function percentage(x, y) {
return (x * 100 / y)+'%';
}
$('.box2').animate({
width: percentage(50, 100)
}, 400);
You just need to append the %
symbol to the value: 您只需要在值后附加
%
符号即可:
width: box2Width + '%'
Just add the %
只需添加
%
$('.box2').animate({
width: box2Width + '%'
},400);
as you are getting the percentage value by function 当您按功能获取百分比值时
var box2Width = percentage(a,b);
You just need a %
sign at the end of the value,so just add %
any where in the result or function 您只需要在值的末尾添加一个
%
符号,即可在结果或函数中的任意位置添加%
$('.box2').animate({
width: box2Width+'%'
},400);
OR 要么
function percentage(x,y){
return (a * 100 / b)+'%';
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.