1

I tried to transform the next piece of JS code into JQuery but It doesn't work. Could you help ?

function initUpdateNavbarOnScroll() {
  var navbarc = document.querySelectorAll('#site-navigation-wrap .dropdown-menu>li>a');
  window.addEventListener('scroll', () => {
    if (window.scrollY >= window.innerHeight) {
      navbarc.classList.add('darklinker');
    } else {
      navbarc.classList.remove('darklinker');
    }
  });
}

initUpdateNavbarOnScroll();

I've made different try if it helps but any of them works...

// ​$( "#site-navigation-wrap .dropdown-menu>li>a" ).on( "scroll", function( event ) {
//     var navbarc = $( this );
//     if (window.scrollY >= window.innerHeight) {
//       navbarc.addClass('darklinker');
//     } else {
//       navbarc.removeClass('darklinker');
//     }
// });​​​​​​​​​​​​​​​​​​​​​

// $( window ).scrollY(function() {
//   if ($( "site-navigation-wrap .dropdown-menu>li>a" )) {
//     navbarc.addClass('darklinker');
//   } else {
//     navbarc.removeClass('darklinker');
//   }
// });

Thx.

5
  • 2
    if the original works, why do you want to do this? That is so very odd!! Commented Apr 24, 2018 at 17:46
  • if ($( "site-navigation-wrap .dropdown-menu>li>a" )) this will always return true as jQuery() returns an object even if no elements are selected. Also there is no jQuery function named scrollY() unless you are using some plugin library. Commented Apr 24, 2018 at 17:47
  • @RandyCasburn needs more jquery Commented Apr 24, 2018 at 17:50
  • @ASDFGerte - Ah! I see bwahaha Thanks for that Commented Apr 24, 2018 at 18:05
  • Well, the problem is that the querySelectorAll doesn't work but the querySelectordo his job. But I want to select them all... I asked to the theme owner and he told me to write it in JQuery because it's load in the theme. They should have a conflict somewhere, i think but don't know where exactly... Commented Apr 26, 2018 at 7:49

2 Answers 2

1
function initUpdateNavbarOnScroll() {
    var navbarc = $("#site-navigation-wrap .dropdown-menu>li>a");

    window.addEventListener('scroll', () => {
      if (window.scrollY >= window.innerHeight) {
        navbarc.addClass('darklinker');
      } else {
        navbarc.removeClass('darklinker');
      }
    });
}

initUpdateNavbarOnScroll();
Sign up to request clarification or add additional context in comments.

Comments

0

But why mannnn???????

function initUpdateNavbarOnScroll() {
    var navbarc = $('#site-navigation-wrap .dropdown-menu>li>a');

    $(window).on('scroll', function(){
        if( $(window).scrollTop() >= $(window).innerHeight() ) {
            navbarc.addClass('darklinker);
        } else {
            navbarc.removeClass('darklinker);
        }
    });
}

1 Comment

Hi Tom, I loaded the code with the missing ' ' -> ` function initUpdateNavbarOnScroll() { var navbarc = $('#site-navigation-wrap .dropdown-menu>li>a'); $(window).on('scroll', function(){ if( $(window).scrollTop() >= $(window).innerHeight() ) { navbarc.addClass('darklinker'); } else { navbarc.removeClass('darklinker'); } }); } initUpdateNavbarOnScroll(); ` But it doesn't work. Basicly I asked for JQuery because the querySelectorAlldidn't work (versus the querySelectorwho's working) But I want to select them all

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.