Stop the page from scrolling when the mouse hovers over the scene in Firefox

I have a three.js application embedded within a div on a webpage. The issue I am facing is that when using the OrbitControls.js for zooming with the mouse wheel, it also scrolls the entire page. To overcome this, I need to prevent scrolling when the mouse is over the canvas.

After researching, I discovered a workaround in this post which involved modifying some code in OrbitControls.js. While this solution was effective in Chrome and Internet Explorer, it did not work in Firefox (the only browsers I tested).

Do you have any suggestions for a workaround that would be compatible with Firefox as well?

You can see an example of the desired functionality by visiting this provided link mentioned in the question above (excluding Firefox users).

JavaScript:

var container = document.getElementById("container");
container.appendChild(renderer.domElement);

controls = new THREE.OrbitControls(camera, renderer.domElement);

HTML:

<div id="container"></div>

Answer №1

Is it possible to simply listen for a scroll event on the specific div and then use scrollTop 0; scrollLeft 0;?

Check out this jsFiddle version

jsFiddle : https://jsfiddle.net/qLh22das/

HTML

<div id="scroller">
    <p>TEXT 1</p>
    <p>TEXT 2</p>
    <p>TEXT 3</p>
    <p>TEXT 4</p>
</div>
<br />
<br />
<br />
<div id="scroller2">
    <p>TEXT 1</p>
    <p>TEXT 2</p>
    <p>TEXT 3</p>
    <p>TEXT 4</p>
</div>

CSS

#scroller {
    height:100px;
    overflow-y: auto;
}

#scroller2 {
    height:100px;
    overflow-y: auto;
}

Javascript

var div1 = document.getElementById('scroller');

div1.onscroll = function(event)
{
    div1.scrollTop = 0;
    div1.scrollLeft = 0;
};

Answer №2

Encountering a similar issue in my latest project, I was able to resolve it by making adjustments to the lines within OrbitControls.js:

# Line 781
this.domElement.removeEventListener( 'DOMMouseScroll', onMouseWheel, false ); // firefox

# Line 798
this.domElement.addEventListener( 'DOMMouseScroll', onMouseWheel, false ); // firefox

This was updated to:

# Line 781
this.domElement.removeEventListener( 'MozMousePixelScroll', onMouseWheel, false ); // firefox

# Line 798
this.domElement.addEventListener( 'MozMousePixelScroll', onMouseWheel, false ); // firefox

A pull request has been submitted on github for further reference: https://github.com/mrdoob/three.js/pull/7310

Answer №3

After finding inspiration from Canvas's solution, I managed to discover a fix for my issue. Initially, I believed the problem lied within three.js/OrbitControls.js, but it turns out that using regular Javascript events did the trick.

var container = document.getElementById('container');
var active = false; //determines if the mouse is over the div
var scrollPosition = 0;
container.onmouseenter = function(e) {
    active = true;
    scrollPosition = document.documentElement.scrollTop;
};

container.onmouseleave = function(e) {
    active = false;
};

window.onscroll = function(e)
{
    if (active) {
        window.scrollTo(0,scrollPosition);
    }
};

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

Automatically trigger a page reload using a jQuery function

Currently facing an issue with jQuery. I created a drop-down menu and would like it to expand when the li tag within the menu is clicked. Even though I am using the jQuery click function successfully, there seems to be a problem where the webpage refreshe ...

Is it possible to keep adding the keys of an array to themselves until reaching a specified limit

Given an array var ary = [5,10,28,50,56,280], I am exploring the idea of generating all possible combinations or subsets of this array until a certain threshold is reached. The objective is to store this limit in a variable and collect all the elements be ...

A guide on retrieving data with JSONP, Ajax, and jquery

I am having trouble retrieving data from an API using JSONP. I enter an ISBN number into an input box and attempt to fetch the data, but I keep encountering an error message stating "Cannot read property 'title' of undefined". Can anyone assist m ...

How can I restrict the panning of an OrthographicCamera in OrbitControls to ensure that the object (texture image) remains within the visible bounds of the scene at

Utilizing OrbitControls to manage an Orthographic camera, with a 2D texture image in the scene. The objective is to restrict the camera's pan once it reaches the edge of the image. For instance, if the camera pans left and the image shifts right in t ...

