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.
- .
- $(function(){
- //INIT//
- $('.slider').width($('.slider a').length * 240);
- var $carouselDesktop = $(".carousel-desktop"),
- $slider = $(".slider"),
- carouselWidth = $(window).width(),
- sliderWidth = $slider.width(),
- difference = sliderWidth - carouselWidth,
- ratio = difference/carouselWidth,
- margin = 0;
-
- console.log(sliderWidth);
- $(window).resize(function(){
- carouselWidth = $(window).width();
- sliderWidth = $slider.width();
- difference = sliderWidth - carouselWidth;
- ratio = difference/carouselWidth;
- $slider.css({marginLeft: -difference});
-
- })
-
- var margin = 0;
- var mouseIn =false;
-
- $carouselDesktop.mouseenter(function(event){
- margin = Math.round(event.pageX * (ratio+0.005));
- $slider.stop(true, false).animate({marginLeft: -margin}, 200,function(){mouseIn = true; });
- });
- $carouselDesktop.mousemove(function(event){
- margin = Math.round(event.pageX * (ratio+0.005));
- if(margin < difference && mouseIn){
- console.log("w mousemove");
- $slider.css({marginLeft: -margin});
- }
- console.log(margin);
- });
- $carouselDesktop.mouseleave(function(){
- mouseIn = false;
- });
- /////////////////////////////////////////DARKEN ON HOVER /////////////////////////////////////////////////
- $('.darken').hover(function() {
- $(this).find('img').fadeTo(10, 0.5);
- }, function() {
- $(this).find('img').fadeTo(10, 1);
- });
-
- });
-
-