jquery - Jquery中如何停止动画效果(stop方法)

标签 jquery html css

我是 Jquery 的新手,我正在尝试制作动画,但我不知道如何停止动画。

我在 3 个 anchor 标记中有 3 个高度或宽度不相等的图像,它们堆叠在一个 div 中。

我写了一个脚本,当你 mouseenter() 一个图像时,图像的宽度应该增加 20px,当你 mouseleave() 时,宽度应该达到预览宽度。

我设法做到了这一点,但是当我用鼠标快速浏览图像时,图像开始变大变小,布局变得困惑。

我找到了一些可以满足我需要的插件,但它们都使用等宽和等高的图像。

如果有人能提供帮助,我将不胜感激。

这里是一个链接,代码为 http://jsfiddle.net/ClaudiuTicu/9kTft/2/

 var original_w;
$('.getBigger').on('mouseenter', function () {
    original_w = $(this).find('img').width(); //add the width of thye image to the parent
    original_h = $(this).find('img').height(); //add the height to the parent

    if ($(this).find('img').is(':animated')) { }
    else {
        if ($(this).attr('style')) { } //has the style attribut
        else {
            $(this).css({
                'width': original_w,
                'height': original_h
            });
        } //dosen't have the style attribut
    }

    if ($(this).find('img').width() == original_w) {
        $(this).find('img').animate({
            'width': original_w + 20
        }).addClass('hovered');
    } //if the image width is equal with the parent's width
    else {

    }


}).on('mouseleave', function () {
    if ($(this).find('img').width() != original_w) {
        $(this).find('img').animate({
            'width': original_w
        }).removeClass('hovered');
    }
    console.log(original_w);
}); //end mouse leave

最佳答案

.animate() 的所有实例之前使用 .stop(true,true) - 它强制动画进入其最终状态,并删除所有排队的动画。

例子:

$(this).find('img').stop(true,true).animate(...);

关于jquery - Jquery中如何停止动画效果(stop方法),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15860561/

相关文章:

javascript - 需要解决 reportValidity()

javascript - 当类型 ="number"后面没有数字时,小数点将被忽略

javascript - 从 HTML 表格中读取列中的文本

javascript - 页脚贴在底部

javascript - 自定义选择框 js 不适用于 menu-aim js

javascript - 如何判断用户点击的是视频元素还是视频控件之一?

html - CSS 在大于屏幕的元素之前居中

javascript - 附加奇怪的行为

CSS :first child on link class not working

html - 怪癖模式和标准模式