Table of Contents
Section 4
Home Web Front-end CSS Tutorial How to implement a smooth scrolling navigation menu on a web page through pure CSS

How to implement a smooth scrolling navigation menu on a web page through pure CSS

Oct 20, 2023 pm 04:22 PM
css Navigation menu smooth scrolling

How to implement a smooth scrolling navigation menu on a web page through pure CSS

How to implement a smooth scrolling navigation menu on a web page through pure CSS

Guiding users to navigate in a web page is an important design element, and the smooth scrolling navigation menu is provided A way to create a user-friendly navigation experience. This article will introduce how to implement such a navigation menu through pure CSS and provide corresponding code examples.

1. HTML structure

First, we need to create the basic structure of the navigation menu in HTML. Here is a simple example:

<nav class="smooth-scroll-nav">
  <ul class="smooth-scroll-menu">
    <li><a href="#section1">Section 1</a></li>
    <li><a href="#section2">Section 2</a></li>
    <li><a href="#section3">Section 3</a></li>
    <li><a href="#section4">Section 4</a></li>
  </ul>
</nav>

<section id="section1">
  <h2 id="Section">Section 1</h2>
  <p>This is the content of section 1.</p>
</section>

<section id="section2">
  <h2 id="Section">Section 2</h2>
  <p>This is the content of section 2.</p>
</section>

<section id="section3">
  <h2 id="Section">Section 3</h2>
  <p>This is the content of section 3.</p>
</section>

<section id="section4">
  <h2 id="Section">Section 4</h2>
  <p>This is the content of section 4.</p>
</section>
Copy after login

In the above example, we created a <nav> element as a container for the navigation menu and then added a inside it. The <ul> element serves as a list of menus, with corresponding menu items added. Next, we use the <section> element to create the content for each section, where the id attribute corresponds to the href attribute in the menu item.

2. CSS Style

Next, we will use CSS to create a smooth scrolling effect. First, we need to apply some basic styles on the navigation menu:

.smooth-scroll-nav {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  background-color: #333;
  padding: 10px 0;
}

.smooth-scroll-menu {
  list-style: none;
  margin: 0;
  padding: 0;
  text-align: center;
}

.smooth-scroll-menu li {
  display: inline-block;
  margin-right: 15px;
}

.smooth-scroll-menu li:last-child {
  margin-right: 0;
}

.smooth-scroll-menu a {
  color: #fff;
  text-decoration: none;
  padding: 5px 10px;
}

.smooth-scroll-menu a:hover {
  background-color: #fff;
  color: #333;
}
Copy after login

In the above code, we added the position: fixed; attribute to the navigation menu container to make it fixed. top of the page. Then, set the corresponding background color, padding, font color and other styles. Next, we use the inline-block layout to display the menu items horizontally and add some spacing and hover effects.

Next, we will add a smooth scrolling effect. We will use the scroll-behavior property of CSS to achieve this effect. In some cases, browsers may not support this attribute, so we also need to provide a fallback solution for browsers that do not support it.

html {
  scroll-behavior: smooth;
}

@media screen and (-webkit-min-device-pixel-ratio:0) {
  /* 回退方案:使用jQuery滚动动画 */
  .smooth-scroll-nav a {
    transition: all 0.3s ease-in-out;
  }
  
  .smooth-scroll-nav a[href^="#"] {
    position: relative;
  }
  
  .smooth-scroll-nav a[href^="#"]:after {
    content: "";
    position: absolute;
    bottom: 0;
    left: 50%;
    width: 0;
    height: 2px;
    background-color: #fff;
    transition: all 0.3s ease-in-out;
    transform: translateX(-50%);
  }
  
  .smooth-scroll-nav a[href^="#"]:hover:after,
  .smooth-scroll-nav a[href^="#"]:focus:after {
    width: 100%;
  }
}
Copy after login

In the above code, we first use scroll-behavior: smooth; to apply the smooth scrolling effect. We then use the @media query to provide a fallback for browsers that do not support the scroll-behavior attribute.

In the fallback solution, we add a transition effect to the menu item so that it has an underline effect when the mouse is hovered. We use the ::after pseudo-element to create this underline and use a transition effect to animate it. Through the corresponding transition attributes, we realize the expansion and contraction of the underline.

3. JavaScript additional functions

If you want to achieve smooth scrolling in browsers that do not support the scroll-behavior attribute, we can also use JavaScript to achieve it. The following is a sample code using the jQuery library:

$(function() {
  $('a[href^="#"]').on('click', function(e) {
    e.preventDefault();
    var target = $(this.getAttribute('href'));
    if (target.length) {
      $('html, body').stop().animate({
        scrollTop: target.offset().top
      }, 1000);
    }
  });
});
Copy after login

In the above code, we use jQuery’s animate function to achieve a smooth scrolling effect. When a menu item is clicked, we first prevent the default jump behavior, then use the offset() function to obtain the position of the target element, and implement the page scrolling through the animate function .

This completes the implementation of a smooth scrolling navigation menu based on pure CSS. Using this method, we can provide a smooth navigation experience for the web page and control the appearance and behavior of the entire menu through CSS styles.

The above is the detailed content of How to implement a smooth scrolling navigation menu on a web page through pure CSS. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use bootstrap in vue How to use bootstrap in vue Apr 07, 2025 pm 11:33 PM

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

How to write split lines on bootstrap How to write split lines on bootstrap Apr 07, 2025 pm 03:12 PM

There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.

Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

How to set up the framework for bootstrap How to set up the framework for bootstrap Apr 07, 2025 pm 03:27 PM

To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

How to resize bootstrap How to resize bootstrap Apr 07, 2025 pm 03:18 PM

To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-

How to insert pictures on bootstrap How to insert pictures on bootstrap Apr 07, 2025 pm 03:30 PM

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.

How to use bootstrap button How to use bootstrap button Apr 07, 2025 pm 03:09 PM

How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text

See all articles