Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 598

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 62k
Author
Asked: November 25, 20242024-11-25T12:21:07+00:00 2024-11-25T12:21:07+00:00

Implementing Smooth Scroll Using Javascript ⚡

  • 62k

Hello Everyone 👋🏻, today we will be looking at how to implement smooth scrolling behavior using the scrollIntoView property.

Normally for the creation of Navbar, we use this code.

    <nav class="navbar" id="nav--bar">         <ul>             <li><a href="#1" class="link--a">DIV 1</a></li>             <li><a href="#2" class="link--a">DIV 2</a></li>             <li><a href="#3" class="link--a">DIV 3</a></li>         </ul>     </nav> 
Enter fullscreen mode Exit fullscreen mode

But when we click on the element it takes us to the element with the id provided in href. This is how a normal scrolling effect looks like 👇🏻

ezgif.com-gif-maker.gif

So what can we do for a smooth scrolling effect ?

HTML CODE

    <nav class="navbar" id="nav--bar">         <ul>             <li><a href="#1" class="link--a">DIV 1</a></li>             <li><a href="#2" class="link--a">DIV 2</a></li>             <li><a href="#3" class="link--a">DIV 3</a></li>         </ul>     </nav>     <section id="1">         <div class="div div--1">This is DIV 1</div>     </section>     <section id="2">         <div class="div div--2">This is DIV 2</div>     </section>     <section id="3">         <div class="div div--3">This is DIV 3</div>     </section>      <script src="script.js"></script> 
Enter fullscreen mode Exit fullscreen mode

CSS CODE

This code depends on personal choices as this will affect the design part!

body {   margin: 0;   display: flex;   flex-direction: column;   justify-content: space-around;   align-items: center; } .div--1 {   background-color: rgb(0, 132, 255); } .div--2 {   background-color: rgb(255, 136, 0); } .div--3 {   background-color: rgb(255, 0, 242); } .navbar ul {   display: flex;   justify-content: space-around;   background-color: rgb(179, 230, 245);   list-style: none;   padding: 1.5em;   border-radius: 12px;   border: 1px solid black; } .navbar ul li a {   background-color: yellow;   padding: 1em;   border-radius: 12px;   border: 1px solid black;   text-decoration: none; } .navbar ul li:hover {   background-color: orangered;   cursor: pointer; } .navbar {   width: 90%; } section {   width: 75%; } .div {   margin: 1em;   border-radius: 12px;   border: 1px solid black;   font-size: 5em;   height: 80vh;   display: flex;   align-items: center;   justify-content: space-around; }  
Enter fullscreen mode Exit fullscreen mode

JS CODE

The Most Important Part !!

document.getElementById('nav--bar').addEventListener('click', function (e) {     e.preventDefault();     const target = e.target;     if (target.classList.contains('link--a')) {         const id = target.getAttribute('href').slice(1);         document.getElementById(id).scrollIntoView({ behavior: 'smooth' });     } }); 
Enter fullscreen mode Exit fullscreen mode

Now let's Focus on JS code and understand how each line works!

  1. We are having this block of code at the top. This says that the document targets the element who is having the id “nav-bar” and adds an event listener on it which will listen to the event (click) on it. And if a click happens on that event then call the function which is having parameter e which will represent the event.
document.getElementById('nav--bar').addEventListener('click', function (e) { }); 
Enter fullscreen mode Exit fullscreen mode

2.The prevent Default function will prevent the auto-scrolling due to the anchor tag. And such help us to implement smooth scrolling. In the target variable, we will store the location at which point the click event happened so we can know that exactly on which link the user clicked.

e.preventDefault(); const target = e.target; 
Enter fullscreen mode Exit fullscreen mode

3.This if loop will help us to target only those clicks which are on elements having class “link–a”.

if (target.classList.contains('link--a')) { } 
Enter fullscreen mode Exit fullscreen mode

4.In variable id we will be saving the value given in href attribute of the element where the event (click) occurred. As we have given “#1” in href we need to cut the '#' and store the remaining value.

const id = target.getAttribute('href').slice(1); 
Enter fullscreen mode Exit fullscreen mode

5.This will now target the element which is having the id the same as the value stored in the id variable and apply the “scrollIntoView” method on it with smooth behavior as we mentioned in it.

document.getElementById(id).scrollIntoView({ behavior: 'smooth' }); 
Enter fullscreen mode Exit fullscreen mode

This will give us output like this 👇🏻

ezgif.com-gif-maker(1).gif

Great 🎉 We have successfully implemented the smooth scroll!

You can check the difference between both these effects will give professional look to your website!

Normal Smooth Scroll
ezgif.com-gif-maker.gif ezgif.com-gif-maker(1).gif

STAY TUNED FOR MORE 😄
GET CONNECTED WITH ME

Twitter
LinkedIn

javascriptwebdev
  • 0 0 Answers
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    ES6 - A beginners guide - Template Literals

    • 0 Answers
  • Author

    Understanding Higher Order Functions in JavaScript.

    • 0 Answers
  • Author

    Build a custom video chat app with Daily and Vue.js

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.