Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

153
Views
Changing JS code to work with other elements - HTML/CSS/JS

If I have the following JS code:

$(document).ready(function() {
  // Add smooth scrolling to all links
  $("a").on('click', function(event) {

    // Make sure this.hash has a value before overriding default behavior
    if (this.hash !== "") {
      // Prevent default anchor click behavior
      event.preventDefault();

      // Store hash
      var hash = this.hash;

      // Using jQuery's animate() method to add smooth page scroll
      // The optional number (800) specifies the number of milliseconds it takes to scroll to the specified area
      $('html, body').animate({
        scrollTop: $(hash).offset().top
      }, 800, function() {

        // Add hash (#) to URL when done scrolling (default click behavior)
        window.location.hash = hash;
      });
    } // End if
  });
});

This is the HTML:

<a href="#about">
  <div class="containerScroll">
    <div class="first-scroll"></div>
    <div class="second-scroll"></div>
  </div>
</a>

Right now the scrolling effect is working for all links with the tag a; however, can I change the $("a").on('click', function(event) { to something like $("scroll").on('click', function(event) {? Now, I'm hoping it will work for all the tags with scroll so now my HTML would look like:

<scroll href="#about">
  <div class="containerScroll">
    <div class="first-scroll"></div>
    <div class="second-scroll"></div>
  </div>
</scroll>

Would this work? Essentially, I want to add another tag instead of a because I do not want the scrolling effect to take place on all the links and only the ones I choose. This is why I tried changing the tag to scroll but it still did not work? Am I supposed to define a scroll css property for it to work?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Using jQuery for smooth scrolling is pointless nowadays

There are two methods (#2 is better IMO)

const element = document.querySelector('#something')
// element is the target you're trying to scroll to
  1. JS Method- Works just for the element you've selected
element.scrollIntoView({ behavior: "smooth" })
  1. CSS Method
/* in CSS */
html {
    scroll-behavior: smooth;
}
//in JS
element.scrollIntoView()
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!