Prevent body from scrolling when mouse is over a div, while keeping the position unchanged

Although I've come across a few discussions about this topic before (including one of my own), I'm still searching for an improvement on the only solution that has actually worked for me.

My goal is to enable mouse wheel scrolling for a height-limited <div> with overflow: scroll; and ensure that it continues to work even when the <div> is within another <div> whose content is loaded via AJAX.

During my online journey, I stumbled upon this fiddle: http://jsfiddle.net/mrtsherman/eXQf3/33/. Unfortunately, I haven't been able to make it work with data fetched through AJAX - I'm baffled by this issue, so any guidance would be greatly appreciated!

However, my current dilemma revolves around the only functional solution I've found: adding a class onmouseover.

DIV

<div onmouseover="$('html').addClass('noscroll');" 
     onmouseout="$('html').removeClass('noscroll');" 
     style="overflow:auto;max-height: 200px;">Long data</div>

CSS Class

html.noscroll
{
    position: fixed; 
    overflow-y: scroll;
    width: 100%;
}

I had thought this code was flawless until I discovered a major inconvenience on smaller screens: it keeps jumping back to the top of the screen!

It appears that activating position: fixed; not only disables the scrollbar but also repositions it at the very top, making it impossible to use as my scrollable div is midway down the page.

Answer №1

When attempting to implement this section:

I'm encountering a problem with the functionality when the data is retrieved through an AJAX call - the reason behind this issue remains unclear to me. If anyone has insights on why this may be happening, I would greatly appreciate some guidance!

A minor modification is required in your code:

$('#abs').on('mousewheel', function(e) {

View updated version

The use of bind is restricted to existing elements and cannot be applied to elements fetched via ajax. According to jQuery documentation:

Starting from jQuery 1.7, the .on() method is recommended for attaching event handlers to a document. In previous versions, the .bind() method was used for direct attachment of event handlers to elements. Handlers are connected to the presently selected elements within the jQuery object, hence these elements must be present when the .bind() function is called. For more versatile event binding, refer to the explanation of event delegation using .on() or .delegate().

Learn more about the .on() method


Please make the suggested changes to your code as shown and provide feedback post-update.

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 AJAX onreadystatechange function may not be consistently triggered

I have an issue with my AJAX call to retrieve XML data. It seems that the code does not enter the onreadystatechange function until the last iterations of my foreach loop. This delay is likely due to the time it takes for the calls to "www.webpage.com/" ...

Ways to evenly distribute divs into rows

Greetings if this question has already been inquired about, but my search turned up nothing quite like it. My current setup is as follows: .main{ display: inline-flex; flex-direction: row; } <div class = "main"> <div class="sub-div ...

I am currently facing challenges in successfully setting up my title and ensuring that the menu animation runs smoothly and works effectively

I'm struggling to solve these basic problems: 1. The text animation in the menu disappears when closing it, but I want it to appear like it does when opening. 2. I need the menu header text animation to be delayed so it doesn't overlap with the ...

What is the best way to position a button at the bottom of an image?

I am facing an issue with the following code snippet: * { box-sizing: border-box; } .wrapper { position: relative; /* border:1px solid blue; */ width: 250px; } .text { position: absolute; border:1px solid red; bottom: 0px; ...

Does text-underline qualify as a property in CSS?

My stylesheet includes a property called text-underline:none, and I'm wondering if it is a valid CSS property. Can you confirm this? ...

What steps do I need to take in order to modify information within a table using Symfony?

Working on my Symfony project, I frequently need to display various views in table format. I wish I could easily update the data by clicking on table cells, entering new information, and saving it dynamically. After exploring jqGrid, DataTable with jEdit ...

Ajax ceased functioning following the execution of a query

Can someone help me figure out why my AJAX script stops working when I add the line $var=$_GET['postid']; in getcomments.php? In my index.php file, I have the following code: <div id="commentsonpost" value="<?php echo $_GET['post&apo ...

How can I delete the onmouseover function in HTML?

Is there a way to remove the (onmouseover="mouseoversound.playclip()") function from the following HTML code? <a href="roster.html" onmouseover="mouseoversound.playclip()">Roster</a> Can we instead implement this functionality in a JavaScript ...

What could be causing the block to fail in the event of an AJAX request?

I have encountered an issue with my PHP file and AJAX setup. The if block in my code is working properly, but the else block seems to be not functioning as expected. Even after changing the condition from data!= null to data== null, the problem persists wh ...

What is the process for outputting JSON data from a script page?

I had a JSON file named results.json which is displayed below. Additionally, I had an HTML file containing some script that is used to retrieve data from this JSON file. Upon entering the HTML page, a script function get_machFollow(que_script) is called to ...

The Safari browser is plagued by a flickering issue with CSS perspective flip animations, even when using the back

I'm facing an issue with Safari (both desktop and iOS) related to a simple CSS flip animation. Despite searching through similar posts and answers, I haven't found a solution yet and am in need of a fresh perspective. Essentially, I am creating ...

The issue with the functionality of the Jquery accessibility keycode has not been

I've been working on creating a keycode jQuery function for removing 'li' tags using the enter key for the sake of accessibility practices. However, I'm facing an issue as the code doesn't seem to work properly. Despite following g ...

jQuery disregards the HTTP request method and adds unusual query parameters to the URL

My current project involves utilizing a web service that requires POST requests containing a JSON string in the body. I've encountered some challenges while attempting to interact with this web service using jQuery: 1) jQuery appears to default to th ...

Customize the Position of Icons in Material UI

I am looking for guidance on how to position my components within an image using Material UI. Specifically, I want to move my heart component to the top right corner of the image and place the ratings at the bottom. import image from "../../Assets/pic ...

The issue with Yii2's renderPartial/renderAjax is that it fails to properly load JavaScript on the view

After using the renderAjax or renderPartial methods from the controller to display a view, I encountered issues with the JavaScript functionality not working as expected. The specific problems revolve around a Google Places Autocomplete feature and a cus ...

Using PHP and Ajax to retrieve a distinct string prior to executing a time-consuming script

It's a question that comes up often. In short, I want to display progress to the user while a lengthy script is running. This is my approach: store "progress" in mysql so that Ajax can access it (via PHP). "progress()" is the lengthy script being te ...

Ways to refresh a page once the 'ok' button is clicked with sweetalert

Hey there! I recently used sweetalert in my code. swal("Great work!", "You successfully clicked the button!", "success") This code triggers a message box with an 'okay' button. I am wondering if it is possible to automatically refresh the page ...

Updating a calendar page in Rails 4 using AJAX technology

Currently, I am utilizing jQuery's datepicker functionality to display a calendar. The intended behavior is that upon clicking on a specific date, the page should generate relevant information (in this case, a table showcasing available seating) assoc ...

Can you explain the purpose of the spaces on the buttons?

mysterious button spacing issue <div class="buttons"> <button id="btn-search" type="submit">Search Engine</button> <button id="btn-lucky" type="submit">Feeling Lucky Today</button> </div> .buttons { max-width: 29 ...

Exploring the World of Bootstrap 4 Tab Components

Hey everyone, I'm working on creating dynamic tabs using Bootstrap 4, similar to this example: https://codepen.io/jek/pen/BoWNRy [I didn't create it] However, when I try to use the code locally, it's not working as expected. I am able to di ...