简体   繁体   English

jQuery .animate命令的问题

[英]Issue with jQuery .animate command

I was using the animate command for jQuery and for some reason this doesn't work: 我正在为jQuery使用animate命令,由于某种原因,这不起作用:

$('#my_div_id').animate({left:'calc(50% + 50px)'}, 1000);

Is there any way to get around this? 有什么办法可以解决这个问题?

I know the code works if i put '50%', or '50px', but I want my div to animate to 50% of the screen + 50px. 我知道如果我输入“ 50%”或“ 50px”,代码会起作用,但是我希望我的div动画到屏幕的50%+ 50px。

Thanks in advance! 提前致谢!

It would seem that calc cannot be animated, but fortunately it's easy for you to calculate 50% of the width of the screen already since you have the power of JavaScript. 似乎无法动画化calc ,但幸运的是,由于您具有JavaScript的功能,因此您已经很容易计算出屏幕宽度的50%。

$("#my_div_id").animate({left: $("body").width() / 2 + 50 + "px"}, 1000);

This assumes that #my_div_id is positioned relative to the body (ie no relative position ancestory -- otherwise you would have to use that). 假定#my_div_id相对于body定位(即没有相对位置祖先-否则您将不得不使用该位置)。

Basically nothing new, but using the explicit request of the asker, to set it to 50% of the screen width: 基本上没有什么新鲜的,但是使用询问者的明确请求,将其设置为屏幕宽度的50%:

var width = screen.width / 2 + 50;
$('#my_div_id').animate({left: width + "px"}, 1000);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM