javascript - 添加元素的高度

标签 javascript jquery

我正在循环遍历所有具有不同高度并且绝对定位的列表项。我希望每个 li 在 Y 轴上显示得比前一个 li 低。

我不知道如何将所有先前的 prev_height 变量相加。第三个 li Y 坐标是前两个 li 的高度之和。

 $("li").each(function(){
    the_height=$(this).height();
    prev_height = $(this).prev().height();

    $(this).css({"left" : prev_height * 1, "top" : prev_height});
    console.log('prev height: '+prev_height);
 });

HTML

 <ul class="origin_left" style="top:100px; left:100px;">
    <li>Bus Stop Serves as Social Meeting Point Made of Giant Letters</li>
    <li style="top:; left:;">The organization builds communities by bringing together artists and audiences from diverse backgrounds to engage in the creative process. In a series of streetscape design workshops, hosted by Creative Alliance and Southeast CDC, residents of Highlandtown expressed a desire for an interactive bus stop that made a statement for the Highlandtown Arts & Entertainment District.</li>
    <li style="top:; left:;"><img src="image.jpg" alt="" /></li>
    <li style="top:; left:;">In response, Creative Alliance teamed up with Spanish collaborative mmmm….as part of the initiative TRANSIT&mdash;Creative Placemaking with Europe in Baltimore. With the help of local sculptors, they created BUS.</li>
    <li style="top:; left:;"><img src="image.jpg" alt="" /></li>
 </ul>

最佳答案

以下是具体操作方法。

var y = 0;

$("li").each(function() {
    $(this).css({
        "top": y
    });
    y += $(this).outerHeight();
});

<强> See Working Demo

<强> See Complete Code

关于javascript - 添加元素的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25414117/

相关文章:

javascript - jQuery .when 函数没有正确等待?

javascript - 错误 : "Too much recursion"

jquery - 使用jquery和rails向表添加行

javascript - 了解嵌套回调和作用域?

jquery - 使用jquery自动滚动另一个网格

javascript - AngularJS:使用相同输入添加和编辑列表项

c# - 单击操作按钮,每次单击此按钮时显示一组字段

javascript - 使用字符串名称访问类变量?

javascript - 如何使文本、数字和日期 html 输入字段以一致的方式支持 Ctrl+Z(撤消)?

javascript - 如何通过单击按钮全屏播放 YouTube 嵌入式视频?