jQuery滑块不会立即停止

时间:2016-09-19 17:09:44

标签: jquery css slider

我想知道为什么我的jQuery滑块在事件后不会立即停止:mouseover。有一个延迟,有人可以帮我解决这个问题吗? 这里有我的代码:https://jsfiddle.net/jrswchkp/1/

    $(function() {

  var $clientcarousel = $('#clients-list');
  var clients = $clientcarousel.children().length;
  var clientwidth = (clients * 400); // 140px width for each client item 
  $clientcarousel.css('width', clientwidth);

  var rotating = true;
  var clientspeed = 0;
  var seeclients = setInterval(rotateClients, clientspeed);


  function rotateClients() {
    if (rotating != false) {
      var $first = $('#clients-list li:first');
      $first.animate({'margin-left': '-220px'}, 5000, "linear", function() {
        $first.remove().css({'margin-left': '0px'});
        $('#clients-list li:last').after($first);
      });
    } else {
    $('#clients-list li').stop();
    }
  }

  $(document).on({
    mouseover: function(){
      rotating = false; // turn off rotation when hovering
    },
    mouseleave: function(){
      rotating = true;
    }
  }, '.clients');

});

1 个答案:

答案 0 :(得分:1)

您需要确保在运行.stop()

时清除队列
else {
    $('#clients-list li').stop(true, false);
}

您可以详细了解.stop()here

这是JSFiddle