DEV Community

Rafael Borges
Rafael Borges

Posted on

How to create a dynamic, smooth-scrolling menu

I am using react but is possible to use in all projects that use javascript how language.

To achieve this effect, you can implement a container div that splits the screen into sections with Id in each element:

//create a page

      <div className="container">
        <div className="content">
          <div id="Profile">
           <h1>Profile Content</h1>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</p>
          </div>
          <div id="ExperiencesContainer">
            <h1>Experiences Content</h1>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</p>
          </div>
          <div id="AboutMe">
            <h2>About me</h2>
            <h3>I work with what I love</h3>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</p>
          </div>
        </div>
      </div>

Enter fullscreen mode Exit fullscreen mode

Our HTML Menu could be:

<div className="containerMenu">
                <button onClick={() => handleNavigateMenu('Profile')}>Profile</Tab>
                <button onClick={() => handleNavigateMenu('AboutMe')}>About me</Tab>
                <button onClick={() => handleNavigateMenu('Experiences')}>Experiences</Tab>
</div>
Enter fullscreen mode Exit fullscreen mode

and this function from onClick:

//define this interface to use menu items without errors when call function handleNavigateMenu
export interface IMenuItems {
    items:
    | 'Profile'
    | 'Experiences'
    | 'AboutMe'
}

const handleNavigateMenu = (menuItems: IMenuItems['items']) => {

  const element = document.getElementById(menuItems) //created a element
  if(element) {
    element.scrollIntoView({ behavior: 'smooth' }) //scrolling to element
  }
}
Enter fullscreen mode Exit fullscreen mode

Postgres on Neon - Get the Free Plan

No credit card required. The database you love, on a serverless platform designed to help you build faster.

Get Postgres on Neon

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay