How to exchange the position (css) attribute of a div

Asked

Viewed 1,193 times

1

I have a div of id = 'retangulo' with position:absolute;. When the page loads, this vertical rectangle only appears in half. I want when the person rolls the site at a time when the whole rectangle appears, he gets the position:fixed; always following the movement of the screen.

header{
  width:100%;
  height:150px;
  background-color:#423142;
}

div#retangulo{
  width:50px;
  height:400px;
  background-color: green;
  position:absolute;
  top:220px;
  left:100px;
}

footer{
  height:200px;
  width:100%;
  background-color:red;
  position:absolute;
  top: 1500px;
}
<header></header>
<div id="retangulo">
  {texto exemplo....}
</div>
<footer></footer>

Exemplifying with the fiddle: https://jsfiddle.net/pjegfkrz/

Following the example: As you can notice, when opening the example, we see that we are not seeing completely the div of the rectangle, but there is a moment when rolling down you can see the whole rectangle. At that point, I’d like the rectangle to have the position:fixed; accompanying the screen.

  • 2

    Could share your code or provide a Fiddle?

  • You want when the user decreases the screen to change to position:fixed; ?

  • This, when the user scrolls down to a certain height, the position stay absolute, but when it comes to that, switch to position:fixed;

  • I still don’t understand what you want. I could explain a little more?

2 answers

2

Assuming you don’t know the offset (position in the scroll of your element) we first need to discover it:

//detecta scroll na janela
$(window).on("scroll", function() {

    var retangulo = $('#retangulo');
    //obtem a posição da div com relação ao scroll
    var posicao = retangulo.offset();

    //define se o scroll atual é maior ou igual a posição da div
    if($(window).scrollTop() >= posicao.top) {
      $("#retangulo").css({"position" : "fixed"});
    }else{
       $("#retangulo").css({"position" : "absolute"});
    }
  });

Basically each time the user scrolls on the page this function will repeat itself and check if the height of the current scroll is greater or equal to that of its element, if it is it will have position Fixed, and if it is not it will return to position Absolute. I didn’t get to test it, check it out

  • This function did not work very well. When I scrolldow, the object keeps locking between the top and the bottom.

  • In defining the position:fixed you can define where you want the element to be using for example top:0 will crash the element at the top, if you leave it fixed on not setting a position the same should tend to bug even using this function

0

You can use media query for this. When it reaches the width of 700px and height of 600px, the position will change from absolute to fixed. You can set the other properties on it as well. Everything you set within that scope will be applied when decreasing the screen.

@media screen and (max-width:700px), screen and (max-height: 600px) {
  div#retangulo{
    width:50px;
    height:200px;
    background-color: green;
    position:fixed;
    top:100px;
    left:100px;
  }
}

header{
  width:100%;
  height:150px;
  background-color:#423142;
}

div#retangulo{
  width:50px;
  height:400px;
  background-color: green;
  position:absolute;
  top:220px;
  left:100px;
}

footer{
  height:200px;
  width:100%;
  background-color:red;
  position:absolute;
  top: 1500px;
}

@media screen and (max-width:700px), screen and (max-height: 600px) {
  div#retangulo{
    width:50px;
    height:200px;
    background-color: green;
    position:fixed;
    top:100px;
    left:100px;
  }
}
<header></header>
<div id="retangulo">
  {texto exemplo....}
</div>
<footer></footer>

See working on Jsfiddle.

  • I don’t understand why it’s max-width:700px and max-height:600px

  • This is the screen size, you decide the height and width you want. It was just a suggestion.

  • But that way you’ll always stay position:fixed;

  • Yes, whenever the screen is in the size you set in the media, the CSS will be the one set within it.

Browser other questions tagged

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