JavaScript - scrollBy() method

revision:


Category : element

The scrollBy() method of the Element interface scrolls an element by the given amount.

Syntax :

        scrollBy(x-coord, y-coord)
        scrollBy(options)
    

Parameters:

x-coord : the horizontal pixel value that you want to scroll by.

y-coord : the vertical pixel value that you want to scroll by.

options : A dictionary containing the following parameters:

top : specifies the number of pixels along the Y axis to scroll the window or element.
left : specifies the number of pixels along the X axis to scroll the window or element.
behavior : determines whether scrolling is instant or animates smoothly. This option is a string which must take one of the following values:

smooth: scrolling should animate smoothly.
instant: scrolling should happen instantly in a single jump.
auto : scroll behavior is determined by the computed value of scroll-behavior.

Examples:

            // scroll an element
            element.scrollBy(300, 300);

            element.scrollBy({
                top: 100,
                left: 100,
                behavior: "smooth",
            });

        

Practical examples

example: scroll the document up and down.

Click one of the buttons (multiple times) to scroll the document.

Look at the scrollbar to see the effect.





code:
                    <div>
                        <p>Click one of the buttons (multiple times) to scroll the document.</p>
                        <p>Look at the scrollbar to see the effect.</p>
                        <button onclick="scrollWin(0, 50)">Scroll down</button><br><br>
                        <button onclick="scrollWin(0, -50)">Scroll up</button><br><br>
                    </div>
                    <style>
                        body {height: 2100px;}
                        button { position: relative;}
                    
                    </style>
                    <script>
                        function scrollWin(x, y) {
                            window.scrollBy(x, y);
                        }
                                                        
                    </script>
                

example: scroll the document right and left.

Click one of the buttons (multiple times) to scroll the document.

Look at the scrollbar to see the effect.





code:
                    <div>
                        <p>Click one of the buttons (multiple times) to scroll the document.</p>
                        <p>Look at the scrollbar to see the effect.</p>
                        
                        <button onclick="scrollWin1(100, 0)">Scroll right</button><br><br>
                        <button onclick="scrollWin1(-100, 0)">Scroll left</button><br><br>
                    </div>
                    <style>     
                        body {width: 2100px;}
                        button { position: relative;}
                </style>
                    <script>
                        function scrollWin1(x, y) {
                        window.scrollBy(x, y);
                        }
                                    
                    </script>
                    
                

example:
code: