What is the best way to prevent clicking and scrolling on a webpage?

I have added a "more" button to the bottom left of my website to reveal a menu.

Upon clicking the button, the plus sign changes to an x.

I would like the page to disable click and scroll actions when the plus sign is clicked.

<div class="dropup">
  <button class="btn btn-default dropdown-toggle" type="button" id="dropdown-menu-action" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
    <div class="fa-4x">
      <span class="fa-layers fa-fw">
        <i class="fas fa-circle"></i>
        <i class="fa-inverse fas fa-plus" data-fa-transform="shrink-6"></i>
      </span>
    </div>
  </button>
  <ul class="dropdown-menu" aria-labelledby="dropdown-menu-action">
    <li><a href="/profile" data-drupal-link-system-path="profile"><i class="fas fa-id-card fa-lg"></i> Create my profile</a></li>
  </ul>
</div>

Below is my CSS code:

.overlay-is-navbar-collapse {
    overflow: hidden;
    pointer-events: none;
}

Here is my JavaScript file:

(function ($) {

  $('#dropdown-menu-action').on('show.bs.dropdown', function () {
       $('body').addClass('overlay-is-navbar-collapse');
  });

  $('#dropdown-menu-action').on('hide.bs.dropdown', function () {
       $('body').removeClass('overlay-is-navbar-collapse');
  });

})(window.jQuery);

UPDATE

The code above is not functioning as intended. When I click outside the menu, it closes but the body class remains.

  $('#dropdown-menu-action').on('click', () => {
       $('body').toggleClass('overlay-is-navbar-collapse');
  });

I have also tried the following code, but it does not work:

  $('#dropdown-menu-action').on('shown.bs.dropdown', function () {
       $('body').addClass('overlay-is-navbar-collapse');
  });

  $('#dropdown-menu-action').on('hidden.bs.dropdown', function () {
       $('body').removeClass('overlay-is-navbar-collapse');
  });

Answer №1

To toggle the body class name overlay-is-navbar-collapse in your JavaScript file, simply add a click handler.

(function ($) {

  $('#dropdown-menu-action').on('click', () => {
   $('body').toggleClass('overlay-is-navbar-collapse');
  });

})(window.jQuery);

Make sure to delete pointer-events: none from your CSS file so that you can click on the button again after adding the class name overlay-is-navbar-collapse.

.overlay-is-navbar-collapse {
    overflow: hidden;
}

Answer №2

To prevent scrolling, consider adding a CSS class to the body element or directly styling the desired axis. For example:

body.class { overflow-y: hidden; }

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The Jquery Datatable fails to display the accurate number of rows based on the selections made in the dropdown

I am working on an ajax call that returns a table. In the success method, I am using $("#tableID").dataTable();. Although it shows paging and the number of rows in the dropdown, it is displaying all rows instead of only the number of rows selected in the d ...

sending the express application to the route modules

Currently, I am developing an express 4 api server with no front end code. Rather than structuring my project based on file types such as routes and models, I have decided to organize it around the business logic of the project. For example, within my Use ...

The datepicker feature has been programmed to only allow past or present dates

