samedi 25 avril 2015

How do I check if the current url or path is equal to a certain link and if not, append a forward slash to the href in the nav bar?


I am a JavaScript and jQuery newby and have some issues with page scroll being directed from different page. The work is being done in WordPress PHP. I am currently using page-scroll where the href=#id and I changed it to href=/#id where the slash allows for the directing properly. The issue is if I am on the current page of home. the forward slash now jumps rather than page-scrolls. My thinking is leave the href=#id without the slash and have it check if the current url != to home, then append a forward slash to href. Help is genuinely appreciated!

This allows for page scroll from current page and doesn't direct from Pricing & Services page

    <li><a class="page-scroll" href="#howitworks">How It Works</a></li>

Adding the "/" allows for directing from Pricing& Services page however it jumps if I am on the homepage. I want it to jump if I am not on the homepage. I would like for it to smooth scroll on current page.

    <li><a class="page-scroll" href="/#aboutus">About Us</a></li>

Page I am directing from.

    <li><a class="page-scroll" href="pricing-services">Pricing & Services</a></li>

This is the javascript code I am using which requires the Jquery Easing plugin

    $(function() {
      $('a.page-scroll').bind('click', function(event) {
       var $anchor = $(this);
       $('html, body').stop().animate({
        scrollTop: $($anchor.attr('href')).offset().top-50
       }, 1500, 'easeInOutExpo');
       event.preventDefault();
      });
    });


Aucun commentaire:

Enregistrer un commentaire