Prevents side-to-side scrolling in the section while enabling scrolling up and down on the

I have a div on my website that allows for horizontal scrolling, while the entire page can be scrolled vertically. When the cursor is over a specific section, it disables vertical scrolling and enables horizontal scrolling within that section. I have implemented a function to achieve this functionality. How can I modify the function so that when the horizontal scroll within the section reaches its limit, the section scrolling ceases and the webpage scrolling resumes? What steps should I take to accomplish this task? Below is the function I am currently using for horizontal scrolling within the section:

 (function() {
    function scrollHorizontally(e) {
        e = window.event || e;
        var delta = Math.max(-1, Math.min(1, (e.wheelDelta || -e.detail)));
        document.getElementById('myDIV').scrollLeft -= (delta*40); // Multiplied by 40
        e.preventDefault();
    }
    if (document.getElementById('myDIV').addEventListener) {
        // IE9, Chrome, Safari, Opera
        document.getElementById('myDIV').addEventListener("mousewheel", scrollHorizontally, false);
        // Firefox
        document.getElementById('myDIV').addEventListener("DOMMouseScroll", scrollHorizontally, false);
    } else {
        // IE 6/7/8
        document.getElementById('myDIV').attachEvent("onmousewheel", scrollHorizontally);
    }
})();

Answer №1

To determine if the width of a div with horizontal scroll has reached its end, you can measure the width of the div and compare it to the distance scrolled horizontally using jQuery's .scrollLeft() method. Make sure to include the div's width in the calculation. If the two measurements are equal, it means the scrolling has reached the end of the div. You can then set a flag that should be constantly monitored in your scroll function - this flag could help decide whether the div or the page should continue scrolling.

Answer №2

 $(window).scroll(function() {   

  if($("#yourdiv").scrollLeft() + $(window).width() == $("#yourdiv").width()) {
     Insert your custom code here
   }
});

This script should function properly, but only if your div is wider than the window. If not, you may need to revise $(window).width() to $("#mycontainer").width instead. I've had success using this approach in a similar situation, although the effectiveness may vary depending on the layout of your webpage.

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

Is there a solution for iOS automatic hover adjustment?

Is there a way to create a jQuery plugin or JavaScript script that automatically loops through each CSS hover effect (from an external stylesheet) and binds it with a double tap event? Tap 1 - Triggers the CSS :hover effect Tap 2 - Acts as a click (follo ...

Adding data to a multidimensional array in JavaScript

I am in need of creating a multidimensional JavaScript array dynamically, following this specific structure: array_answers[0][1]:"yes" array_answers[1][2]:"no" array_answers[2][2-subquestion]:"text input" array_answers[3][8]:"yes" array_answers[4] ...

Unexpected behavior observed when trying to smoothly scroll to internal links within a div, indicating a potential problem related to CSS dimensions and

Within a series of nested div containers, I have one with the CSS property overflow:hidden. My goal is to smoothly scroll to internal links within this specific div using jQuery. The snippet of code below has worked successfully in previous projects: ...

Sending JSON Data over URL

I am facing a challenge with passing data between two HTML files. My initial solution involved sending the data through the URL using the hash and then parsing the link using JSON.parse(window.location.hash.slice(1));. This method worked for a few attempts ...

javascript utilizing the canvas feature to resize and add graphics to an image

Is it possible to leverage Canvas for drawing over images that have been downloaded by users, all while adjusting the scale? For instance: User uploads an image Starts drawing on the image Zooms out and continues adding more drawings All modifications ...

Utilizing Nested Masterpages in conjunction with CSS files

I am faced with a situation where I have two masterpages - one named main.Master and the other search.Master. The search.Master is actually a nested masterpage, residing within the main.Master. In order to ensure that my CSS files function properly with t ...

Can one initiate a server within Zapier Code?

Is there a way to send an HTTP CODE = 200 with a response body of 'OK' when receiving a notification on Zapier? I attempted to use the following code snippet in Zapier: var http = require('http'); const server = http.createServer((r ...

Determine the quantity of data entries in a Mysql database table using php

I organized a school voting process with 10 questions, allowing participants to select one teacher for each question from a pool of 80 teachers. Once a participant has voted for all questions, a new entry is inserted into my MySQL table. My MySQL table co ...

Is it possible for React Server Side rendering to be re-rendered on the client side?

In this demonstration of isomorphic rendering found at https://github.com/DavidWells/isomorphic-react-example, the author showcases Server Side Rendering by disabling Javascript. However, if JavaScript is enabled on the frontend, does it trigger a re-rende ...

Using the video-js feature to play multiple videos simultaneously

Is it possible to play multiple videos using video-js functionality simultaneously? The answer is yes! Check out Fiddle 1 However, an issue arises when wrapping a trigger, such as a button, around the function that invokes playVideo(). This causes the v ...

Can an if-statement be utilized in an Ajax scenario?

I'm currently developing a web application that enables users to post messages on a page. There are two ejs files involved in this process: index.ejs contains the form for users to input information and displays a list of usernames and messages re ...

Issue encountered while attempting to utilize setStart and setEnd functions on Range object: Unhandled IndexSizeError: Unable to execute 'setEnd' on 'Range'

Every time I attempt to utilize a range, an error message appears in the console: Uncaught IndexSizeError: Failed to execute 'setEnd' on 'Range': The offset 2 is larger than or equal to the node's length (0). This is the script I ...

Running a function of a component from within another component

In order to prevent any clutter and avoid using "native" jQuery/javascript, my goal is to elegantly call a function in the child component from the parent. The specific function I want to execute is change_map_data() from the child component G_Map.vue, all ...

What is the method to obtain the zoom level in IE5 using JavaScript/jQuery?

Currently, I am using IE11 and have enabled emulation in the developer tools to change the document mode to 5. My goal now is to determine the current zoom level, which I adjust in the settings of IE. https://i.stack.imgur.com/DYftF.png The code snippet ...

Creating adjustable absolute positioned elements in Bootstrap 4 for optimal responsiveness

Is there a Bootstrap 4 class that can help me position an absolute yellow color container in a responsive way across all devices, without the need for writing multiple media queries? I want to achieve a screen layout like this on my webpage. Any suggestion ...

Error: The initial parameter must be a string, Buffer, ArrayBuffer, Array, or Array-like Object. An object type was passed in instead in CryptoJS

In my quest to encrypt and decrypt data in react-native, I embarked on using the crypto node module by incorporating it into my react native project through browserify. While attempting encryption with the code snippet provided below, an error surfaces sta ...

An introduction to integrating Paged.js with Vue.js3

index.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <link rel="icon" type="image/svg+xml" href="/vite.svg" /> < ...

Content is the main driver for CSS Flexbox dynamic aspect-ratio code

When creating a grid layout for a webpage, I opted to use Flexbox. My goal was to incorporate some automatic functionality so that additional grid boxes could be included without the need to manually add classes or styles in the HTML code. One particular f ...

Requesting data from an external URL using Ajax

Attempting to retrieve specific data attributes from my personal Duolingo account has led me to encounter a JSON object upon entering the following URL: (substituting "username" as my id in the url). var getDuolingoData = function() { return $.ajax( ...

Versatile, stationary division - Looking to attach a different division at the bottom

Element A has a flexible and fixed position (at the top of the window) with a high z-index, allowing items below to pass underneath when scrolling. Element B is positioned below Element A, and I want it to remain attached to the bottom of Element A while ...