Rotate image follow mouse jquery with smooth transition

Rotate image follow mouse jquery with smooth transition

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): http://stackoverflow.com/a/10235298.  You can see an example of what I am trying to achieve here: https://jsfiddle.net/t100gq25/

HTML:
  1. <div class="container">
  2.       <a href="#" class="menu-1">
  3.         <img src="http://placehold.it/140x35&text=4" class="animation-trigger">
  4.       </a>

  5.       <a href="#" class="menu-2">
  6.         <img src="http://placehold.it/140x35&text=1" class="animation-trigger">
  7.       </a>

  8.       <a href="#" class="menu-3">
  9.         <img src="http://placehold.it/140x35&text=3" class="animation-trigger">
  10.       </a>

  11.       <a href="#" class="menu-4">
  12.         <img src="http://placehold.it/140x35&text=2" class="animation-trigger">
  13.       </a>

  14.       <img src="https://image.freepik.com/free-icon/arrow-full-shape-pointing-to-right-direction_318-32063.png" class="arrow">
  15. </div>
JavaScript (dependent on jQuery):

  1. $(function () {
  2.   var img = $('.arrow');

  3.   if (img.length > 0) {
  4.     var offset = img.offset();

  5.     $('.animation-trigger').mouseenter(function (event) {
  6.       var element = $(this);
  7.       var elementPosition = element.offset();
  8.       var elementX = elementPosition.left + (element.width() / 2);
  9.       var elementY = elementPosition.top + (element.height() / 2);
  10.       var imgX = offset.left + (img.width() / 2);
  11.       var imgY = offset.top + (img.height() / 2);
  12.       var radians = Math.atan2(elementX - imgX, elementY - imgY);
  13.       var degrees = (radians * (180 / Math.PI) * -1) + 90;


  14.       img.css('-moz-transform', 'rotate(' + degrees + 'deg)')
  15.         .css('-webkit-transform', 'rotate(' + degrees + 'deg)')
  16.         .css('-o-transform', 'rotate(' + degrees + 'deg)')
  17.         .css('-ms-transform', 'rotate(' + degrees + 'deg)');
  18.     });
  19.   }
  20. });
CSS:
  1. body {
  2.   padding-top: 150px;
  3. }

  4. .container {
  5.   height: 500px;
  6. }

  7. .menu-1,
  8. .menu-2,
  9. .menu-3,
  10. .menu-4 {
  11.   position: absolute;
  12.   z-index: 99
  13. }

  14. .menu-1 {
  15.   top: 20%;
  16.   left: 20%;
  17. }

  18. .menu-2 {
  19.   top: 20%;
  20.   right: 20%;
  21. }

  22. .menu-3 {
  23.   bottom: 20%;
  24.   left: 20%;
  25. }

  26. .menu-4 {
  27.   bottom: 20%;
  28.   right: 20%;
  29. }

  30. .arrow {
  31.   position: absolute;
  32.   top: 50%;
  33.   left: 50%;
  34.   margin-top: -100px;
  35.   margin-left: -100px;
  36.   height: 200px;
  37.   width: 200px;

  38.   -webkit-transition: all 400ms cubic-bezier(0.455, 0.03, 0.515, 0.955);
  39.   -moz-transition: all 400ms cubic-bezier(0.455, 0.03, 0.515, 0.955);
  40.   -o-transition: all 400ms cubic-bezier(0.455, 0.03, 0.515, 0.955);
  41.   transition: all 400ms cubic-bezier(0.455, 0.03, 0.515, 0.955);
  42. }
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).


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. 

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.

Many thanks:)