【问题标题】:independently jquery toggle animate height only runs once each time独立jquery切换动画高度每次只运行一次
【发布时间】:2012-06-29 19:04:34
【问题描述】:

这个问题基于我之前的问题,我得到了一个有效的答案,但仅限于一个滑动切换元素:link

我使用 animate 方法在后台加载“隐藏”元素。否则我可以只使用滑动切换,但这会导致显示:没有我不想要的。

所以,这是我目前得到的函数,但它只为每个 h2 运行一次。

HTML:

<h2>Show</h2>
    <div class="content">
        text text text
        <br />
        text text text
        <br />
        text text text
    </div>

<h2>Show</h2>
    <div class="content">
        text text text
        <br />
        text text text
        <br />
        text text text
    </div>

CSS:

.content {
  height: 0;
  overflow: hidden;
}

.heightAuto {
    height: auto;
}​

脚本:

$(function(){  

    $("h2").toggle(function()

     {    
       var $content = $(this).next(".content");
       var contentHeight = $content.addClass('heightAuto').height();
       $content.removeClass('heightAuto');

       $content.removeClass('heightAuto').animate({ height: contentHeight}, 500);

     }, function() {

        $(this).next(".content").animate({ height: "0"}, 500);    

     });
});​

​再次将高度设置为自动会不会有问题?我就是找不到诀窍。

这也是fiddle:jsfiddle

【问题讨论】:

标签: jquery jquery-animate toggle


【解决方案1】:

添加一个不会调整大小的额外包装器:

<div class="content"><div class="inner">
    text text text
    <br />
    text text text
    <br />
    text text text
</div></div>

然后使用这个JS:

jQuery(function($) {  
    $('h2').toggle(function() {
        var $content = $(this).next('.content');
        $content.animate({ height: $content.find('> .inner').height() }, 500);
    }, function() {
        $(this).next('.content').animate({ height: 0 }, 500);       
    });
});

编辑:如果您不想在 HTML 中使用额外的包装器,您可以让 JS 为您添加一个包装器:

jQuery(function($) {  
    $('h2').toggle(function() {
        var $content = $(this).next('.outer');
        $content.animate({ height: $content.find('> .content').height() }, 500);
    }, function() {
        $(this).next('.outer').animate({ height: 0 }, 500);       
    }).next('.content').wrap('<div class="outer" style="height:0;overflow:hidden"></div>');
});​

如果您使用这种方法,您应该删除 .content 块的 CSS。查看更新的小提琴:http://jsfiddle.net/QwmJP/21/

【讨论】:

  • 哇,谢谢!这终于奏效了!但是还有没有额外包装的方法吗?
  • 我必须承认,我不完全理解你为什么不只是使用 .slideToggle()
  • 谢谢!好的,这是我的解释,如果我在这里错了,请纠正我。我有一个带有 5 个隐藏容器的页面。他们每个人都有 youtube 视频、soundcloud 播放器和第三方工具。如果我使用导致 display: none 的 slideToggle,所有这些元素都没有完全准备好直到 display: 块集,这不仅导致 slideToggle 效果而且在加载时间上都非常缺乏。使用 height:0 我确保所有元素都在后台正确加载,并且在面板设置动画时可以正常工作。还是我看错了?
  • 如果您已经测试并验证您的内容没有加载 display:none,那么您是正确的。我认为即使在 display:none 中图像也会加载,但也许这些 3rd-party 工具有 JS 来检测它们何时被隐藏。如果您使用了我的回答,请帮我一个忙并将其标记为“已接受”。
  • 就这样做了!是的,我做了一些测试,这是第 3 方工具在这里造成了麻烦。图像没有问题。再次感谢!
【解决方案2】:

为了解决您的问题,如果高度为 0,请设置 overflow:hidden

参考LIVE DEMO

JS:

$(function(){
    $("h2").toggle(
        function() {    
            var $content = $(this).next(".content");
            var contentHeight = $content.addClass('heightAuto').height();
            if (contentHeight == 0) {
                contentHeight = $content.attr({
                    style:'overflow:hidden'
                }).height(); 
            }            
            $content.removeClass('heightAuto').animate({ height: contentHeight}, 500);
        }, 
        function() {
            $(this).next(".content").animate({height:0}, 500);
        });
});​

【讨论】:

    【解决方案3】:

    因为你第一次设置height为0,所以下次设置为0,所以.height()值返回0。

    改用 offsetHeight 属性:它始终是内容的自然高度。

    【讨论】:

    • 感谢您的快速回答,但不幸的是,这不起作用,动画也不再起作用。还有其他想法吗?
    • 是属性,不是函数,所以不是offsetHeight(); w3schools.com/jsref/dom_obj_all.asp
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-28
    • 2011-06-25
    • 2021-04-01
    • 2013-02-12
    • 2014-12-17
    • 2023-04-03
    • 2011-10-15
    相关资源
    最近更新 更多