【问题标题】:Problem using JQuery to animate div to 100% height使用 JQuery 将 div 设置为 100% 高度的问题
【发布时间】:2009-07-14 21:40:34
【问题描述】:

您好,我正在使用 Jquery 将 div 从设定高度设置为 100% 并再次返回到设定高度。这用于显示/隐藏长于设定高度的 wordpress 帖子中的文本(溢出被隐藏)。我无法将 div 从设置的高度设置为 100% 的动画 - 相反,div 会跳转到高度的 100%。它确实可以平滑地动画回到它的设定大小。我无法为其设置动画高度,因为帖子的高度总是会根据已写入的内容而有所不同。

这是 JQuery 代码:

$(document).ready(function(){

     $(".read_more").toggle(function(){
     $(this).prev(".entry").animate({height:"100%"}, {
    queue:false,
    duration:1000,
    easing: "easeOutQuart"});
     },

function(){  

        $(this).prev(".entry").animate({height:"5.9em"}, {
        queue:false,
        duration:1000,
        easing: "easeOutQuart"});

});         
});

CSS:

.entry {

     float:left;
     position:relative;
     width:870px;
     height:5.9em;
     overflow:hidden;
     padding: 0px 10px 10px 10px;
     display:block;
}

基本上 .entry div 被包装在一个容器中,该容器用于放置帖子标题、帖子和帖子分隔符。如果我插入一个设定值但没有运气让 100% 工作,动画就可以工作。有什么想法吗?

这是完整的 jquery(yellow_read、invistext 和 gray_read 是单击时触发动画并从展开变为折叠的文本。

$(document).ready(function(){
     $(".read_more").toggle(function(){
     $(this).prev(".entry").animate({height:"100%"}, {
    queue:false,
    duration:1000,
    easing: "easeOutQuart"});
     $(this).children(".yellow_read").css("display","none");   
     $(this).children(".invistext").css("display","inline");
     $(this).children(".gray_read").css("display","none");
},

function(){  
    $(this).prev(".entry").animate({height:"5.9em"}, {
    queue:false,
    duration:1000,
    easing: "easeOutQuart"});
$(this).children(".yellow_read").css("display","inline");   
    $(this).children(".invistext").css("display","none");
    $(this).children(".gray_read").css("display","inline"); 
});     

});

这是完整的CSS

        #content{
            width:1000px;
            margin-left:16em;
            padding-top:15em;
            position:absolute;
            overflow:hidden;
            display:block;
            }

            *:first-child+html #content{
                height:1000px;
                position:relative;
                float:left;
                width:400px;
                padding: 0px 0px 0px 0px;

                }

            .post{
            float:left;
            position:relative;
            margin-bottom:10px;
            padding:9px
            }

            .post p{
                font-size:.9em; /*controls the post body when WP sIFR is off*/
                line-height:1.8em; /*controls the post body when WP sIFR is off*/
                }

            .post p a:hover, .post p a:active{ color: #ed1c24; text-decoration:none;}



            .postHeading{
                float:left;
                position:relative;
                width:870px;    

                }

                .postTitle{
                    float:left;
                    position:relative;
                    padding:5px 7px 5px 7px;

                    }

                    * html .postTitle{
                        width:860px;
                        padding:5px 7px 5px 7px;
                    }

                .postTitle h2{
                    line-height:1.2;
                    margin:0;
                    padding:0;
                    color:#ed1c24;
                    font-size:1.8em;
                }



            .entry {
                float:left;
                position:relative;
                width:870px;
                color: #fff;
                height:5.9em;
                overflow:hidden;
                padding: 0px 10px 10px 10px;
                display:block;
                }


            .entry a a:link, .entry p a:visited{ color:#fff200; text-decoration:none;}



            .read_more {                
                width:870px;
                font-size:14px;
                color: #999999;
                float:left;
                position:relative;
                padding: 10px 5px 10px 10px;
                cursor:pointer;
                }

                * html .read_more{
                    width:870px;
                    }


            SPAN.gray_read{             
                color: #999999;
                font-size:.8em;
                }


            SPAN.yellow_read{
                color: #fff200;
                font-size:.8em;
                }

            SPAN.invistext{
                display:none;
                color: #ff5d00;
                font-size:.8em;
                }

            .divider {  
                background:url(images/divider.png) no-repeat center;
                width:680px;
                height:6px;
                float:left;
                position:relative;
                }

                    * html .divider{
                        width:680px;
                    }

和html:

<div id="content">



<?php if (have_posts()) : ?>

    <?php while (have_posts()) : the_post(); ?>

        <div class="post" id="post-<?php the_ID(); ?>">

            <div class="postHeading">

                <div class="postTitle">
                    <h2><?php the_title(); ?></h2>
                </div><!-- post title ends -->
            </div><!-- post heading ends -->


<div class="entry">


                <?php the_content('Read the rest of this entry &raquo;'); ?>

<div class="read_more">
    <span class="gray_read">Read </span>
        <span class="yellow_read"> More </span>
            <span class="gray_read"> +</span>
                <span class="invistext"> Collapse -</span>
</div>

<?php endwhile; ?>

<?php else : ?>

    <h2 class="center">Not Found</h2>
    <p class="center">Sorry, but you are looking for something that isn't here.</p>
    <?php include (TEMPLATEPATH . "/searchform.php"); ?>

<?php endif; ?>

谢谢

【问题讨论】:

  • 我尝试了您的 jQuery 代码,它对我来说效果很好。它逐渐增加到100%。也许容器 DIV 中还有其他东西导致了问题。请发布包装每个条目的容器 div 的 html/css。

标签: jquery


【解决方案1】:

我之前尝试在 animate 中混合不同类型的值时遇到过问题。您可以尝试获取窗口高度并将其转换为 em,以查看它是否更喜欢 em-to-em 动画而不是 em-to-percent。它可能会或可能不会为您解决问题,但这是我的想法。

【讨论】:

  • 这可能会有所帮助。是否有可能以 100% 传递相当于 div 高度的 em?我知道 jquery 可以计算 div 的高度,但它可以计算 100% 的高度是多少,将其转换为 em,然后动画到那个高度?如果是这样,您知道如何编写脚本吗?
【解决方案2】:

试试slideDownslideUp 效果方法,它们应该完全符合您的要求。

【讨论】:

  • 在继续使用 Animate 方法之前,我尝试了这些方法。问题是我希望 div 以设定的高度开始/结束,而不是完全折叠。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-11-10
  • 2015-03-17
  • 2013-07-18
  • 2012-07-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多