Prevent body scrolling but allow overlay scrolling

Cover Image for Prevent body scrolling but allow overlay scrolling
Matheus Mello
Matheus Mello
published a few days ago. updated a few hours ago

Prevent Body Scrolling But Allow Overlay Scrolling: A Simple Solution! 💡

Have you ever wondered how to create a cool and interactive "lightbox" effect like Pinterest? You know, the kind where you click on an image and an overlay appears on top of the page, allowing you to scroll through the overlay content while keeping the body behind it fixed? 📷👀

You're not alone! Many developers have struggled to achieve this effect with CSS alone, only to find that the div overlay remains scrollable, ruining the whole experience. But fear not, because we're about to share a simple solution that will make your dream of a Pinterest-like lightbox come true! 🎉

The Problem: Scrollable Overlay vs. Fixed Body

Let's say you create a div overlay on top of the page and set the body's overflow property to hidden. At first, it seems like the perfect solution to prevent body scrolling. However, you soon realize that the overlay itself remains scrollable, which is not what you want. 😔

The Solution: A Little JavaScript Magic ✨

To achieve the desired effect, we need to combine CSS and a touch of JavaScript. Here are the steps to make it work:

Step 1: HTML Structure

First, let's set up the HTML structure for the full-screen container and the overlay:

<div class="fullscreen-container">
  <!-- Content inside the overlay goes here -->
</div>

Step 2: CSS Styling

Next, we need to apply some CSS styles to achieve the fixed body and scrollable overlay effect. Add the following CSS code to your stylesheet:

body {
  overflow: hidden;
}

.fullscreen-container {
  position: fixed;
  top: 0;
  left: 0;
  height: 100vh;
  width: 100vw;
  overflow-y: scroll;
}

Step 3: JavaScript Magic 🪄

Now comes the exciting part! We'll use JavaScript to control the scrolling behavior of the overlay. Add the following JavaScript code to your script file or inline script tag:

document.querySelector('.fullscreen-container').addEventListener('scroll', function(event) {
  event.stopPropagation();
});

Step 4: Engagement Time! 💬

That's it! With just a few lines of code, you've achieved the desired effect of preventing body scrolling while allowing overlay scrolling.

Now, it's time for you to put this code into action and see the magic happen! Don't forget to share your experience and any cool lightbox implementations you create with this technique. We'd love to see what you come up with! 🌈🚀

Conclusion: Let the Scrolling Begin! 📜✨

With the solution we've provided, you can now easily prevent body scrolling while allowing smooth scrolling within the overlay. Gone are the days of frustration and scrolling mishaps!

Remember, this technique relies on both CSS and JavaScript working together to achieve the desired effect. So make sure to include the necessary code snippets and enjoy creating beautiful and interactive lightboxes on your website.

Now go forth and let the scrolling begin! 🤳💃

Did you find this blog post helpful? Have questions or suggestions? Let's continue the conversation in the comments below! 👇


More Stories

Cover Image for How can I echo a newline in a batch file?

How can I echo a newline in a batch file?

updated a few hours ago
batch-filenewlinewindows

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Matheus Mello
Matheus Mello
Cover Image for How do I run Redis on Windows?

How do I run Redis on Windows?

updated a few hours ago
rediswindows

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Matheus Mello
Matheus Mello
Cover Image for Best way to strip punctuation from a string

Best way to strip punctuation from a string

updated a few hours ago
punctuationpythonstring

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Matheus Mello
Matheus Mello
Cover Image for Purge or recreate a Ruby on Rails database

Purge or recreate a Ruby on Rails database

updated a few hours ago
rakeruby-on-railsruby-on-rails-3

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my

Matheus Mello
Matheus Mello