首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将div折叠到在style属性中设置的高度?

如何将div折叠到在style属性中设置的高度?
EN

Stack Overflow用户
提问于 2013-07-12 04:01:28
回答 2查看 223关注 0票数 0

下面是悬停时展开折叠div的代码。在div中多次单击超链接后,或者在多次鼠标悬停之后,当鼠标移出时,div有时不会折叠到其显示高度。

有没有办法将div折叠到鼠标输出时在div的style属性中设置的高度?我的示例页面是:http://apparelnbags.com:8000/showproduct2.aspx?ProductID=829 (选择在活动颜色块/分区中显示的不同颜色

代码语言:javascript
复制
 <script type='text/javascript' src='/jscripts/jquery-1.5/jquery-1.5.js'></script>

         <script type="text/javascript">
             $(document).ready(function () {
                 var divHeight;
                 var contentHeight ;
                 $('.expand').hover(function () {
                     divHeight = $(this).height();
                     contentHeight = 0;
                     $(this).children().each(function () {
                         contentHeight += $(this).height();
                     });
                     if (divHeight < contentHeight) {
                         $(this).animate({
                             height: contentHeight
                         }, 300);
                     }
                 }, function () {
                     if (divHeight < contentHeight) {
                         $(this).animate({
                             height: divHeight
                         }, 300);
                     }
                 });
             });
        </script>

Div的css为

代码语言:javascript
复制
div.expand
{
    border: 1px solid #C8C8C8;overflow-y: scroll; -ms-overflow-y:scroll;
}

Div代码为

代码语言:javascript
复制
<div class="text_theme_red_bold" style="Display:block;margin-top: 15px;width:438px">Active Colors:<br/>
    <div id="Color_Active"  class="expand"  style="Display:block;margin-top: 8px;padding:5px 0px 5px 5px;  height:36px;">
        (!Dynamic_Contents!)
    </div>
</div>
EN

回答 2

Stack Overflow用户

发布于 2013-07-12 05:12:26

尝试这样做:在每个$(this).animate(...)之前,调用$(this).stop()来结束前面的动画。

票数 0
EN

Stack Overflow用户

发布于 2013-07-16 04:38:31

试试这个JS:

代码语言:javascript
复制
 $(document).ready(function () {
     var startHeight = $('.expand').height();
     var fullHeight = 0
     $('.expand').children().each(function () {
         fullHeight += $(this).height();
     });
     $('.expand').mouseenter(function(){
         $(this).stop();
         $(this).animate({
             height: fullHeight
         }, 320);
     });
     $('.expand').mouseleave(function(){
        $(this).stop();
        $(this).animate({
            height:startHeight
        }, 320);         
     });
 });
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17602397

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档