0

Simple scroll down -> change navbar sequence using jQuery.

$(document).ready(function () {
    'use strict';
     var c, currentScrollTop = 0,
     navbar = $('nav');
     $(window).scroll(function () {

        currentScrollTop = a;
        if (c > 650) {
          navbar.addClass("scrollUp");
          console.log("Showing navbar", c, navbar.hasClass("scrollUp"));

        } else if (c < 700) {
          navbar.removeClass("scrollUp");
        }
        c = currentScrollTop;
    });
}

Console log outputs current scroll and "false" where needed. Class just doesn't add up. Fyi I'm bad at jQuery and it's the only thing I have in my react app. scrollUp in CSS adds a background-color transition to nav, making it opaque. I noticed that sometimes it works, sometimes doesn't and I can't identify the problem. Haven't seen it working for a while now.

Bonus:

$("#toProjects").click(function() {
      $('html, body').animate({
          scrollTop: $("#projects").offset().top -75
      }, scrollSpeed);
      return false;
  });

This scrollTo function doesn't work as well.

HTML:

<head>
<script language="javascript" src="./jquery.js"></script>
</head>

CSS:

.navbar.scrollUp {
  background-color: #000000;
  transform: background-color 0s ease 0s;
}
9
  • Do you have some sample code hosted somewhere like codesandbox? Commented Apr 29, 2019 at 19:48
  • Does the nav element have navbar class already? You are accessing the element by "nav" but the css is applied to any element that has both .navbar and . scrollUp classes. Commented Apr 29, 2019 at 19:50
  • Also, is there a reason you're using jquery to select 'nav', but you refer to the class 'navbar' in your css? Commented Apr 29, 2019 at 19:50
  • currentScrollTop = a <-- what is a? I am sure the console is full of errors. Where is c being set? Commented Apr 29, 2019 at 20:03
  • @Mark It's live on rendemental.firebaseapp.com I used this code from some stackoverflow fella, who tried the same thing. It was working fine before and stopped just now. No idea why. Commented Apr 29, 2019 at 20:25

2 Answers 2

1

You're setting currentScrollTop to a but a is never defined.

Edit: I see you have different code on your website than you've posted here.
You're not connecting to your navbar correctly. Try this instead.

var c, currentScrollTop = 0, navbar = $('#navbar');

$('nav') is looking for elements with the name 'nav', of which, none exist on your page. same as calling $('div') or document.querySelector('div').

$('#navbar') grabs the elements on your page where the id attribute is set to navbar.

This fixes the class not being added part but I think you'll need to work with the css a bit to actually see the affect take place.

Sign up to request clarification or add additional context in comments.

2 Comments

Accidentally edited your post instead of replying. It's defined and it's not the problem
If this solved the issue please accept it as the answer.
0

The issue was solved by integrating jQuery code directly into ComponentDidMount() function inside React component. That way it re-engages and works fine

Comments

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.