Skip to main content

How to Scroll to an Element with Javascript

 

Overview

Scrolling to a specific element or part of a webpage is mainly done to keep a user intact with the webpage for a longer period. A scroll is a function that can be used also to scroll to the beginning of a webpage from the bottom of the webpage instantly. Instant scrolling functionality is done or implemented using JavaScript. Scrolling to a particular section of a webpage adds a single-click functionality without much intervention from the user.

There are different JavaScript scroll to element methods that are used to implement the functionality of scrolling to a particular element. These methods include:

  • JavaScript scrollIntoView method.
  • JavaScript scroll method.
  • JavsScript scrollTo method. In this article, we will understand them thoroughly.

Introduction

Before learning about the JavaScript scroll to element, Let's learn a bit about JavaScript.

JavaScript is an object-oriented programming language used on web pages. JavaScript is the web programming language used on both the client and server sides to provide dynamic and interactive behavior in a webpage. It is a lightweight and easy-to-understand programming language. JavaScript is the most popular programming language in the world. It is a scripting language of a webpage that can be used even by non-browser environments like NodeJsAdobe Acrobat, etc. Languages like HTML and CSS are used to provide structure and styling to a webpage respectively whereas JavaScript engages users by providing interactiveness to a webpage.

Now, let's learn about the Scroll function and the JavaScript scroll to element methods that implement the Scroll function.

Scrolling refers to functionality that moves the user from a specific part of the webpage to another. The scrolling in a website can be done by the physical components such as a scroll bar or with the help of buttons which are solely made to scroll in a specific element.

Scrolling to a specific element or part of a webpage is done using methods available in Javascript scroll to element which are as follows:

  • scrollIntoView() method
  • scrollTo() method
  • scroll() method

Now, we will discuss each of the above-mentioned methods used for scrolling with proper explanations and examples.

The ScrollIntoView Method

The scrollIntoView method in JavaScript is used to scroll to a specific element of a webpage in a web browser. Specifically, in the JavaScript scrollIntoView method, the scrolling is done based on the scrollable parent element. In general, most of the time the parent element will be the window of the web browser but in some of the scenarios, When the required scrollable element is placed within another scrollable element then the scrolling is done concerning the parent element rather than the window of the web browser's window. The scrollIntoView() method helps to carry out the scrolling of an element into the visible area of the browser window.

Now, let's see the syntax of scrollintoview method.

Syntax

element.scrollIntoView();

Parameter

There are several customizations available for the scrollIntoView() method which is done by passing scrollIntoViewOptions as the parameter.

scrollIntoView method takes a parameter which is known as the scrollIntoViewOptions. The scrollIntoViewOptions object contains the following customization properties:

  • Behaviour :- This property of scrollIntoViewOptions tells about the scrolling behavior. Whether the scrolling is done using scrollintoview method should contain an animation or there should be a normal instant scrolling without any animation up to the specific element. This property contains the following values which can be passed with the parameter:

    • smooth: This value specifies smooth scrolling that is the scrolling with animation.
    • auto: This value specifies instant scrolling to the required element without any animation.
  • Inline :- This property of scrollIntoViewOptions tells about the alignment mainly horizontal alignment of the required element concerning the parent scrollable element when using the scrollintoview method. This property contains the following values which can be passed with the parameter:

    • start: This value specifies that the required element is aligned to the left or start of the parent scrollable element.
    • center: This value specifies that the required element is aligned to the center of the parent scrollable element.
    • end: This value provides that the required element is aligned at the end or right of the parent scrollable element.
    • nearest: This value specifies that the element should be aligned according to the situation. If the element is at the right or the end then the required elements will also be aligned at the end. Similarly, if the element is at the left or the starting then the required elements will also be aligned at the left.
  • Block :- This property of scrollIntoViewOptions tells about the vertical alignment of a required element concerning the parent scrollable element when using the scrollintoview method. This property contains the following values which can be passed with the parameter:

    • start: This value specifies that the required element is aligned at the top or start of the parent scrollable element.
    • center: This value specifies that the required element is aligned to the center of the parent scrollable element.
    • end: This value specifies that the required element is aligned at the bottom of the parent scrollable element.
    • nearest: This value specifies that the element should be aligned according to the situation. If the element is at the top then the required elements will also be aligned at the top. Similarly, if the element is at the bottom then the required elements will also be aligned at the bottom.

