简体   繁体   English

按钮单击后停止动画

[英]Stop animation after button click

I have two buttons that control the image above, and they basically change the image in the forward direction or reverse direction. 我有两个控制上面图像的按钮,它们基本上改变了正向或反向的图像。 My code is as follows: 我的代码如下:

  slider.fadeOut(400, function() {
    $(this).attr('src', images[index]);
  }).fadeIn(400);
  console.log(index);
});

The problem is that if I have multiple button clicks, the image changes, but my fade animation happens like many times after, not just once to change the image. 问题是,如果我有多次按钮点击,图像会改变,但我的淡入淡出动画会发生很多次,而不只是一次更改图像。

Is there a way to stop the current animation, and proceed to fade in the next image? 有没有办法停止当前动画,并继续淡入下一个图像? I just dont want multiply button clicks and then like 5 fade in's after. 我只是不想要多次按钮点击,然后像5淡入淡出后。

You can use the stop() method to achieve this. 您可以使用stop()方法来实现此目的。 Providing true as the first parameter will also clear the queue of any following animations, which is useful if someone has repeatedly triggered the parent event. 提供true作为第一个参数也将清除以下任何动画的队列,这在有人重复触发父事件时很有用。 Try this: 尝试这个:

slider.stop(true).fadeOut(400, function() {
  $(this).attr('src', images[index]);
}).fadeIn(400);

http://api.jquery.com/stop http://api.jquery.com/stop

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM