0

hey I'm having a little issue. Right now my navigator bar just take me to sections on my page. I did it using id which will connect the a tag to a specific section. I get to see the # char with the section name on the url for example:

http://orelhindi.s3-website.us-east-2.amazonaws.com/#skills

is there a way to make it dissapear?

  <div class="menu">
        <ul>
          <li><a href="#about">About</a></li>
          <li><a href="#cv">cv</a></li>
          <li><a href="#skills">skills</a></li>
          <li><a href="#portfolio">portfolio</a></li>
          <li><a href="#contact">contact</a></li>
        </ul>
      </div>
      ....
       <section id="skills" data-aos="fade-right">
      <ul>
        <li><i class="fab fa-java"></i></li>
        <li><i class="fab fa-js-square"></i></li>
        <li><i class="fab fa-node"></i></li>
        <li><i class="fab fa-angular"></i></li>
        <li><i class="fab fa-html5"></i></li>
        <li><i class="fab fa-css3-alt"></i></li>
        <li><i class="fab fa-git"></i></li>
        <li><i class="fab fa-aws"></i></li>
      </ul>
    </section>

thanks a lot!

rio
  • 757
  • 5
  • 19
  • 32

2 Answers2

1

Technically yes: How to modify the URL without reloading the page?. Or you need to implement the scroll in JavaScript, making your life slightly more difficult.

And the # is useful, if someone shares the link anyone clicking on it will scroll to the correct place on the page.

Bob
  • 614
  • 1
  • 7
  • 19
1

Alternative to Bob's answer. You can use JS to listen for the clicks and scroll the element into view.

  1. Change all href to data-section-id
  2. Using JS: query those a[data-section-id] elements
  3. Add click event listener
  4. Find the element with same id as data-section-id, and scroll it into view

Worth noting the support for scrollIntoView.

// When DOM is ready
document.addEventListener("DOMContentLoaded", function() {
    
 // Get all `a` elements with `data-section-id`
 document.querySelectorAll("a[data-section-id]").forEach(aElement => {
     
  // Add click event listener
  aElement.addEventListener("click", event => {
      
   // Store the element with `id` matching `data-section-id`
   const scrollToElement = document.getElementById(event.target.dataset.sectionId);

   // If element is not null, scroll to it
   if (scrollToElement !== null) {
    scrollToElement.scrollIntoView();
   }
  });
 });
});
<div class="menu">
        <ul>
          <li><a data-section-id="about">About</a></li>
          <li><a data-section-id="cv">cv</a></li>
          <li><a data-section-id="skills">skills</a></li>
          <li><a data-section-id="portfolio">portfolio</a></li>
          <li><a data-section-id="contact">contact</a></li>
        </ul>
      </div>
      ....
       <section id="skills" data-aos="fade-right">
      <ul>
        <li><i class="fab fa-java"></i></li>
        <li><i class="fab fa-js-square"></i></li>
        <li><i class="fab fa-node"></i></li>
        <li><i class="fab fa-angular"></i></li>
        <li><i class="fab fa-html5"></i></li>
        <li><i class="fab fa-css3-alt"></i></li>
        <li><i class="fab fa-git"></i></li>
        <li><i class="fab fa-aws"></i></li>
      </ul>
    </section>
Gary Thomas
  • 2,291
  • 1
  • 9
  • 21