title: Rotate image follow mouse jquery with smooth transition
body: <p>I am trying to make an image rotate when you hover over certain items on the page. It uses the solution provided here (with edits for my purposes): <a href="https://stackoverflow.com/a/10235298">https://stackoverflow.com/a/10235298</a>.  You can see an example of what I am trying to achieve here: <a href="https://jsfiddle.net/t100gq25/" rel="nofollow noreferrer">https://jsfiddle.net/t100gq25/</a></p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>$(function () {
    var img = $('.arrow');
  
    if (img.length &gt; 0) {
      var offset = img.offset();
  
      $('.animation-trigger').mouseenter(function (event) {
        var element = $(this);
        var elementPosition = element.offset();
        var elementX = elementPosition.left + (element.width() / 2);
        var elementY = elementPosition.top + (element.height() / 2);
        var imgX = offset.left + (img.width() / 2);
        var imgY = offset.top + (img.height() / 2);
        var radians = Math.atan2(elementX - imgX, elementY - imgY);
        var degrees = (radians * (180 / Math.PI) * -1) + 90;
  
  
        img.css('-moz-transform', 'rotate(' + degrees + 'deg)')
          .css('-webkit-transform', 'rotate(' + degrees + 'deg)')
          .css('-o-transform', 'rotate(' + degrees + 'deg)')
          .css('-ms-transform', 'rotate(' + degrees + 'deg)');
      });
    }
  });</code></pre>
  <pre class="snippet-code-css lang-css prettyprint-override"><code>body {
    padding-top: 150px;
  }
  
  .container {
    height: 500px;
  }
  
  .menu-1,
  .menu-2,
  .menu-3,
  .menu-4 {
    position: absolute;
    z-index: 99
  }
  
  .menu-1 {
    top: 20%;
    left: 20%;
  }
  
  .menu-2 {
    top: 20%;
    right: 20%;
  }
  
  .menu-3 {
    bottom: 20%;
    left: 20%;
  }
  
  .menu-4 {
    bottom: 20%;
    right: 20%;
  }
  
  .arrow {
    position: absolute;
    top: 50%;
    left: 50%;
    margin-top: -100px;
    margin-left: -100px;
    height: 200px;
    width: 200px;
  
    -webkit-transition: all 400ms cubic-bezier(0.455, 0.03, 0.515, 0.955);
    -moz-transition: all 400ms cubic-bezier(0.455, 0.03, 0.515, 0.955);
    -o-transition: all 400ms cubic-bezier(0.455, 0.03, 0.515, 0.955);
    transition: all 400ms cubic-bezier(0.455, 0.03, 0.515, 0.955);
  }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"&gt;&lt;/script&gt;
  &lt;a href="#" class="menu-1"&gt;
      &lt;img src="http://placehold.it/140x35&amp;text=4" class="animation-trigger"&gt;
  &lt;/a&gt;
  
  &lt;a href="#" class="menu-2"&gt;
      &lt;img src="http://placehold.it/140x35&amp;text=1" class="animation-trigger"&gt;
  &lt;/a&gt;
  
  &lt;a href="#" class="menu-3"&gt;
      &lt;img src="http://placehold.it/140x35&amp;text=3" class="animation-trigger"&gt;
  &lt;/a&gt;
  
  &lt;a href="#" class="menu-4"&gt;
      &lt;img src="http://placehold.it/140x35&amp;text=2" class="animation-trigger"&gt;
  &lt;/a&gt;
  
  &lt;img src="https://image.freepik.com/free-icon/arrow-full-shape-pointing-to-right-direction_318-32063.png" class="arrow"&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>It works great! However if you hover over link 1, the arrow will turn to it and then you decide to hover over link 4 and the arrow will again point to it. However it goes all the way around (clockwise) instead of taking the short route of just spinning back(anti-clockwise).</p>
  
  <p>I've had a go at a few attempts but none have worked and if I come up with an idea that could work it's extremely long winded. I'm struggling to work out the best way to approach this so any help would be very much appreciated. </p>
  
  <p>Please note the jsfiddle is a quick mock up example of what I am trying to achieve. Unfortunately I cannot share the actual source code due to client confidentiality. Any solutions provided I will apply to the final site.</p>
  
  <p>Many thanks:)</p>