0

I know there have been a variety of answers to similar questions, but my JS skills are so poor I cannot really get the answers to work in my specific example. I have the following script on my WordPress site which tells the navigation bar to change color once it hits the #startchange CSS ID when scrolling down the page.

jQuery(document).ready(function( $ ) {      
   var scroll_start = 0;
   var startchange = $('#startchange');
   var offset = startchange.offset();
   $(document).scroll(function() { 
      scroll_start = $(this).scrollTop();
      if(scroll_start > offset.top) {
          $('#main-header').css('background-color', 'rgba(28, 48, 64, 0.4)');
       } else {
          $('#main-header').css('background-color', 'transparent');
       }
   });
});

This works perfectly on the page that I want it implemented on (the landing page) but for the blog posts pages, the background is white and therefore the navbar is invisible. What modification do I need to do to this script to tell it to only function on the landing page, or alternatively what can be done to the script to tell it that when it is a blog post page that a certain CSS class be applied to it?

2
  • Just add the script to one page, if its in an external file referenced by every page then you will have to remove the reference and add what you need Commented Dec 14, 2017 at 11:27
  • You could put a class on the container of whatever page you want this script to run on, then check for the existence of that class before attaching the scroll event handler. Commented Dec 14, 2017 at 11:30

3 Answers 3

2

As suggested, you can just add the script directly to that one page (there's likely a good plugin to do that... for example, Elementor gives you an option to insert javascript block among other things)

Or, just wrap it in an if statement that checks for the page-id-## class you need:

jQuery(document).ready(function( $ ) {    
  // replace the XX with your page id
  if (document.body.classList.contains('page-id-XX')) { /
    var scroll_start = 0;
    var startchange = $('#startchange');
    var offset = startchange.offset();
    $(document).scroll(function() { 
       scroll_start = $(this).scrollTop();
       if(scroll_start > offset.top) {
           $('#main-header').css('background-color', 'rgba(28, 48, 64, 0.4)');
        } else {
           $('#main-header').css('background-color', 'transparent');
        }
    });
  }
});
Sign up to request clarification or add additional context in comments.

Comments

0

You can create an if statement checking the path of the page like this:

if (top.location.pathname === '/x/x/path')
{

}

2 Comments

Thanks Tomm, but I need to clarify that my JS skills are less than zero. Where would I place that in the above code?
@TimmehNZ you just place your jquery in the if statement and add your pathname in the placeholder
0

Something like this but not being familiar with Wordpress it would be better just to implement the JS for just the page you require it.

jQuery(document).ready(function( $ ) {    
   var pageName = location.pathname.substring(location.pathname.lastIndexOf("/") + 1);
   if(pageName != null && pageName.toLowerCase() == "somepagename.php") {
       var scroll_start = 0;
       var startchange = $('#startchange');
       var offset = startchange.offset();
       $(document).scroll(function() { 
          scroll_start = $(this).scrollTop();
          if(scroll_start > offset.top) {
              $('#main-header').css('background-color', 'rgba(28, 48, 64, 0.4)');
           } else {
              $('#main-header').css('background-color', 'transparent');
           }
       });
   }
});

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.