Calculate height difference with CSS

Asked

Viewed 205 times

-1

There’s a way for me to do the code below, which is in Jquery, but with CSS?

 $(document).scroll(function() {

    if (  $(this).scrollTop() > $(window).height() ) {

        $("section#topo").css("height", "10vh");
        $("section#topo").css("background", "#999");

    } else {

        $("section#topo").css("height", "7vh");
        $("section#topo").css("background", "none");

    }

});

The idea would be something like:

<style>

    calc (body.scrollTop > window.height) {
        section#topo {
            height: "10vh";
            background: #999;
        }
    }

    calc (body.scrollTop <= window.height) {
        section#topo {
            height: "7vh";
            background: none;
        }
    }

</style>
  • I don’t understand why the negatives are a valid question. But to answer: no, not that way. As simple as it is you’ll have to use a little JS.

  • @Renangomes, Thanks for the support. I don’t have much experience with CSS. But I figured there was a way around this problem! I researched a lot before posting the question and found nothing. Who knows anyone here j[a has gone through this and found a solution. On the negatives, call no! Only serve to disturb those who have less knowledge and can not even formulate your question!

1 answer

6


You can solve half the problem with just CSS, maybe I can help you in some way.

As in jQuery to realize that you want any movement of scroll the top section change, I imagine that this section should be pasted at the top of the page...

In this example I’m using position:sticky with negative value in the father, and with positive value in the son, so you get a very interesting effect. Only with CSS!

About changing a color of background only with CSS when making scroll, this is not yet possible as CSS does not detect events from scroll, but js yes...

inserir a descrição da imagem aqui

html, body {
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
}

body {
  height: 200vh;
}

.box {
  height: 105px;
  background-color: red;
  position: sticky;
  top: -50px;
}
.box .teste {
  position: sticky;
  top: 40px;
  display: inline-block;
  border: 1px solid #000;
  margin-bottom: 20px;
}
<div class="box">
  <span class="teste">
    teste
  </span>
</div>

  • Hugo. Interesting your solution. But I think it will be better to keep the jQuery. But still, for the effort in helping, I will accept your solution! Again! Thank you so much for the support!

  • @Carlosrocha was worth it my dear! Really in this situation only with jQuery same, because the CSS does not detect moves in scroll, ai is not possible to put or remove classes or properties with this type of interaction. What you can do is treat this with pure Javascript, without using jQuery. This type of code is not complex to do with pure JS no, and you don’t need to import jQuery just for that. Vlw

Browser other questions tagged

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