Author avatar

Vartika Chaudhary

Scrolling Inside a div in React

Vartika Chaudhary

  • Nov 4, 2020
  • 7 Min read
  • 230 Views
  • Nov 4, 2020
  • 7 Min read
  • 230 Views
Web Development
Front End Web Development
Client-side Frameworks
React

Introduction

Scrolling is defined as sliding-effect movement on images, text, or graphics across the computer display screen horizontally, vertically, or both. When developing web pages, you can enable scrolling by default for the complete webpage or only the areas where it is required.

The div is a tag element provided by the HTML language to reserve a particular part of the computer screen so that certain features can be added to the webpage. This makes the webpage work more efficiently and look more attractive to a user.

In this guide, you will learn about different types of scrolling inside the div tag and how to disable scroll movement when a popup appears.

Overview

Scrolling inside a div tag can be accomplished through three methods: smooth scrolling, single-direction scrolling, and bi-directional scrolling.

To understand all these scrolling methods, consider the following example: Suppose you want to build a webpage that contains basic information about birds in a text format. The webpage consists of three different divs. You will get to apply each scrolling type to each of these divs.

Smooth Scrolling

Smooth scrolling can be applied when there is a single-pixel increment in every scroll—that is, the scroll is so smooth that you can read as you scroll further.

Continuing the previous example: The first div contains the heading of the page and three buttons at the end. The first button directs you to the birds’ growth content, the second to their lifestyle content, and the third to their food intake content. When you click on any of these buttons, you are redirected to that content within the same webpage. Here, smooth scrolling is applied to the buttons for smooth redirection. Below is the code for creating the div and adding data into the div.

1
2
3
<body>
 <div id=”menu”></div>
</body>
html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Article_1 extends Component {
 render() {
  return(
   <div>
    <h1>BIRDS</h1>
    <br/>
    <button><a href="#growth">Birds: Growth</a></button>
    <button><a href="#lifestyle">Birds: Lifestyle</a></button>
    <button><a href="#food">Birds: Food</a></button>
   </div>
  )
 }
}

ReactDOM.render(<Article_1/>, document.querySelector('#menu'));
javascript

menu div

The smooth scrolling is achieved by placing scroll-behavior: smooth in the html inside the style tag.

1
2
3
html {
 scroll-behaviour: smooth;
}
css

smooth scroll

Single-Direction Scrolling

Single-direction scrolling can be applied when the scroll movement is either horizontal or vertical.

Continuing the previous example: When the first button, Birds: Growth, is clicked, it redirects to the content of the second div, which stores information about birds’ growth. The information in this div will be presented in a vertical overflow manner. Below is the code for creating the div and adding the information into it.

1
2
3
4
<body><div id=”growth”></div>
</body>
html
1
2
3
4
5
6
7
8
9
10
11
12
class Article_2 extends Component {
 render() {
  return(
   <div>
    <h2>Birds: Growth</h2>
    <p>..</p>
   </div>
  )
 }
}

ReactDOM.render(<Article_2/>, document.querySelector('#growth'));
javascript

before single-scroll

To fit all the text inside the div, the single-direction scrolling method will be used. You can apply it by placing the overflow-y:scroll in the id growth inside the style tag.

1
2
3
4
#growth {
 ...
 overflow-y: scroll;
}
css

after single-scroll

Notice the scroll bar on the right side of the div.

Bi-directional Scrolling

Bi-directional scrolling can be applied when the scroll movement is on both axie, horizontal and vertical.

Continuing the previous example: When the second button, *Birds: Lifestyle,* is clicked, it redirects to the content of third div, which stores the information about birds’ lifestyle. The information in this div will be represented in both a horizontal and vertical overflow manner. Below is the code for creating the div and adding the information into it.

1
2
3
4
5
<body><div id=”lifestyle”></div>
 ....
</body>
html
1
2
3
4
5
6
7
8
9
10
11
12
class Article_3 extends Component {
 render() {
  return(
   <div>
    <h2>Birds: Lifestyle</h2>
    <p>..</p>
   </div>
  )
 }
}

ReactDOM.render(<Article_3/>, document.querySelector('#lifestyle'));
javascript

before bi-scroll

To fit all the text inside the div, the bi-directional scrolling method will be used. You can apply it by placing the overflow:scroll and white-space:nowrap in the id lifestyle inside the style tag.

1
2
3
4
5
#lifestyle {.
 overflow: scroll;
 white-space: nowrap;
}
css

after bi-scroll

Notice the scroll bar at the bottom and right side of the div.

Disabling the Scroll Bar when a Popup Appears

A popups presents a dialog box that shows up on the computer screen whenever the webpage wants to give you a notification, alert, or confirmation.

Continuing the previous example: After reading all the information, you can exit the web page by clicking the Exit button. This opens up a dialog box that shows a goodbye message. While the pop-up is present on the screen, make sure that the scroll bar behind the webpage is disabled. Below is the code for creating the button and popup:

1
2
3
4
<body>
 ….
 <div id=”exit”></div>
</body>
html
1
2
3
4
5
6
7
8
9
function popup() {
 alert('Thank you for visiting!!');
}

const click = (
 <button onClick={popup}>Exit</button>
);

ReactDOM.render(click, document.querySelector('#exit'));
javascript

In the image below, the scroll bar is accessible while the popup is on the screen.

enable scroll

To stop the scrolling behind the dialog box, add addEventListener() and removeEventListener() functions to the popup() function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
function noscroll() {
 window.scrollTo(0,0);
}

function popup() {
 alert('Thank you for visiting');
 window.addEventListener("scroll", noscroll);
 window.removeEventListener("scroll", noscroll);
}

const click = (
 <button onClick={popup}>Exit</button>
);

ReactDOM.render(click, document.querySelector('#exit'));
javascript

disable scroll

Conclusion

As a frontend developer, scrolling is a basic feature you need on your webpages. The scrolling provided by default is applied to the whole webpage, whereas manually added scrolling is used to avoid the overflow of any div. Scroll movement can be applied to a single axis or both axes. You can also disable the scrolling feature so it doesn't interfere with a popup. Refer to information on the react-scroll component to learn more about animating vertical scrolling.

0