I've integrated a date picker on a website like so: <input type="text" id="popupDatepicker" placeholder="Select Date" name="from_date" class="input" size="50" /> Here's the script being used: $(function() { $('#popupDatepicker&apos ...

Angularjs drop-down menu changes images with animation

<body ng-controller="myCtrl"> <input type="checkbox" ng-model="loaded"/> <select ng-model="list"> <option ng-repeat="option in data.availableOptions" value="{{option.name}}">{{option.id}}</option> </select> {{list}} &l ...

Proper application of article, aside, and header elements

Can someone help me with my page template setup? I have a sidebar on the left and main content area on the right. I'm wondering if I am using the article, aside, and header tags correctly. Also, should I attach classes/ids to html5 elements or is that ...

Adjusting label color when input is deactivated in a React application using TypeScript

Is there a way to change the label color in my view when the input is disabled using Typescript or CSS? This is the interface I am working with: interface image Here is the code snippet: <tr> <td className="name">Critère d&a ...

Run a PHP script on the current page upon choosing an option from a dropdown list with the help of Ajax or JavaScript

I'm currently working on a MySQL query that needs to be executed when a user selects options from multiple dropdown lists. What I am looking for is the ability to automatically run a query related to the selected dropdown list option using AJAX/JavaS ...

Strengthening JavaScript Security

Throughout the past few years, I have delved into various javascript libraries like Raphael.js and D3, experimenting with animations sourced from different corners of the internet for my own learning. I've obtained code snippets from Git repositories ...

CodeIgniter functionality for generating auto-incrementing IDs that are accessible in both the view and within JavaScript's Window.Print() method

While working on creating an invoice, I encountered a few issues. First, I want the Invoice No: to be displayed in my view (receipt.php) as 0001 and use it as the primary key in my tbl_payment table. However, I'm unsure how to have an auto-incremented ...

Finding the identifier for resources through excluding external influences

I am currently facing an issue with the full calendar plugin. In my set up, I have 3 resources along with some external events. The problem arises when I try to drop an external event onto the calendar - I want to retrieve the resource id from which the ev ...

CSS Problem with Image Overlapping

Attached is an image that I would like to design in HTML. It has been quite successful, but when testing it on different resolutions, the red box seems to move around. The design was created with 100% width and height. <style type="text/css"> ...

Display a progress bar in Bootstrap with a labeled indicator positioned above the bar and background to the

Is there a way to create a progress bar with a label positioned to the right using Bootstrap v5.2 Progress Bar? https://i.sstatic.net/gOWie.png I attempted to use negative margins to achieve this layout but faced issues when the label expanded due to lon ...

PHP contact form not properly sending complete submission data

I'm facing an issue with my HTML contact form that utilizes JavaScript for a change function. Essentially, I have a dropdown menu for different subjects, and based on the selected option, specific fields should appear. However, when a user fills out t ...

Can the <article> tag and all its content be positioned in the center of the webpage?

Can the <article> and its content remain centrally aligned on the page? Check out my website here: Typically, when you visit a website, the article is positioned in the center of the page. However, in my case, the article only remains centered when ...

React and the turn.js library (please note that turn is not a predefined function)

I'm trying to integrate turn.js with React by following an example I found here: https://codesandbox.io/s/005xlk45mn After adapting the code to my project, I encountered the following error: TypeError: jquery__WEBPACK_IMPORTED_MODULE_6___default(... ...

JavaScript: Passing the "this" object in an AJAX request situation

Here is the code snippet I am working with: $(function(){ $("#CASA").click(function(){ rsExecute("rs/rs_luci.php","premi",1,function(cc){}); var col=$( this ).css( "background-color" ); se ...

How to modify the background image of a div using CSS from a specific folder

<head id="Head1" runat="server"> <title>Title</title> <style> .parallax { /* The image used */ background-image: url(Pictures/BackUS.png); height: 100%; /* Create the parallax scrolling effect */ back ...

What are the steps to switch the dropdown values?

I am facing an issue with swapping values in two dropdowns. The scenario is that I have a dropdown with minimal values and another one with maximum values. If a value selected in the first dropdown is greater than the value in the second dropdown, they nee ...

Placing buttons on top of a video within a div container

I am facing a challenge with implementing custom controls on a video embedded in my webpage. I have tried setting a div to absolute position for the buttons, but they are not clickable. Is there a way to achieve this without using jQuery? I need the butto ...

The jQuery mobile tap event triggers the vclick event

I have been developing a jQuery mobile application that needs to be functional on both mobile devices and PCs. To detect finger events, I am utilizing the jQuery Finger plugin (): $('body').on('tap', 'image', function(e) { ...