简体   繁体   English

固定位置,但相对于容器

[英]position fixed but relative to a container

I have to display two divs (in and out) so that they are positioned one on the left, the other on the right. 我必须显示两个div(输入和输出),以便它们一个位于左侧,另一个位于右侧。

Using this css code they fit perfectly great on full screen, but I would like to be able to put them into a parent div, that will be the container 使用此CSS代码,它们非常适合全屏显示,但我希望能够将它们放入父div,这将是容器

#in{
      position: fixed;
      top: 0;
      left: 0;
      bottom: 0;
      width: 50%;
      overflow: auto;
      font-size: 12px;
      box-shadow: 0 2px 10px rgba(0,0,0,0.2);
    }

#out{
      position: fixed;
      top: 0;
      right: 0;
      left: 50%;
      bottom: 0;
      overflow: auto;
      padding: 10px;
      padding-left: 20px;
      color: #444;
      font-family:Georgia, Palatino, 'Palatino Linotype', Times, 'Times New Roman', serif;
      font-size: 16px;
      line-height: 1.5em
    }

Can anyone help me modify this code for that matter ? 有人可以帮我修改此代码吗?

Thank you 谢谢

I'm not sure if this is exactly what you're looking for, but essentially kmmathis is right. 我不确定这是否正是您要寻找的东西,但是从本质上来说kmmathis是正确的。 http://jsfiddle.net/R8bUQ/ http://jsfiddle.net/R8bUQ/

The markup: 标记:

<div>
    <div>
    </div>
</div>

and the CSS: 和CSS:

div {
    height: 800px;
    width: 300px;
    background: #f0f;
    margin-top: 80px;
    margin-left: 180px;
    position: relative;
}
div div {
    height: 20px;
    width: 100%;
    background: #0f0;
    position: absolute;
    margin: 0;
    top: 0;
    left: 0;
}

and the JS: 和JS:

$parent = $('div').eq(0);
$elem = $parent.find('div');

$(window).scroll(function(){ 
    $elem.css('top', $(window).scrollTop());
}).trigger('scroll');

Basically, you set the child to be positioned relative to its parent, and as you scroll, the amount that the window has scrolled sets the top of the element. 基本上,您将子级设置为相对于其父级定位,并且在滚动时,窗口滚动的量将设置元素的top It's fixed positioning, but is positioned relative to its parent. 它是固定位置,但相对于其父对象定位。 It's not 100%, but it might give you a basic idea of how to accomplish what you want. 这不是100%,但可能会为您提供有关如何完成所需内容的基本概念。

When using position: fixed; 使用position: fixed; it fixes the element to the screen. 它将元素固定在屏幕上。 You cannot position it relative to a element with CSS, in fact if you think about what position: fixed does you would never want to position it relative to a element and thought of it sounds impossible to me. 实际上,如果您考虑使用什么position: fixed ,就无法将其相对于CSS元素position: fixed是,您永远都不想相对于元素定位它,而想到它对我来说是不可能的。 A fixed positioned element is is taken out of the flow so that it is relative to the viewport you cannot just put it back in the flow somewhere. 从流中取出固定位置的元素,以便它相对于视口,您不能只是将其放回流中的某个位置。

When using position:fixed; 使用position:fixed; you are positioning the elements relative to the browser window. 您正在相对于浏览器窗口定位元素。

To use your own parent container, you'll need to set #in and #out to use position:absolute; 要使用自己的父容器,您需要将#in和#out设置为使用position:absolute; ;。 and then wrap them in a container, let's say #wrap, that has position:relative; 然后将它们包装在一个具有position:relative; set on it. 放在上面。

Edit: here's a fiddle: http://jsfiddle.net/ktAAa/ 编辑:这是一个小提琴: http : //jsfiddle.net/ktAAa/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM