$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>javascript - Move at an angle, jquery, animate()? - Stack Overflow|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Move at an angle, jquery, animate()? - Stack Overflow

matteradmin9PV0评论

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

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

Share Improve this question asked Jan 16, 2012 at 20:55 thelostthelost 7053 gold badges12 silver badges30 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 11

You can specify both left and top property:

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

In this case, the angle would be 45°.

To specify a different angle, first calculate cos(α) and 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. If you want to cover 30 pixels distance instead, multiply the sinus and cosinus results by 30.

I just wanted to correct a mistake in 32bitkid's otherwise excellent example from the best answer by Yogu.

If Math.PI is divded by -180 (instead of 180) it will result in the correct position for the div. 90 degree angle was moving div down instead of up.

$(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/9yL8hxnz/

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far