4

我有这个棘手的 CSS 问题:我有这个 HTML:

<div id="wrapper">
    <div class="left"></div>

    <div id="scroll">
        Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque faucibus volutpat turpis at iaculis. Proin at nisl leo. Morbi nec blandit leo? Pellentesque interdum nunc sed nisl rhoncus gravida. Nunc sollicitudin, mi sit amet porta mollis, metus erat ornare odio, eu accumsan mauris diam nec augue. Ut tincidunt dui at lorem consequat vitae consectetur sapien pharetra. Suspendisse potenti. Donec turpis enim, varius accumsan congue vitae, rhoncus ut justo. Curabitur tristique lobortis eros ut pharetra. Maecenas non condimentum justo. Integer tincidunt; velit quis auctor varius, magna lorem pharetra urna, eget pellentesque leo nibh at mi. Ut pretium bibendum dui vel venenatis. Proin vel sem vitae lacus tincidunt bibendum. Pellentesque blandit mauris sit amet mauris sollicitudin pretium. In molestie condimentum nisi placerat consequat.
    </div>

    <div class="right"></div>
</div>

使用这个 CSS:

#wrapper {
    position: relative;
    display: block;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    height: 47px;
}

#scroll {
    position: relative;
    height: 100%;
    width: 10000px;
}

div.left, div.right {
    position: absolute;
    display: block;
    background-color: rgba(255, 0, 0, 0.5);
    width: 24px;
    height: 100%;
    z-index: 100;
    top: 0;
}

div.left {
    left: 0;
}

div.right {
    right: 0;
}

视觉结果是这样的: 在此处输入图像描述

出于某种原因,div.right当我滚动#scroll. 我希望它始终漂浮在#wrapper.

这就是我现在得到的: 在此处输入图像描述

这是jsfiddle:http: //jsfiddle.net/b5fYH/

谢谢

编辑

仅仅因为它不明显,它必须在移动设备上工作。

4

2 回答 2

5

position: absolute你必须知道和之间的区别position: fixed

第一个意味着:将元素放置在相对元素内的绝对位置并保持在那个位置(相对)。

第二种:将元素放在窗口(框架)内的绝对位置,无论发生什么都保持在那里。

检查这个小提琴:http: //jsfiddle.net/b5fYH/1/

于 2013-03-11T21:05:13.053 回答
3

The problem is with how overflow-x changes the wrapper div width.

The solution I found was:

Demo: http://jsfiddle.net/5jWpG/

  1. wrapping the whole thing with a new div with the id wrapper-container
  2. then adding the following CSS code:

    #wrapper-container {
        position: relative;
    }
    
    #wrapper {
        position: static; /* or remove position relative from your code */
    }
    
    div.left, div.right {
        bottom: 16px; 
        height: auto; /* or remove height: 100% from your code */
    }
    
于 2013-03-11T21:16:58.190 回答