简体   繁体   中英

a fixed div inside another div

I have been struggling with this problem for over 2 hours, Is there a way to make a div fixed while it is inside a bigger div. When I scroll I want to keep the right part not scrolling.

滚动图像

So my question is, is there a way to do this without jquery?

Thanks

You have to position the inner div absolute ly:

.outerDiv {
    position: relative;
    /* give it some height */
}
.contentDiv {
    height: 100%;
    overflow: auto;
}
.innerDiv {
    position: absolute;
    right: 0;
    left: 50%;
    top: 0; bottom: 0;
}

Here's the fiddle: http://jsfiddle.net/wSxss/


Adjust the positioning values according to your needs.

This fiddle demonstrates the following solution :

HTML

<div class="wrapper">
    <div class="scroller></div>
    <div class="fixed"></div>
</div>

CSS (example of key parts)

.wrapper {
    position: relative;
    height: 40px;
    overflow: hidden;
}

.scroller {
    padding-right: 200px;
    height: 100%;
    overflow: auto;
}

.fixed {
    position: absolute;
    top: 0;
    right: 15px;
    bottom: 0;
    width: 160px; /* .scroller padding minus the right offset to accomodate scroll bar and minus any real separation between it and the scroller */
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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