floating div accompany page scroll

Asked

Viewed 8,310 times

3

Would anyone have any SIMPLIFIED idea or example of how to make a floating div roll together with the scroll of the page ? Something very simple, for understanding and learning, because the ones that exist here are very confusing and I’ve been looking for this model for some time. There are those floating div s that are on the right side of the page and as the page is scrolled, the same div accompanies until the footer or until a certain point. It is just that. Who can share, I thank.

2 answers

3


I don’t know if this is exactly what you need, but only with CSS you can do it using position:sticky or position:fixed or If you want to see examples over the internet you can search for "css persistent div"

See the example with position:sticky: (in this example the box starts at 120px from the top and at 50px from the top)

body {
    height: 200vh;
    background-image: url(http://placecage.com/500/500);
}
.teste {
    background-color: red;
    width: 100px;
    height: 50px;
    margin-top: 140px; /* altura que está do topo */
    top: 50px; /* altura que vai parar antes do topo */
    position: sticky;
}
<div class="teste"></div>


See the example with position:fixed:

body {
    height: 200vh;
    background-image: url(http://placecage.com/500/500);
}
.box {
    width: 100px;
    height: 100px;
    position: fixed;
    z-index: 1000;
    left: 1rem;
    top: 1rem;
    background-color: #f00;
}
<div class="box"></div>

Here’s Mozilla’s documentation on positioning elements with CSS https://developer.mozilla.org/en-US/docs/Web/CSS/position

  • Cool, I get it, now the question is: Do I have to run this div through "certain space" ... <div class="Row"> <div class="col-Md-8"> <p> fixed content here </p> <div class="col-Md-8"> <p> fixed content here </p> </div> <div class="col-Md-4"> <p>content of floating div here</p> </div> </div> I have this div col-Md-4(right side) to run between the space of the two Divs col-Md-8 (one on top of the other), I will have to make with JS the adjustment between them ?

  • 1

    I got @hugocsl, it worked! I followed your css and it worked when it came to applying the class with the Divs inside Row. Thanks!

  • @Eitaehnoiz Nice young that it worked!

0

Dude I made an example well even basic, I think more basic than this impossible. scroll the slider to see the result. I only used the property display with the value Fixed.

div {
  height:900px;
}
.fixed {
  position: fixed;
  background-color: red;
}
<div>
  <h1 class="fixed">TESTE</h1>
</div>

  • Thank you Leandro !

  • For nothing man!!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.