简体   繁体   English

jQuery animate()第二次未正确设置高度动画

[英]jQuery animate() doesn't correctly animate height the second time

I have built a toggle that will slide down a div to reveal content. 我建立了一个可将div向下滑动以显示内容的开关。 I am not using the normal toggle() function of jQuery because I wanted to show the top 300px of content and then slide to reveal the rest. 我没有使用jQuery的常规toggle()函数,因为我想显示内容的前300px,然后滑动以显示其余内容。

Anyways, I have a script setup that animates the height of the container div, which reveals the content inside. 无论如何,我有一个脚本设置,可对容器div的高度进行动画处理,以显示内部内容。

function slideCut() {

    var revertHeight = 300;
    var finalHeight = 0;
    var s = 1000;

    $('.cutBottom a').click(function() {

        event.stopPropagation();
        var p = $(this).parent().parent();
        var h = p.css('height', 'auto').height();
        var cut = $(this).parent().find('.cutRepeat');

        // Fix height
        if (finalHeight == 0) {
            p.css('height', 'auto');
            finalHeight = p.height();
            p.height(revertHeight);
        }

        if ($(this).hasClass('toggled')) {
            $(this).removeClass('toggled');
            p.animate({height:revertHeight}, {
                duration: s
            });
            cut.fadeIn('fast');
        } else {
            $(this).addClass('toggled');
            p.animate({height:finalHeight}, {
                duration: s,
                complete: function() {
                    cut.fadeOut('fast');
                }
            });
        }
        return false;
    });

}//end

The problem is, the second time it animates the height to the full size (sliding the div to reveal content) it does not animate, it just jumps to the full height. 问题是,第二次将高度设置为全尺寸动画(滑动div以显示内容)时,它没有动画,而只是跳至全高度。 Why is this happening? 为什么会这样呢?

Working example: http://jsfiddle.net/6xp2Y/3/ 工作示例: http : //jsfiddle.net/6xp2Y/3/

After all that hard work and fiddle being broken, all we had to do was remove one line from your code: 在所有的辛勤工作和小提琴被打破之后,我们要做的就是从您的代码中删除一行:

function slideCut() {

    var revertHeight = 300;
    var finalHeight = 0;
    var s = 1000;

    $('.cutBottom a').click(function() {

        event.stopPropagation();
        var p = $(this).parent().parent();
        //var h = p.css('height', 'auto').height(); //REMOVE THIS LINE
        var cut = $(this).parent().find('.cutRepeat');

        // Fix height
        if (finalHeight == 0) {
            p.css('height', 'auto');
            finalHeight = p.height();
            p.height(revertHeight);
        }

        if ($(this).hasClass('toggled')) {
            $(this).removeClass('toggled');
            p.animate({height:revertHeight}, {
                duration: s
            });
            cut.fadeIn('fast');
        } else {
            $(this).addClass('toggled');
            p.animate({height:finalHeight}, {
                duration: s,
                complete: function() {
                    cut.fadeOut('fast');
                }
            });
        }
        return false;
    });

}//end

slideCut();

Updated your fiddle: http://jsfiddle.net/brandonscript/6xp2Y/5/ 更新了您的小提琴: http : //jsfiddle.net/brandonscript/6xp2Y/5/

Updated answer! 更新的答案!

The proplem lies here 问题就在这里

if (finalHeight == 0) {
  parent.css('height', 'auto');
  finalHeight = parent.height();
  parent.height(revertHeight);
  console.log('finalHeight:'+finalHeight);
}

This is only running at the beginning, because finalHeight is not 0 anymore after first run. 这仅在开始时运行,因为首次运行后finalHeight不再为0

You can fix this by setting finalHeight back to 0 after closing it again, like so 您可以通过在将其再次关闭finalHeight设置回0来解决此问题,如下所示

if ($(this).hasClass('toggled')) {
  $(this).removeClass('toggled');
  parent.animate({height:revertHeight}, {
    duration: speed
  });
  cut.fadeIn('fast');
  finalHeight = 0;
} else { [...]

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

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