Now, let's take an example to understand the scrollIntoView() method with proper implementation and explanation.

Code:

<!DOCTYPE html>
  <html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Scroll Method in JS</title>
  </head>
  <style>
    #scroll {
      height: 450px;
      width: 350px;
      overflow: auto;
      background: rgb(243, 242, 245);
    }

    #content {
      margin: 500px;
      height: 800px;
      width: 2000px;
      background-color: rgb(250, 245, 243);
    }
  </style>

  <body>
    <h1>The scrollIntoView() Method</h1>
    <button onclick="scrollFunction()">Scroll</button>

    <p>Click on the "Scroll" button</p>

    <div id="scroll">
      <div id="content">
        <p>scrollIntoView() method is used.</p>
      </div>
    </div>

    <script>
      function scrollFunction() {
        const element = document.getElementById("content");
        element.scrollIntoView();
      }
    </script>
  </body>
</html>

Output:

example scrollintoview method


Comments

Popular posts from this blog

C# Generic class to parse value - "GenericConverter"

    public class GenericConverter     {         public static T Parse<T>(string sourceValue) where T : IConvertible         {             return (T)Convert.ChangeType(sourceValue, typeof(T));         }         public static T Parse<T>(string sourceValue, IFormatProvider provider) where T : IConvertible         {             return (T)Convert.ChangeType(sourceValue, typeof(T), provider);         }     }     public static class TConverter     {         public static T ChangeType<T>(object value)         {             return (T)ChangeType(typeof(T), value);         }         public static object ChangeType(Type t, object value)         {             TypeConverter tc = TypeDescriptor.GetConverter(t);             return tc.ConvertFrom(value);         }         public static void RegisterTypeConverter<T, TC>() where TC : TypeConverter         {             TypeDescriptor.AddAttributes(typeof(T), new TypeConverterAttribute(typeof(TC)));         }     } ----------------

Tip/Trick: Fix Common SEO Problems Using the URL Rewrite Extension

Search engine optimization (SEO) is important for any publically facing web-site.  A large % of traffic to sites now comes directly from search engines, and improving your site’s search relevancy will lead to more users visiting your site from search engine queries.  This can directly or indirectly increase the money you make through your site. This blog post covers how you can use the free Microsoft  URL Rewrite Extension  to fix a bunch of common SEO problems that your site might have.  It takes less than 15 minutes (and no code changes) to apply 4 simple  URL Rewrite  rules to your site, and in doing so cause search engines to drive more visitors and traffic to your site.  The techniques below work equally well with both ASP.NET Web Forms and ASP.NET MVC based sites.  They also works with all versions of ASP.NET (and even work with non-ASP.NET content). [In addition to blogging, I am also now using Twitter for quick updates and to share links. Follow me at:  twitter.com/scottg

How to create a countdown timer in jquery

Create a countdown timer in jQuery First we need to include the jQuery library file to the HTML page to perform this task. To do that we need to understand that what exactly a jQuery library fie is ? JQuery library file is the library of JavaScript, which means this file contains the predefined functions of jQuery. We just need to call these functions to perform the task. jQuery functions reduces the lines of code and makes our task easy. As this jQuery library file contains the javascript functions so we need to call the function within <script> </script> tag. Now after including the file, we need to define a variable which will store that for how long you want the timer on the page(c=60) and now the time you set needs to be changed in hours , minutes and seconds using the code “ var hours = parseInt( time / 3600 ) % ;var minutes = parseInt( time / 60 ) % 60; var seconds = time % 60;” Now we need to put the condition if timer got finished (if (t