1

This is the code where i would like to add 100% instead of pixels. Can anyone please help me with this code? I'm bad at javascript

function init() {
  window.addEventListener('scroll', function(e) {
    var distanceY = window.pageYOffset || document.documentElement.scrollTop,
      shrinkOn = 100,
      header = document.querySelector("header");
    if (distanceY > shrinkOn) {
      classie.add(header, "smaller");
    } else {
      if (classie.has(header, "smaller")) {
        classie.remove(header, "smaller");
      }
    }
  });
}
window.onload = init();
<header>
  <div class="navcontainer">
    <div id="logo"></div>
    <ul id="menu">
      <li data-menuanchor="firstPage"><a href="#firstPage">POČETNA</a>
      </li>
      <li data-menuanchor="secondPage"><a href="#secondPage">BIOGRAFIJA</a>
      </li>
      <li data-menuanchor="3rdPage"><a href="#3rdPage">RURALNI MOTIVI</a>
      </li>
      <li data-menuanchor="4thPage"><a href="#4thPage">REČNI MOTIVI</a>
      </li>
      <li data-menuanchor="5thPage"><a href="#5thPage">GRADSKI MOTIVI</a>
      </li>
      <li data-menuanchor="lastPage"><a href="#lastPage">KONTAKT</a>
      </li>
    </ul>
  </div>
</header>

8
  • Short answer no, you cannot convert to percentage for scroll positions. Compute the number 100% in pixels instead. Commented Oct 19, 2016 at 12:31
  • Can you please provide your html too, or any snippet Commented Oct 19, 2016 at 12:31
  • Are you looking to set shrinkOn = screen.height ? Commented Oct 19, 2016 at 12:37
  • Yes, 100% of screen height Commented Oct 19, 2016 at 12:38
  • that is 100% screen height :-) Commented Oct 19, 2016 at 12:40

1 Answer 1

1

You cannot use 100% but you can do it by using window.innnerHeight

function init() {
    window.addEventListener('scroll', function(e){
        var distanceY = window.pageYOffset || document.documentElement.scrollTop,
            shrinkOn = window.innerHeight,
            header = document.querySelector("header");
        if (distanceY > shrinkOn) {
            classie.add(header,"smaller");
        } else {
            if (classie.has(header,"smaller")) {
                classie.remove(header,"smaller");
            }
        }
    });
}
window.onload = init();
Sign up to request clarification or add additional context in comments.

9 Comments

Good call, window.innerHeight is probably better than screen.height as I was suggesting.
Thanks, it seem simple. I tried with shrinkOn = screen.height and shrinkOn = window.innerHeight and it almost works. The thing is i'm making one page website with full page scroll, so when i scroll to next page header doesn't change, only when i scroll one more time.
it looks like it's missing just one more pixel :)
Thanks for your help guys
Sorry, this code changes header afterwindow height, do you know maybe how can it change on screen height
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.