简体   繁体   中英

Use ViewPort as units in JavaScript

I am looking to replace the "100" and "101" in the following script by a value in VH. I have tried everything that has come to mind but cannot figure it out, Google doesn't seem to have the answer either.

   $(document).ready(function() {

  $(window).scroll(function () { 
      console.log($(window).scrollTop())
    if ($(window).scrollTop() > 100) {
      $('#nav_bar').addClass('navbar-fixed');
    }
    if ($(window).scrollTop() < 101) {
      $('#nav_bar').removeClass('navbar-fixed');
    }
  });
});

Thank you.

$(document).ready(function() {
  $(window).scroll(function () { 
    console.log($(window).scrollTop())
    if ($(window).scrollTop() > $(window).height()) {
      $('#nav_bar').addClass('navbar-fixed');
    }
    if ($(window).scrollTop() < (101 * $(window).height())/100) {
      $('#nav_bar').removeClass('navbar-fixed');
    }
  });
});

$(window).height() returns the viewport's height.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM