jquery animate with percenteges

一曲冷凌霜 提交于 2019-12-07 13:02:48

问题


Is there any way i can animate a div using percentages with jquery?

I've tried these:

$('.box1').animate({width:($(".wrapper").width()*.100)},"fast");})
$('.box1').animate({width:'100%'},"fast");})
$('.box1').animate({width:100%;})

none of the above work...

any ideas?

UPDATE

var p_e_h= 1.0;
var p_e_w= 1.0;
 $('.box1').animate({width:($(".wrapper").width()* p_e_w)},"fast");
       $('.box1').animate({height:($(".wrapper").height()* p_e_h)},"fast");

width works, but height does not. Unless i click the button again.


回答1:


var fiftyPrct = 0.5, //50%
    hundredPrct = 1; //100%

$('.box1').animate({ width: ($('.wrapper').width() * fiftyPrct) }, 'fast');
$('.box2').animate({ width: ($('.wrapper').width() * hundredPrct) }, 'fast');

Should work fine.

Update

Since you are doing 2 different .animate calls, the animations are queued and executed sequentially. Just do them with the same animation call:

var p_e_h= 1.0;
var p_e_w= 1.0;

$('.box1').animate({
    width: ($(".wrapper").width() * p_e_w),
    height:($(".wrapper").height() * p_e_h)
},"fast");

Example of it working: jsFiddle



来源:https://stackoverflow.com/questions/8299608/jquery-animate-with-percenteges

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!