JQuery:预先计算插入项目的高度
一些文本已通过 AJAX 调用检索,需要插入到 DOM 中:
$("#someInsertionPoint").append(someHtml);
someHtml 可能很小,以便为 #someInsertionPoint 创建 50px 的结果高度。话又说回来,可能有太多文本,#someInsertionPoint 最终的高度为 300 像素或更高,以便包含所有文本。
在 someHtml 过长的情况下,规范要求显示文本的截断版本(带有尾部省略号),后跟“更多...”链接,单击该链接时,会增加 #someInsertionPoint 的高度,动画方式,以便容纳全文。
一切看起来都很简单,但有一个例外:如何确定插入全文时 #someInsertionPoint 的高度?回想一下,该值将用作用户单击“更多...”链接时发生的动画高度变化的终点。 StackOverflow.com 上的一些帖子表明高度无法预先计算;只有在文本插入完成后才能检索它。这使我相信我的方法应该如下:
(1)将 someInsertionPoint 的可见性设置为“隐藏”。
(2) 插入全文并将someInsertionPoint的高度缓存在变量中。
(3) 截断插入的文本并添加省略号。将 #someInsertionPoint 的高度设置为 50px。
(4) 使 someInsertionPoint 可见,以在 50px 的高度显示截断的文本。
(5) 当单击“更多...”链接时,截断的文本可以替换为完整文本,并且我可以使用我在 #2 中缓存的高度将高度动画化为其理想大小。
有更好的办法吗?
谢谢。
Some text has been retrieved via an AJAX call and needs to be inserted into the DOM:
$("#someInsertionPoint").append(someHtml);
someHtml might be small, so as to create a resulting height for #someInsertionPoint of 50px. Then again, there might be so much text that #someInsertionPoint ends up with a height of 300px or more in order to contain all of the text.
In cases where someHtml is lengthy, the specifications call for a truncated version of the text to be shown (with trailing ellipsis), followed by a "more…" link which when clicked, increases the height of #someInsertionPoint, in an animated way, so as to accommodate the full text.
Everything seems straight-forward with one exception: How do I determine what the height of #someInsertionPoint will be when the full text is inserted? Recall that this value will be used as an end-point for the animated height change that occurs when the user clicks the "more…" link. Some posts on StackOverflow.com indicate that the height can't be pre-calculated; It can only be retrieved after the insertion of text is complete. This leads me to believe that my approach should be as follows:
(1) Set the visibility of someInsertionPoint to "hidden".
(2) Insert the full text and cache the height of someInsertionPoint in a variable.
(3) Truncate the inserted text and add the ellipsis. Set the height of #someInsertionPoint to 50px.
(4) Make someInsertionPoint visible to show the truncated text at a height of 50px.
(5) When the "more…" link is clicked, the truncated text can be replaced with the full text and I can animate the height to its ideal size using the height that I cached in #2.
Is there a better way?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你制造了一个问题,因为通常没有问题。这是一个设计缺陷。只是不要使用固定高度。如果您想自动调整某些内容的大小,请使用最小高度并让内容执行调整大小的操作。这是最简单的方法。否则,当您想使用固定高度时,您可以使用两个 div(一个具有固定高度)并在插入后测量“ajax”高度(它必须可见才能计算高度)。
例如:
当您尝试此示例时,#fixed div 在加载净内容时不会调整大小。插入后,您可以测量高度并根据需要调整父 div #fixed。
希望有帮助
I think you create a problem as it is normally no problem. It is a design flaw. Simply don't use fixed heights. If you want to autosize something use a min-height and let the content do the resizing stuff. That's the easy way. Otherwise when you want to use fixed heights you can use a two divs (one with a fixed height) and measure the 'ajax' height after you insert it (it must be visible to calculate heights).
For example:
When you try this example the #fixed div will not resize when loading net content. After inserting you can measure the height and adjust the parent div #fixed if you want.
Hopes it helps