jquery - 动态页面增长和流畅,在那?

标签 jquery css dynamic html

我还没有真正对动态页面做任何事情,所以我想知道真正的诀窍是什么。我设置了一个示例并为其提供了一些动态内容,但是让页面正确响应并顺利进行并不是我想的那样。

这是我目前拥有的 jQuery Controller

var container = $('#container'),
    heightOrig = $(document).height(),
    contHeightOrig = container.height(),
    limit = 8,
    current = 0;

$('.current').html( heightOrig + 'px' );

var end = setInterval(function() {
    
    if ( current === limit ) {
        if ( contHeightOrig != container.height() ) {
            container.height($(document).height());
        }
        clearInterval( end );
    }
    
    var height = $(document).height(),
        newEnd = $('.offset:last-child'),
        appendNew = '<div class="offset"><p class="adjustment">HTML at ' + height + 'px</p></div>';

    if ( contHeightOrig != container.height() ) {
        container.height($(document).height());
    }
    
    newEnd.after(appendNew);
    
    current++;
    
}, 1000);

显然,目前显示的内容只是一个简单的附加内容,因为我正在尝试了解其功能。

JSFiddle Example

最佳答案

只要您不为这些对象设置固定高度,就不需要使用 javascript 调整页面高度或容器。当您添加更多内容时,它们的高度会自动调整。

修改后的 CSS

html, body { margin: 0; padding: 0; }
body > #container { min-height: 100%; }
#content { padding-bottom: 100px; }
#footer { clear: both; position: relative; z-index: 10; height: 100px; margin-top: 0; }

修改后的JS

var container = $('#container'),
heightOrig = $(document).height(),
contHeightOrig = container.height(),
limit = 8,
current = 0;

$('.current').html( heightOrig + 'px' );

var end = setInterval(function() {

    if ( current === limit ) {
        //if ( contHeightOrig != container.height() ) {
        //    container.height($(document).height()-100);
        //}
        clearInterval( end );
    }

    var height = $(document).height(),
        newEnd = $('.offset:last-child'),
        appendNew = '<div class="offset"><p class="adjustment">HTML at ' + height + 'px</p></div>';

    //if ( contHeightOrig != container.height() ) {
        //container.height($(document).height()-100);
    //}

    newEnd.after(appendNew);

    current++;

    }, 1000);

这是为您更新的 fork :

http://jsfiddle.net/Rvm6L/

编辑:

好的,这是一个带有流畅动画的 jsfiddle。稍作花招,您就会想要清理 CSS 和 JavaScript。解决方案与我提出的没有什么不同,只是为您添加了一个小动画,它根据内容量与视口(viewport)之间的差异进行调整 (jQuery(window).height())

http://jsfiddle.net/Rvm6L/2/

祝你好运!

关于jquery - 动态页面增长和流畅,在那?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23098547/

相关文章:

javascript - 如何选择没有当前选择器的标签的子项

jquery - Rails 自动完成 Jquery (crowdint) : select . uniq 不在范围内工作,避免重复

javascript - 单页应用程序中的历史记录.pushState/后退按钮/导航

javascript - 固定 div 父级的 jQuery UI 小部件自动完成问题

html - 为什么有时父元素的高度为 0px?

c# - 在 C# 中将对象视为属性字典

html - 使用 SCSS 动态添加 block 引用图像

html - 如何制作即使在小页面和宽度下也能完美的 html 表格

sql - 动态跳过 Where 子句

具有深/浅绑定(bind)的动态/静态范围(练习)