Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript

Function only triggers on refresh ??

Hi everyone!!

This function works but i have to scroll down the page and refresh to see the affects and vice versa ...

//Nav color change on scroll 
var $scrollHeight = $(window).scrollTop();

$(function (navColorToggle) {
    if ($scrollHeight > 80 ) {
        $(".navbar").addClass("scrollNav");
    }
});

Please can somebody explain why / am i missing a line of code to call the function or have i muddled this up in a spectacular way .. lol

Craig

1 Answer

Hi Craig,

I'm not sure I understand the problem you're trying to solve, but here's what I cooked up. Perhaps this can help point you in the right direction.

$(function() {
  // avoiding prefixing with $, because this will just hold a numeric value, not a jQuery object
  var scrollHeight;

  $(window).on('mousewheel', function() {
    scrollHeight = $(this).scrollTop();
    console.log("That's how we scroll. " + scrollHeight);

    if (scrollHeight > 80) {
      $(".navbar").addClass("scrollNav");
    } else {
      $(".navbar").removeClass("scrollNav");
    }
  });
});

Cheers