Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First off, you need to use an easing animation that starts and ends with the same "angle". If you look at the different <a href="http://jqueryui.com/resources/demos/effect/easing.html" rel="nofollow">easing options</a>, <code>swing</code>, <code>easeInOutQuad</code> and <code>easeInOutSine</code> are some of the valid options.</p> <p>To calculate an approximation of the angle, you can look at the mail icon's current position and its next position (in the next animation frame). To get a good approximation you need to "manually" calculate the current and next position using the easing function. This also means you need to control the animation manually.</p> <p>Here's a code snippet, and you can also see it on <a href="http://jsfiddle.net/BjVHW/10/" rel="nofollow">JSFiddle</a>.</p> <pre><code>// Init dom elements var $start = $('#start'); var $end = $('#end'); var $mail = $('#mail'); // Get position coordinates var startPos = $start.offset(); var endPos = $end.offset(); // Angle calculation var getAngle = function(currX, currY, endX, endY) { var angle = Math.atan2(currX - endX, currY - endY) * (180 / Math.PI); if (angle &lt; 0) { angle = Math.abs(angle); } else { angle = 360 - angle; } return angle; }; // Animate var maxframe = 1000; $({frame: 0}).animate({frame: maxframe}, { easing: "linear", duration: 1000, // Executed each "animation" frame, so we rotate here. step: function() { var easing = $.easing.easeInOutQuad; var left = easing(0, this.frame, startPos.left, endPos.left - startPos.left, maxframe); var leftNext = easing(0, this.frame+1, startPos.left, endPos.left - startPos.left, maxframe); var top = startPos.top + (endPos.top - startPos.top) * this.frame / maxframe; var topNext = startPos.top + (endPos.top - startPos.top) * (this.frame + 1) / maxframe; var angle = getAngle(left, top, leftNext, topNext); $mail.offset({left: left, top: top}); $mail.css('transform', 'rotate(' + angle + 'deg)'); }, // Set the final position complete: function() { $mail.offset($end.offset()); $mail.css('transform', ''); } }); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload