How to change opacity while scrolling the page?

In this article, we will learn how to modify the opacity of HTML elements based on the user's scrolling position. This technique adds dynamic visual effects to your website and can be accomplished using JavaScript or jQuery along with CSS.

Approaches

We will explore two methods to change opacity while scrolling ?

  • Using JavaScript Native JavaScript with scroll event listeners

  • Using jQuery jQuery library for simplified syntax

Method 1: Using JavaScript

Here's how to implement opacity changes during scrolling with vanilla JavaScript ?

Steps

  • Define the target element Identify the HTML element and assign an ID for JavaScript selection

  • Style the element Use CSS to position and style the element

  • Add scroll event listener Listen for window scroll events

  • Calculate scroll position Use window.pageYOffset or document.documentElement.scrollTop

  • Update opacity Modify the element's opacity based on scroll position

Example

The following example creates a fixed header that gradually becomes transparent as you scroll down ?

<!DOCTYPE html>
<html>
<head>
   <title>Opacity Change on Scroll - JavaScript</title>
   <style>
      #header {
         position: fixed;
         top: 0;
         left: 0;
         right: 0;
         background-color: #333;
         color: white;
         padding: 20px;
         text-align: center;
         z-index: 1000;
      }
      .content {
         margin-top: 80px;
         padding: 20px;
         height: 2000px;
         background: linear-gradient(to bottom, #f0f0f0, #e0e0e0);
      }
      .section {
         margin: 50px 0;
         padding: 20px;
         background: white;
         border-radius: 8px;
         box-shadow: 0 2px 5px rgba(0,0,0,0.1);
      }
   </style>
</head>
<body>
   <div id="header">
      <h2>Header - Watch me fade!</h2>
   </div>
   
   <div class="content">
      <div class="section">
         <h3>Section 1</h3>
         <p>Scroll down to see the header fade away. The opacity changes based on scroll position.</p>
      </div>
      <div class="section">
         <h3>Section 2</h3>
         <p>Keep scrolling to observe the opacity effect in action.</p>
      </div>
      <div class="section">
         <h3>Section 3</h3>
         <p>The header should be nearly transparent by now.</p>
      </div>
   </div>

   <script>
      window.addEventListener("scroll", function () {
         let header = document.getElementById("header");
         let scrollTop = window.pageYOffset || document.documentElement.scrollTop;
         let opacity = Math.max(0, 1 - scrollTop / 400);
         header.style.opacity = opacity;
      });
   </script>
</body>
</html>
A fixed header at the top gradually becomes transparent as you scroll down. The header starts fully opaque and fades to completely transparent after scrolling 400 pixels.

Method 2: Using jQuery

jQuery provides a more concise syntax for the same functionality. The approach is identical, but uses jQuery methods for cleaner code ?

Example

This example demonstrates the same opacity effect using jQuery ?

<!DOCTYPE html>
<html>
<head>
   <title>Opacity Change on Scroll - jQuery</title>
   <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
   <style>
      #navbar {
         position: fixed;
         top: 0;
         left: 0;
         right: 0;
         background-color: #2196F3;
         color: white;
         padding: 15px;
         text-align: center;
         z-index: 1000;
      }
      .page-content {
         margin-top: 70px;
         padding: 30px;
         height: 2500px;
      }
      .block {
         margin: 40px 0;
         padding: 30px;
         background: #f9f9f9;
         border-left: 4px solid #2196F3;
      }
   </style>
</head>
<body>
   <div id="navbar">
      <h3>Navigation Bar - jQuery Fade Effect</h3>
   </div>
   
   <div class="page-content">
      <div class="block">
         <h3>Content Block 1</h3>
         <p>This example uses jQuery for smoother code. Notice how the navbar fades as you scroll.</p>
      </div>
      <div class="block">
         <h3>Content Block 2</h3>
         <p>jQuery's $(window).scroll() makes event handling more intuitive.</p>
      </div>
      <div class="block">
         <h3>Content Block 3</h3>
         <p>Continue scrolling to see the complete fade effect.</p>
      </div>
      <div class="block">
         <h3>Content Block 4</h3>
         <p>The navbar should be completely transparent by now.</p>
      </div>
   </div>

   <script>
      $(window).scroll(function () {
         let scrollTop = $(window).scrollTop();
         let opacity = Math.max(0, 1 - scrollTop / 500);
         $("#navbar").css("opacity", opacity);
      });
   </script>
</body>
</html>
A blue navigation bar at the top gradually fades to transparent as you scroll down. The jQuery implementation provides the same visual effect with cleaner, more readable code.

Key Points

  • Math.max(0, opacity) Prevents negative opacity values

  • Scroll distance Adjust the divisor (400, 500) to control fade speed

  • Performance Consider throttling scroll events for better performance

  • Fixed positioning Essential for elements that should fade while remaining visible

Conclusion

Changing opacity while scrolling is an effective way to create engaging visual effects. Both JavaScript and jQuery approaches achieve the same result, with jQuery offering cleaner syntax and JavaScript providing better performance without external dependencies.

Updated on: 2026-03-15T16:29:43+05:30

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements