Slider 'jumps' on mouseenter event

Slider 'jumps' on mouseenter event

Hi, I am trying to make a slider and I can not manage to overcome one problem. On mouseenter event sometimes it happens that my slider 'jumps' left or right and then returns to correct state. Here is the code.

  1.  . 
  2. $(function(){
  3. //INIT//
  4.     $('.slider').width($('.slider a').length * 240);
  5.     var $carouselDesktop    = $(".carousel-desktop"),
  6.         $slider    = $(".slider"),
  7.         carouselWidth = $(window).width(),
  8.         sliderWidth = $slider.width(),
  9.         difference = sliderWidth - carouselWidth,
  10.         ratio = difference/carouselWidth,
  11.         margin = 0;

  12.         console.log(sliderWidth);
  13.     $(window).resize(function(){
  14.         carouselWidth = $(window).width();
  15.         sliderWidth = $slider.width();
  16.         difference = sliderWidth - carouselWidth;
  17.         ratio = difference/carouselWidth;
  18.         $slider.css({marginLeft: -difference});

  19.     })

  20. var margin = 0;
  21. var mouseIn =false;

  22. $carouselDesktop.mouseenter(function(event){
  23.     margin = Math.round(event.pageX * (ratio+0.005));
  24.     $slider.stop(true, false).animate({marginLeft: -margin}, 200,function(){mouseIn = true; });
  25. });
  26. $carouselDesktop.mousemove(function(event){
  27.     margin = Math.round(event.pageX * (ratio+0.005));
  28.     if(margin < difference && mouseIn){
  29.         console.log("w mousemove");
  30.         $slider.css({marginLeft: -margin});
  31.     }
  32.         console.log(margin);
  33.     });
  34. $carouselDesktop.mouseleave(function(){
  35.     mouseIn = false;
  36. });
  37. /////////////////////////////////////////DARKEN ON HOVER /////////////////////////////////////////////////
  38. $('.darken').hover(function() {
  39.     $(this).find('img').fadeTo(10, 0.5);
  40. }, function() {
  41.     $(this).find('img').fadeTo(10, 1);
  42. });

  43. });