0

我有一个无限轮播,当我将鼠标悬停在下一个和上一个按钮上时,我想移动它。现在悬停只触发一次。当鼠标位于下一个或上一个按钮内时,我希望轮播继续移动。

有什么建议么?

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;
   }
  });


 }
};
4

1 回答 1

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); 
于 2010-06-08T18:02:05.763 回答