简体   繁体   English

相对于另一个div固定

[英]Fixed relative to another div

I have a div with text on the right and a div with a menu on the right. 我的div的右侧是文本,而div的右侧是菜单。 Both wrapped up with a div to center it all. 两者都用div包裹起来以将所有内容居中。 Ho can I make the div right follow the screen when the user makes scroll with the text? 当用户滚动文本时,该如何使div正确跟随屏幕? I mean that the div right keeps "fixed" in the same place but the user could make move and scroll the text.(This is not a classic fixed position. I'm not sure if I should call this something like a fixed relative to another div?) 我的意思是div权限在同一位置保持“固定”,但是用户可以移动和滚动文本。(这不是经典的固定位置。我不确定是否应该将其称为固定相对于另一个div?)

HTML: HTML:

<div id="wrapper">

<div id="text">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque convallis adipiscing dolor, eget rutrum lectus malesuada id. Phasellus vulputate, lorem et convallis vulputate, enim nulla scelerisque nunc, vel auctor orci tellus eget diam. Praesent scelerisque, mauris a molestie lobortis, lectus nisi dignissim massa, eget tempor ante sem in nisi. Proin fermentum euismod ullamcorper. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Proin nec orci arcu. Nullam molestie consequat porttitor. Pellentesque sit amet nisl odio. Ut vel mi a eros commodo vehicula sagittis ut mi. Donec eu ante velit, vel ullamcorper arcu. Etiam eros sapien, lobortis a porttitor quis, congue vel velit. Nam et dui auctor augue interdum interdum.<br /><br />

</div>

<div id="right"></div>

</div><!-- final de contingut -->

CSS: CSS:

body {background-color: #f2f2f2;font-size: 13px;margin: 0;padding: 0;}

#wrapper {margin:0 auto;width:700px;height:900px;background-color:#fff;}

#right {
    float:right;
    width:200px;
    height:500px;
    right:0px;
    border:solid thin #f00;
}

#text {
    float:left;
    width:400px;
    padding:40px;
}

I have the example here: http://jsfiddle.net/u7xRX/1/ 我在这里有示例: http : //jsfiddle.net/u7xRX/1/

As per i understand may be you can use javascript for this. 据我了解可能是您可以为此使用javascript Write like this: 这样写:

$(document).scroll(
    function(){
        var scrollOffset = $(window).scrollTop();
        if(scrollOffset >= 100){
            $('#right').css({position: 'fixed',top:'0', left:'650px'});
        }
        else{
            $('#right').css({position: 'absolute',top:'100px', left:'650px'});
        }
    }
);

Check this http://jsfiddle.net/ZVrMF/1/ 检查此http://jsfiddle.net/ZVrMF/1/

After a lot of time I could find a solution with just css. 经过很多时间,我只能使用CSS找到解决方案。 I think in fixed position if you don't give the right position it gets it from the parent div, in this case the wrapper. 我认为如果您没有给出正确的位置,它会从父div(在这种情况下是包装器)中获得正确的位置。 On the other side, I add a margin to position it on the right 另一方面,我添加了一个边距将其定位在右侧

http://jsfiddle.net/u7xRX/3/ http://jsfiddle.net/u7xRX/3/

body {background-color: #f2f2f2;font-size: 13px;margin: 0;padding: 0;}

#wrapper {position: relative; margin:0 auto;width:700px;height:900px;background-color:#fff;}

#right {
    position: fixed;
    top: 40px;
    /* right:0px; important not give right position */
    width:200px;
    height:200px;
    margin:0 0 0 500px; /* important to position it on the right */
    background-color: red;
}

#text {
    position: absolute;
    left: 0px;
    width:400px;
    padding:40px;
}

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

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