I have been reading about fixed
div
's within relative
and absolute
div
's here:
Fix position of div with respect to another div
Fixed positioned div within a relative parent div
Fixed position but relative to container
And many other but none can help me to achive a behavior I have seen in few pages (blogs). I can not remember one at the moment, but here are some images to explain
View 1 & View 2
After scrolling down, the contextual menu sticks to the side of the view and moves down with the scrolling until reach the end of the section in which it stops. If there is more content after it, you can keep scrolling down but the contextual menu no longer follow your view. The same going up, you reach the section, the contextual menu follows you up until the start of the section, then stops and you can keep scrolling up.
Is this posible with only HTML and CSS or do I need a plugin?
Here is a jsFiddle piece of code, perphaps incomplete. Forgot to mention, I'm doing this in Angular 6+ as a component, so I don't have full access to the index.html
file with the body
tag. The jsFiddle shows what I can work with.
There were a few things going on:
body { position: relative }
in your CSSposition: sticky
needs a full height column to work. Because your col-6
that was holding your menu was only as tall as it needed to be, it won't scroll.p-sticky
class to your column.sticky
also needs a top
value to know where the element should stick to once it becomes sticky. .p-sticky {
position: sticky;
top: 60px;
}
body { position: relative; } /*some attemps*/.p-relative { position: relative; }.p-absolute { position: absolute; }.p-sticky { position: sticky; top: 60px; }.p-fixed { position: fixed; } /* Standar CSS*/.navbar { background-color: blue; width: 100%; }.nav-fixed { top: 0px; z-index: 1; position: fixed; }.content-ex1 { height: 200px; background-color: green; }.content-ex2 { height: 500px; background-color: #aaaaaa; }.menu { height: 50px; background-color: red; }
<div class="navbar"> some navbar things </div> <div class="navbar nav-fixed"> some navbar things </div> <div class="content-ex1"> Some content here</div> <div class="container"> <div class="row"> <div class="col-sm-6 p-sticky"> <div class="menu">menu or something</div> </div> <div class="col-sm-6 content-ex2"> Some content here</div> </div> </div> <div class="content-ex1"> Some content here</div>
Here's the fiddle to play around with (which includes your bootstrap): http://jsfiddle.net/w4mz9dte/
Note: you appear to be using an old version of BootStrap. You may want to update to the newest version. In that case, only a few things will change - namely, you move the p-sticky
class to the menu.
Here's the newest version of BS 4.4: http://jsfiddle.net/kamr0bjw/
body { position: relative; } /*some attemps*/.p-relative{ position:relative; }.p-absolute{ position:absolute; }.p-sticky{ position:sticky; top: 60px; }.p-fixed{ position:fixed; } /* Standar CSS*/.navbar{ background-color: blue; width:100%; }.nav-fixed{ top: 0px; z-index:1; position:fixed; }.content-ex1{ height:200px; background-color: green; }.content-ex2{ height:500px; background-color: #aaaaaa; }.menu{ height:50px; background-color: red; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script> <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.4.0/css/bootstrap.min.css" rel="stylesheet"/> <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.4.0/js/bootstrap.min.js"></script> <div class="navbar"> some navbar things </div> <div class="navbar nav-fixed"> some navbar things </div> <div class="content-ex1"> Some content here</div> <div class="container"> <div class="row"> <div class="col-sm-6"> <div class="menu p-sticky">menu or something</div> </div> <div class="col-sm-6 content-ex2"> Some content here</div> </div> </div> <div class="content-ex1"> Some content here</div>
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.