Initiate activity when link is clicked

I am currently in the process of developing a website that includes 5 divs. <div class="one"></div> <div class="two"></div> <div class="three"></div> <div class="four"></div> <div class="five"></div ...

Having trouble with fetching data in React from a URL?

When attempting to send a post request to a specific URL without proxy settings, I encountered a CORS error. After setting up a proxy, the URL was still pointing to localhost. The error persists even after attaching my proxyfile.js and including the code s ...

Conceal flexbox item depending on neighboring element dimensions or content

I've encountered an issue while using a flexbox to display two elements side by side. The left element contains text, while the right one holds a number. When the value in the right element has at least 7 digits ( > 999,999 or < -999,999), I ne ...

invoking both componentDidMount and componentDidUpdate within the identical code block

componentLifeCycleMethod() { let data ; axios.get('http://localhost:8000/wel/') .then(res => { data = res.data; this.setState( { details: data }); }) .catch(err => {}) } I am looking to opt ...

Updating the content within a div following the submission of a contact form 7

I'm struggling with my contact form. I want the content of my contact form div to change after clicking submit on the form. I've been searching for a solution, but so far, no luck. Here is what I have: Form (div1) with input fields, acceptance c ...

Guidelines for adjusting the next/image component to a full 100% height

In my Next.js application, I am trying to display an image that fills the full height of its container while automatically adjusting its width based on its aspect ratio. I attempted the following method: <Image src="/deco.svg" alt=&qu ...

Tips for creating an output directory when the TypeScript files are stored in the './src' location

Here is what I currently have: ./src/myfile.ts ./test/mytest.spec.ts I want tsc to output a javascript file (myfile.js) and a definition file (myfile.d.ts) in the ./build directory. This is my tsconfig.ts: { "compilerOptions": { "module": "common ...

Ensuring that the content fits perfectly inside a div using either Bootstrap or

I am facing an issue with two nested divs. One of them has a button that I want to stick to the bottom of the parent div, while the other contains text or a list of persons. The problem arises when the text is lengthy, causing it to overflow the div. I am ...

The react transition group fails to add the necessary CSS classes to the specified div

Regardless of whether I utilize React transition group, Tailwind, pure CSS, or any other framework, no transitions seem to occur when I structure my simple component in the following manner. I have experimented with various frameworks, but the outcome rema ...

What is the process of transforming an object into a CSV format using JavaScript?

I need to transform the following object into a CSV file. The keys in the array should be the column names, and only one of the arrays will have unique keys while all others will share the same set of keys but with different values. [{ Comment: "Good", ...

Display the invoice bill using text in a Vue component

Hello, I am looking to print an invoice that resembles the one shown in this picture https://i.sstatic.net/6mzwe.jpg However, when I try to print it, I get a different output with some additional elements https://i.sstatic.net/uaKZC.jpg I am using vue. ...

Converting SVG to PNG image directly in the browser (compatible with all browsers, including Internet Explorer)

I am currently working with Highcharts and I have a need to convert all the charts displayed on the webpage into images so that I can send them to my server for merging with a master export Excel file. The Excel file already contains some tables and pivot ...

The FBXLoader in three.js is failing to properly import a 3D model from a FBX file

Hey there! I've been trying to bring in an fbx model into my scene, but I'm struggling to find much information about it online. I came across a method that seems to use the official three.js package, but for some reason, the model isn't sho ...

AngularJS: Pause the data binding functionality temporarily

How can I temporarily deactivate data binding in AngularJS? I am working with a list called items that is being displayed using ng-repeat. I need to perform some operations on this list without immediately updating the page, as this could cause slowdown. ...

Incorrect behavior of responsive background images

For the first time, I am working on a school project that requires me to create a responsive website. I am following the "mobile first" approach. On stackoverflow, I came across a helpful way of making background images responsive: background-image: url(. ...

The prompt "npm run build" command resulted in a 126 Vercel exit status

While attempting to upload my website with Webpack to Vercel from the repository, I encountered an error during the build process: Skipping build cache, deployment triggered without cache. Cloning completed: 2.089s Running "vercel build" Vercel CLI 31.2.3 ...