简体   繁体   中英

Making a scrollable div which doesn't scroll the rest of the body

Should be simple but I can't work it out.

I have two panes on my webpage. I want both to be independently scrollable:

<body>
   <div id="sidebar"> ... lots of content ... </div>
   <div id="container"> ... lots of content ... </div>
</body>

#sidebar{
    width:200px;
    position:fixed;
    overflow:scroll;
    background-color:black;
    color:white;
    top:0;
    left:0;
    height:100%;
}

Here is a JSFiddle . Notice how when you scroll to the bottom in the black sidebar, and keep scrolling, the body starts to scroll? That's what I want to avoid. Is there a simple way to achieve this?

Please try following updated CSS. Here I have set 200px height after that it will add scroll bar for #sidebar div.

#sidebar{
    width:200px;
    position:fixed;
    overflow:auto;
    background-color:black;
    color:white;
    top:0;
    left:0;
    height:200px;

}

Hope it may help.!!

body { 
overflow-y : hidden 
} 

helps you if you don't want body to be scrolled.

This is working if I understood your problem correctly. Just making overflow:hidden when mouse is over sidebar div.

 #sidebar:hover + #container{
    overflow-y: hidden;
    height: some_value; // your window height
 }

You´re right. In Chrome it happens. Maybe some jQuery?

$("#sidebar").focus(function(){
    $('body').css("overflowY","hidden");
});
$("#sidebar").blur(function(){
    $('body').css("overflowY","scroll"); 
});

Add tabindex="-1" to sidebar to get this working

Actually Working

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