V
V
Vladimir Novitsky2015-10-09 03:21:32
JavaScript
Vladimir Novitsky, 2015-10-09 03:21:32

How to make .scrollTop() as a percentage?

Hello.
Is it possible to bind .scrollTop()to percentages (preferably vh) rather than pixels?

For example code:

$(document).ready(function() {
  var $header = $("#header");

  $(window).scroll(function() {
    if ($(this).scrollTop() > 700 && $header.hasClass("transparent")) {
      $header.removeClass("transparent").addClass("opacity");
    } else if ($(this).scrollTop() <= 700 && $header.hasClass("opacity")) {
      $header.removeClass("opacity").addClass("transparent");
    }
  }); //scroll
});
With it, classes are added or removed depending on the scroll. In this case, you need to scroll 700px to change it. I need the metering to take place in percent (or vh). That is, for example, if I scrolled 80% of the window (or viewport), then the class changes.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
kapuletti, 2015-10-09
@Novitsky

The height of the document ( $(document).height() ) is divided by 100, multiplied by the desired % scroll.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question