如何使jquery悬停事件反复触发

时间:2010-06-08 17:54:18

标签: jquery hover infinite-carousel

当我将鼠标悬停在下一个和上一个按钮上时,我想要移动一个无限的旋转木马。现在悬停只发射一次。我希望旋转木马在鼠标位于下一个或上一个按钮内时继续移动。

任何建议?

jQuery.fn.carousel = function(previous, next, options){
 var sliderList = jQuery(this).children()[0];

 if (sliderList) {
  var increment = jQuery(sliderList).children().outerWidth("true"),
  elmnts = jQuery(sliderList).children(),
  numElmts = elmnts.length,
  sizeFirstElmnt = increment,
  shownInViewport = Math.round(jQuery(this).width() / sizeFirstElmnt),
  firstElementOnViewPort = 1,
  isAnimating = false;

  for (i = 0; i < shownInViewport; i++) {
   jQuery(sliderList).css('width',(numElmts+shownInViewport)*increment + increment + "px");
   jQuery(sliderList).append(jQuery(elmnts[i]).clone());
  }

  jQuery(previous).hover(function(event){
   if (!isAnimating) {
    if (firstElementOnViewPort == 1) {
     jQuery(sliderList).css('left', "-" + numElmts * sizeFirstElmnt + "px");
     firstElementOnViewPort = numElmts;
    }
    else {
     firstElementOnViewPort--;
    }

    jQuery(sliderList).animate({
     left: "+=" + increment,
     y: 0,
     queue: true
    }, "swing", function(){isAnimating = false;});
    isAnimating = true;
   }

  });

  jQuery(next).hover(function(event){
   if (!isAnimating) {
    if (firstElementOnViewPort > numElmts) {
     firstElementOnViewPort = 2;
     jQuery(sliderList).css('left', "0px");
    }
    else {
     firstElementOnViewPort++;
    }
    jQuery(sliderList).animate({
     left: "-=" + increment,
     y: 0,
     queue: true
    }, "swing", function(){isAnimating = false;});
    isAnimating = true;
   }
  });


 }
};

1 个答案:

答案 0 :(得分:2)

关于触发事件,请参阅http://api.jquery.com/trigger/

$('#idOfElement').trigger('mouseover'); // or similar

对于你的插件,你也想要捕获mouseOut。如果在动画结束时检查mouseOut是否被触发,如果没有 - 触发另一个悬停。

对于你的代码,我会稍微重构一下:

(function($){  
jQuery.fn.carousel = function(previousButton, nextButton, options){
 var $sliderList = jQuery(this).children()[0];
 var $previous = $(previousButton);
 var $next = $(nextButton);
 var isAnimating = false;
 var buttonPressed = false;

 function previous(obj) {
   buttonPressed = true;
   // previous animation code
   // when you're done animating, see if buttonPressed is true still (if out was fired, then it's false)
  // if true - call previous again
 }
 function next(obj) {
   buttonPressed = true;
   // next animation code
 }
 function out() {
   buttonPressed = false;
 }

 if (sliderList) {
    var increment = jQuery(sliderList).children().outerWidth("true"),
        elmnts = $sliderList.children(),
        numElmts = elmnts.length,
        sizeFirstElmnt = increment,
        shownInViewport = Math.round(jQuery(this).width() / sizeFirstElmnt),
        firstElementOnViewPort = 1;

    for (i = 0; i < shownInViewport; i++) {
      $sliderList.css('width',(numElmts+shownInViewport)*increment + increment + "px"); // use the cached sliderList, ya?
      $sliderList.append(jQuery(elmnts[i]).clone());
    }

    $previous.hover(previous, out);
    $next.hover(next, out); // calls next on over, out on exit

  }
};
})(jQuery);