jquery 基于内容的分割高度

f4t66c6m  于 2023-01-01  发布在  jQuery
关注(0)|答案(3)|浏览(94)

当文档准备好时,我在内容div中加载了一个页面。但是,我也有一些链接,当用户点击它们时,它们会将不同的页面加载到同一个内容div中。我试图建立内容div的高度来控制其他东西,但是一旦在页面加载时设置了div的高度,我似乎无法获得新页面所在的内容div的新大小...
例如,加载的页面是6000px,第1页的高度是500px,第2页的高度是300px。当页面加载时,内容div设置为6000px。然而,当我单击第1页将其加载到内容div中时,内容div仍然设置为6000px,即使内容本身的高度只有500px,第2页也是如此,等等...
下面是html:

<div id="select">
    <div id="menu">
        <ul>
        <li><a class="load" href="javascript:void(0)" id="page1">page1</a></li>
        <li><a class="load" href="javascript:void(0)" id="page2">page2</a></li>
        </ul>
    </div>
</div>
<div id="spacer"></div>
<div id="content"></div>

下面是CSS:

#select { 
    width: 215px;
    top: 20px;
    left: 10px;
    position: absolute;
    }

#spacer {
    border-right:2px solid #000000;
    left:10px;
    width:215px;
    position: absolute;
    }

#content {
    left: 227px;
    top: 20px;
    width: 703px;
    padding: 0; 
    margin: 0;
    position: absolute;
    }

下面是我的jquery:

$(document).ready(function() {
    //Load content on page load
    $("#content").html('<ul><li>Loading Content...</li></ul>')
                 .load("/content/" + "projects.php", null, function() {
                            contentHeight = $("#content").height();
                            selectHeight = $("#select").height();
                            $("#spacer").height(contentHeight + "px")
                                        .css({"top": selectHeight + 40 + "px" });
                 });
    //Load content on click
    $(".load").click(function(){
                    loadName = $(this).attr("id");
                    $("#content").html('<ul><li>Loading Content...</li></ul>')
                                 .load("/content/" + loadName + ".php", null, function(){
                                    contentHeight = $("#content").height();
                                    selectHeight = $("#select").height();

                                    if(selectHeight < contentHeight) {
                                    $("#spacer").css({"top": selectHeight + 40 + "px", "display": "block" })
                                                .height(contentHeight + "px");
                                    }else{        
                                    $("#spacer").css({"display": "none"}); 
                                    return;
                                    }
                    });
    });
});
noj0wjuj

noj0wjuj1#

尝试将事件更改为单击,而不是“加载”

vwkv1x7d

vwkv1x7d2#

你试过把高度设为“自动”吗?
编辑:我看了一下你正在做的事情,一开始有点混乱(一点CSS会有很大帮助)。我想知道是否不使用“var”分配变量会导致问题,但出于某种原因,这个脚本总是为我工作。
所以,我稍微清理了一下代码--我不确定为什么每次都要添加selectHeight,因为该值不应该更改--您可以在CSS中处理它,因为不需要绝对定位。
所以,试试这个(没有改变HTML,所以我没有添加它):
中央支助系统

#spacer {
 background: #555;
 float: left;
 width: 200px;
 margin-right: 20px;
 padding: 20px;
}
#content {
 background: #444;
 float: left;
 width: 1000px;
 padding: 20px;
}

脚本

$(document).ready(function(){
 //Load content on page load
 $("#content").html('<ul><li>Loading Content...</li></ul>')
  .load("/content/" + "projects.php", null, function() {
   $("#spacer").height( $("#content").height() + "px");
  });

 //Load content on click
 $(".load").click(function(){
  var loadName = $(this).attr("id");
  $("#content")
   .html('<ul><li>Loading Content...</li></ul>')
   .load("/content/" + loadName + ".php", null, function(){
    $("#spacer").height( $("#content").height() + "px");   
  });
 });
});
vbkedwbf

vbkedwbf3#

有两种选择
1.)您可以使要加载到内容div中的所有页面高度与内容div的大小相同
2.)其他由Jquery提供
首先清除右侧内容div**$('# content '). remove();**
第二次加载您的内容,并在$('# content'). height(...)的帮助下设置加载文档的高度
参考:http://api.jquery.com/height/

相关问题