【问题标题】:jQuery change the .height when appending contentjQuery在附加内容时更改.height
【发布时间】:2012-02-29 23:10:43
【问题描述】:

我有这个 jQuery 函数,它可以获取我的 document 的高度并将 div 粘贴到视图上。

当加载 DOM 时内容已经在这里时,它可以正常工作,但是如果我将一些 li 附加到我的容器中,我想刷新此函数以获取新的文档高度

这是js代码:

$(function()
{
    var documentHeight = 0;
    var topPadding = 55;

    var offset = $("#sidebar").offset();
    documentHeight = $(document).height();
    $(window).scroll(function() {
        var sideBarHeight = $("#sidebar").height();
        if ($(window).scrollTop() > offset.top)
        {
            var newPosition = ($(window).scrollTop() - offset.top) + topPadding;
            var maxPosition = documentHeight - (sideBarHeight + 100);
            if (newPosition > maxPosition)
            {
                newPosition = maxPosition;
            }
            $("#sidebar").stop().animate({
                marginTop: newPosition
            });
        }
        else
        {
            $("#sidebar").stop().animate({
                marginTop: 0
            });
        };
    });
});

以及HTML的结构:

<div id="single_content">
    <ul>
        <li>...</li>
        <li>...</li>
        <li>...</li>
        ...
    <ul>
</div>

我认为我需要 on.change > 更新文档高度以在我的函数中使用?

感谢您的帮助!

已解决:

我找到了解决方案,方法是将这个:$(document).height(); 放在 $(window).scroll(function() {});

感谢您的回答!

【问题讨论】:

  • 你不能不把它固定在一个变量中并且总是使用 $(document).height();??
  • 不,我需要留在这个函数中

标签: jquery dom height append


【解决方案1】:
$(window).scroll(function() {
    documentHeight = $(document).height(); // put this line inside scroll
    // the rest of code
});

【讨论】:

    猜你喜欢
    • 2011-05-15
    • 2014-05-26
    • 2014-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多