【问题标题】:Set of images stacked with offset increase for each堆叠的图像集,每个图像的偏移量增加
【发布时间】:2013-01-25 06:48:14
【问题描述】:

我有一个图像列表,我想将这些图像堆叠在一起,然后将每个图像上下偏移 30 像素。

<ul>
   <li><img src="#"></li>
   <li><img src="#"></li>
   <li><img src="#"></li>
   <li><img src="#"></li>
   <li><img src="#"></li>
</ul>

我找到了一个类似效果的 jsFiddle (http://jsfiddle.net/BrAbs/1/),但也需要为每个图像(0px、30px、60px、90px)等偏移和增加左侧位置。

【问题讨论】:

    标签: javascript jquery html css


    【解决方案1】:

    使用 jQuery:

    $('li').each(function (i) {
        $(this).css({
            left: 30 * i
        });
    });
    

    Fiddle

    在您将left CSS 属性设置为的元素上需要position:relative


    如果您需要堆栈效果而不在lis 上设置height,也可以根据要求使用absolutely 相对于relatively 定位的祖先的定位:

    .ulClass {
        position: relative; /*resets the absolute positioning of descendant li(s)*/
    }
    .ulClass li {
        position: absolute; /*absolute positioning relatively to ancestor .ulClass*/
    }
    

    然后更新的 JS 添加一个top 偏移量:

    $('li').each(function (i) {
        $(this).css({
            top: 30 * i,
            left: 30 * i
        });
    });
    

    Fiddle

    【讨论】:

    • 谢谢。有没有在不应用高度的情况下堆叠图像?所以它们绝对位于顶部,然后使用您的代码在顶部和左侧进行偏移?
    • @rdck,是的,将容器设置为relative 并绝对定位lis。秒,而我更新小提琴
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多