首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么这个javascript (jquery)函数看起来不正常?

为什么这个javascript (jquery)函数看起来不正常?
EN

Stack Overflow用户
提问于 2010-09-02 16:48:16
回答 2查看 395关注 0票数 4

我在做一个简单的噩梦!

我要它:

height.

  • Animate

  • 读取具有现有内容的div的高度,

  • 用新内容(提供给函数)更新div,

  • 读取div应该是的高度,但将其设置为原始的div高度,即div的高度以调整和适应新内容。

守则如下:

代码语言:javascript
复制
// function to display status content by animating the height of the status message
function slideStatus(content){

    // get current height
    var status_origheight = $("#status-message").height();

    // insert new content
    $("#status-message").html(content);

    // get new height
    var status_newheight = $("#status-message").height();

    // set the height to the orig value, hiding overflow content
    $("#status-message").height(status_origheight).css("overflow","hidden");

    // animate the div to the new height
    $("#status-message").animate({height:"+="+(status_newheight - status_origheight)+""}, 1000);

}

当我运行它时,它似乎忽略了内容已经更改的事实,而只是使用原始的高度(因此,不做动画,因为它认为高度没有改变)。

如果可以的话请帮忙!!快把我逼疯了。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-09-02 16:58:48

为我工作。你的窃听器可能在别的地方..。http://jsfiddle.net/6aBfD/

更新

http://jsfiddle.net/6aBfD/3/

但那只管用一次。问题是,您依赖于一个元素来设置它自己的高度并从中读取。但是在第一次运行之后,您将高度锁定到一个特定的值。更新后的链接具有正确的工作闭合,多次单击它。我补充了以下内容:

代码语言:javascript
复制
$("#status-message")
    .css("overflow","visible")
    .css("height", "auto");

现在,当你读到高度时,它将是自然高度。然后,稍后再设置卵子流和高度,以便将其锁定到您想要的位置。

票数 4
EN

Stack Overflow用户

发布于 2010-09-02 16:59:58

罗伯,

用下面的方法代替.height():

var status_origheight = $("#status-message").css('height');

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3629274

复制
相关文章

相似问题

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