Navigate through the div only when the other divs are not being navigated

Here is a demonstration of what I am trying to accomplish using jsFiddle.

In my design, I have multiple div elements within the parent window size, preventing the need for scrolling on the body.

The goal is to listen for the scroll event on the window, and if no other div is being scrolled (i.e., nothing else is moving), then the .mainContent should start scrolling.

For instance, if I am scrolling within the .sidebar, only the sidebar should scroll. However, if I move the mouse wheel at the right edge, indicating no active scrolling, then the .mainContent div should scroll instead, mimicking an overflow behavior as if the body had more content.

Does this explanation make sense?

My question is how can I detect when no other element is currently being scrolled with the mouse wheel input so that I can trigger the scrolling action on the .mainContent in such situations?

Answer №1

Here is a suggestion for you to try:

$(window).bind('mousewheel', function (event) {
    var isHover = $('.mainContent').is(':hover');

    if (!isHover) {
        $('.sidebar').scrollTop($('.sidebar').scrollTop() - event.originalEvent.wheelDeltaY);
    }
});

Check out this example on jsfiddle

Answer №2

Take a look at this JSFiddle. It is compatible with most web browsers:

//Swap values of the below two variables if required
var scrollDiv1=$('.mainContent'),
    scrollDiv2=$('.sidebar');

var s=0;
var mousewheelevt = (/Firefox/i.test(navigator.userAgent)) ? "DOMMouseScroll" : "mousewheel";
$(window).bind(mousewheelevt, function(e){
    if(s==0)
    {
    var evt = window.event || e;
    evt = evt.originalEvent ? evt.originalEvent : evt;             
    var delta = evt.detail ? evt.detail*(-40) : evt.wheelDelta;
    if(delta > 0) {
        scrollDiv1.stop()
        .animate({scrollTop :scrollDiv1.scrollTop()-100},'linear');
    }
    else{
        scrollDiv1.stop()
        .animate({scrollTop :scrollDiv1.scrollTop()+100},'linear');
    }
    }
    s=0;
});

scrollDiv2.bind(mousewheelevt, function(e){
    s=1;
});

To use wheel change the value of the variable mousewheelevt to :

var mousewheelevt = "onwheel" in document.createElement("div") ? "wheel" : // Modern browsers
          document.onmousewheel !== undefined ? "mousewheel" : // Webkit + IE
          "DOMMouseScroll"; // Remaining (Previous firefox versions)

Answer №3

Thank you to everyone who took the time to provide answers. While I appreciate the responses, they did not fully solve my issue as they focused on one scrollable area only. However, the problem lies in the fact that .sidebar is not the only scrollable element; there may also be modals and other components present. One suggestion was to revamp this using the class .scrollable as recommended by @A.Wolf, but I believe I have come up with a better solution suitable for my particular situation.

Once again, I want to express my gratitude to all those who offered their input. Your responses have given me clarity on what I truly require and the actual nature of my problem.

$(window).bind('mousewheel', function (event) {
    if($(event.target).is('body')) {
        $('.mainContent').scrollTop($('.mainContent').scrollTop() - event.originalEvent.wheelDeltaY);
    }
});

This is a basic function which may need adjustments for cross-browser compatibility, as mentioned by @Zword.

