How can I both move a div and resize it with animation effect at JQuery?

爱⌒轻易说出口 提交于 2019-12-14 00:40:55

问题


How can I both move a div and resize it with animation effect at JQuery?

i.e. div will be at the middle of screen and it will go to the left side and will have %50 of its height and %50 of its width after it completed its move.(It will have for example %75 of its size at the middle of its way)

EDIT: I tried that:

$(".block").animate({"left": "-=100px", "height": "-=30%", "width":"-=30%" }, 1000);

However it disappears?

EDIT2: This works:

$(".block").animate({"left": "-=100px", "height": "30%", "width":"30%" }, 1000);

However it makes my square more bigger, I try =-30% but doesn't work. What should I do getting smaller instead of getting bigger?


回答1:


Seems like the last line is setting the height and width css properties to 30%. You expect it to be 30% of the previous sizes, but it actually is 30% of the nearest sized parent of the block, which seems to be larger than the previous size.

I don't think there is a simple way to resize to a given percentage of the previous size (like a special syntax in animate or so). I assume you have to code a javascript function that calculates the new width from the old one.

Given that you only want to resize one element, I strongly suggest that you use the element ID instead of its class. Then the following line should do what you want:

$("#block").animate({"left": "-=100px", "height": $("#block").height()*0.5, "width":$("#block").width()*0.5 }, 1000);



回答2:


You can use the animate function of jQuery. You can check this link:

  http://api.jquery.com/animate/

In the above link, examples shows you all the things which you want(like change the width with animation). Please check the example.



来源:https://stackoverflow.com/questions/7077085/how-can-i-both-move-a-div-and-resize-it-with-animation-effect-at-jquery

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