Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Craig Watson
Craig Watson
27,930 Points

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