var mousewheelevt = (/Firefox/i.test(navigator.userAgent)) ? "DOMMouseScroll" : "mousewheel";
$(window).bind(mousewheelevt, function(e){

Before implementing this, I intend to conduct further research since Firefox's recommendation of using DOMMouseScroll has been deemed unstable in some cases. The Wheel event might be a more reliable alternative.

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

Finding an element's id within a click event inside an iframe

<iframe id="myiframe" src="doc.html"> <button id="btn1"></button><!-- how do I retrieve this id? --> </iframe> $('#myiframe').on('click', function () { alert(this.id); }); I am trying to make it ...

unable to respond when clicking an angularjs link

I'm facing an issue where I can't get a link to respond to click events in AngularJS. When I click on the anchor link, nothing happens. Here is a snippet of the AngularJS script: <script data-require="<a href="/cdn-cgi/l/email-protection" ...

Tips for creating a clickable li element that can toggle a checkmark

My goal is to create a list with names on the left and checkmarks on the right, where users can toggle the checkbox by clicking on the entire element without using jQuery. <div class="list-checkbox list-group list-button mx-auto mt-2"> <button ...

dynamically open ngx-bootstrap accordion panels

I implemented the ngx-bootstrap accordion feature to display a list of blog posts. Below is the template structure: <accordion id="blog-list"> <accordion-group *ngFor="let post of posts; let first = first;" [isOpen]="first" id="post-{{post.i ...

Determine the scrolling progress within a DOM element

My application requires content to be loaded as the user scrolls down, similar to social media platforms like Twitter and Facebook. I am currently working on detecting the need to load more data based on scroll events and the current position of the scrol ...

AngularJS Treeview - Rearranging tree nodes

My journey with Angular is just beginning, and I managed to create a treeview following this example: jsfiddle.net/eu81273/8LWUc/18/ The data structure I've adopted looks like this: treeview1 = [ { roleName: "User ...

Page freezing due to JQuery scroll event

I successfully implemented a trigger that checks if an element is within the visible viewport and added it to the scroll event. While this works smoothly on some pages, I noticed that on larger pages it causes the page to freeze. In Firefox, I experienced ...

Automated driver code leads back to the original URL

Looking for help with a code snippet that is supposed to open a site, fill in the PNR value, and load an invoice. However, when clicking on the invoice, it sometimes redirects back to the starting URL instead of displaying the invoice details. from seleniu ...

Top WebSocket/Perl solution for maximum effectiveness

Currently, the company I am employed at relies on Perl for all backend tasks. However, we are interested in implementing real-time communication between server processes and connected clients through web browsers. We currently use Apache as our web server ...

Using NodeJS and ExpressJS to send the HTTP request response back to the client

After creating a website using Angular 2 and setting up node.js as the backend, I successfully established communication between the Angular client and the node.js server. From there, I managed to forward requests to another application via HTTP. My curren ...

HTML's Nesting Buttons for Enhanced Interactivity

Can anyone help me figure out why the inner buttons in my HTML code are displaying outside of the parent button? I am attempting to showcase buttons inside a large button on my webpage. Your feedback on what I might be doing wrong here would be greatly a ...

Monitor which image has been selected to alter the content inside

Is there a way to track the image clicked on and modify the inner HTML of the element located above where all the images are displayed? For instance, if I click on St. John The Baptist, I want the title to change to St. John The Baptist. Currently, the fu ...

JavaScript date selector allowing the selection of multiple dates

As a beginner in JavaScript, I am struggling to create a datepicker input field that allows multiple selections. Despite researching for solutions, none seem to fit my specific case. Can anyone offer guidance on how to achieve this? Your help is greatly ap ...

Placing numerous meshes that are mostly alike but not exactly the same in a scene

We utilize a single geometry that is attached to every mesh within our scene. var geometry = new three.PlaneGeometry(1, 1, 1, 1), Each object has a texture that is generated and cached to form a new material and a mesh for the object. this.material = ne ...

The content in my textarea disappears once the submit button is clicked

I have a script that deletes the text from my textarea after a user clicks the submit button. However, I would like the text to remain in the textarea after submission. <form action="/search" name="myform" id="formid" method="post"> <div cla ...

Arranging Functions in JavaScript

I am encountering an issue regarding the execution of JavaScript functions within HTML. Specifically, I am using dimple.js to create a graph and need to select an svg element once the graph is created via JavaScript. Despite placing my jQuery selector as t ...

JavaScript function is not being invoked when receiving PHP arguments

When using the < a > tag in php, I encountered an issue where passing an argument in a JavaScript function did not result in the function being called. However, if I passed empty arguments, the function was executed. Here is the JavaScript code: fu ...

Font file integration with webpack implementation is smooth sailing

I'm having trouble getting my font to load while using webpack2 with express. The JavaScript file is working fine, but the font just won't serve. Can someone please help me troubleshoot this issue? Here's a snippet of my webpack.config: con ...

Exploring and Modifying CSS Styles in Element Properties

Struggling to remove a 50px gap on my website. It's built on Joomla 1.5 and the white space is highlighted in orange below. When I inspect element using Chrome, it shows a margin-bottom: 50px but doesn't specify the location. Any tips on how to ...

Customized CSS scrollbar within a specific div

Is it possible to apply CSS to customize the scroll bar of a specific div without affecting the entire page layout? ...