简体   繁体   English

以某个角度移动,jquery,animate()?

[英]Move at an angle, jquery, animate()?

is there a way to specify the angle when using 有没有办法在使用时指定角度

$(this).animate({'left': '+=30px'}, 1000);

I would like 我想要

$(this).animate({'48': '+=30px'}, 1000);

the 48 would be the angle 48是角度

You can specify both left and top property: 您可以指定lefttop属性:

$(this).animate({left: '+=30px', top: '+=30px'}, 1000);

In this case, the angle would be 45°. 在这种情况下,角度将是45°。

To specify a different angle, first calculate cos(α) and sin(α). 要指定不同的角度,首先计算cos(α)和sin(α)。 The cosinus specifies how far you have to go right, and the sinus how far to go up, to make a distance of 1 pixel in the specified angle. cosinus指定你必须向右走多远,以及鼻窦走多远,以指定角度形成1个像素的距离。 If you want to cover 30 pixels distance instead, multiply the sinus and cosinus results by 30. 如果要覆盖30像素距离,则将窦和余弦结果乘以30。

I just wanted to correct a mistake in 32bitkid's otherwise excellent example from the best answer by Yogu. 我只是想纠正一个错误的32bitkid的优秀例子来自Yogu的最佳答案。

If Math.PI is divded by -180 (instead of 180) it will result in the correct position for the div. 如果Math.PI除以-180(而不是180),则将得到div的正确位置。 90 degree angle was moving div down instead of up. 90度角向下移动而不是向上移动。

$(function() {
    var dist = 30;
    var angle = 90;
    var x = Math.cos(angle*Math.PI/-180) * dist;
    var y = Math.sin(angle*Math.PI/-180) * dist;
    $("div").animate({'left': '+='+x+'px', 'top': '+='+y+'px'}, 1000);      
})

Here's the updated fiddle: http://jsfiddle.net/9yL8hxnz/ 这是更新的小提琴: http//jsfiddle.net/9yL8hxnz/

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

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