Interactive feature in Three.js causes page to scroll downwards upon object selection

Exploring Three.js for the first time, I'm looking to integrate it into the landing page of my website. However, I've encountered an issue where clicking on the scene area with my object causes the page to shift downward. I've tried adding overflow: hidden; in my CSS, but that hasn't fixed the problem. It seems like the page moves to center the object when clicked. My suspicion is that I need to make adjustments in the JavaScript code.

var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 1, 1000);
camera.position.z = 8;
var renderer = new THREE.WebGLRenderer({antialias: true, alpha: true});
ambientLight = new THREE.AmbientLight(0xffffff, 0.8);
scene.add(ambientLight);
light = new THREE.PointLight(0xffffff, 1, 20);
light.position.set(10,10,10);
light.castShadow = true; 
light.shadow.camera.near = 10;
light.shadow.camera.far = 25; 
scene.add(light);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.BasicShadowMap;
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);

// Rest of the JavaScript code...
/* CSS styling code... */
<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" href="main.css">
        <meta charset="utf-8">
        <meta name="viewport" content="initial-scale=1">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title></title>
    </head>
    
    <body onload="document.body.style.opacity='1'">
        <h1>
            <a href="../MAIN/main.html">hello world</a> 
        </h1>
        
        <canvas id="canvas"></canvas>
        <ul>
            <li><a href="../WORK/work.html">work</a></li>
            <li><a href="../ABOUT/about.html">about</a></li>
            <li><a href="../CONTACT/contact.html">contact</a></li>
        </ul>
        
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
        <script src="https://threejs.org/build/three.min.js"></script>
        <script src="https://threejs.org/examples/js/controls/OrbitControls.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/tween.js/17.4.0/Tween.min.js"></script>
        <script src="../MAIN/main.js"></script>
    </body>
</html>

Answer №1

It appears that OrbitControls attaches a mousedown event listener to the canvas element, causing it to be brought into focus. To prevent this behavior, you will need to either remove or replace this event listener.

Check out this screenshot from devtools for reference

Answer №2

Modifying or removing the mousedown function could potentially disrupt other functionalities.

Instead of that, you can utilize the following line of code:

controls.domElement.focus = null;

This will prevent the focus from shifting to the window or domElement.

Credit goes to @hayden-linder for encouraging us to delve deeper into this issue.

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 challenge of Angular form data binding

I'm encountering an issue with binding an input box to a controller in Angular. Despite following tutorials, the model never updates when I access the property or check the scope using AngularJS Batarang. Upon form submission, $scope.licenceKey remai ...

The VLC WebPlugin puts a halt on websocket activity during playback

I currently have a basic HTML/JS application that includes an embedded VLC WebPlugin and several methods for play/pause functionality. This application is being managed by another JavaScript file through a straightforward server/websocket C# setup. The con ...

Is it best practice to use the AngularFirestoreCollection for updating Firestore items in AngularFire?

Within my application, I have a list that necessitates the use of an "or" condition. However, according to the documentation: "In this case, you should create a separate query for each OR condition and merge the query results in your app." Consequently ...

A Multilingual Application developed using either AngularJS or the Ionic Framework

Having recently delved into both AngularJS and the Ionic Framework, my background is primarily in Microsoft technologies. I am currently working on developing an app that supports three languages: English, Arabic, and French. Drawing from my experience wi ...

Can you please provide me with detailed instructions on how to use the radio button?

As someone who isn't a professional coder, I dabble in creating code for fun. Right now, I'm working on a roleplay project on a website where each character has a designated "space" to input HTML and CSS code as a template. I've stumbled upo ...

Nodejs and Express are seamlessly integrating error handling with database submission, ensuring a smooth flow of data processing

While testing my backend using nodejs and express, I encountered an issue with error handling in postman. The error message displays correctly for the 'Multiplier' column validation, but despite this, the data is being submitted into my mysql wor ...

Implementing a jQuery method in a PHP page that is loaded through AJAX

I am facing an issue on my page where I am using jquery/ajax to load a series of buttons into a div. This script loads buttons every time the user scrolls the page and each button should run a jquery function when clicked. The problem arises when I switche ...

Tips on aligning a span inside a div to the right and ensuring the text is responsive

I want to enhance my bar designing skills. I managed to create one, but I'm facing some issues. How can I make this span align to the right? I tried using float but it didn't work. Any suggestions? Also, when I try to shrink the screen, the text ...

Struggling to Personalize Kendo Calendar Month templates using Knockout Kendo JS Binding

I have made modifications to the Kendo Calendar Month Template Resource which can be found Here without utilizing knockout-kendo.js. The official Kendo Reference is available Here. The issue arises when I implement the following code in knockout-kendo.js ...

Using jQuery's .load() method is like including a file in a

Currently, Im working on revamping a company website that comes equipped with its very own CMS. Unfortunately, we are restricted from accessing the server configuration and FTP, which means I am limited to running only client-side files like HTML/CSS/J ...

`When a link is clicked, show one div overlapping another.`

Is it possible to have one div display over another when a link is clicked? Here is the HTML and CSS code: <div id="desc" class="desc">some text</div> <div id="awards" class="awards">some other text</div> <a href="#">click< ...

Update the image and heading simultaneously when hovering over the parent div

I am looking to enhance the user experience on my website by changing the color of headings and images when a user hovers over a specific section. Currently, I have been able to achieve this effect individually for each element but not simultaneously. Any ...

How to retrieve distinct items from an array using Javascript

If I have the following array of objects: const arr = [{ id: 'A', version: 0, name: 'first' }, { id: 'A', version: 1, name: 'first' }, { id: 'B', version: 0, name: 'second&apo ...

Exploring the functionality of event.target.name.value

I've been struggling to make event.target.name.value work for an input field in my form. When I submit the form, the values appear as null and I have to click twice to get them. This is the code I've written: const [name, setName] = useState(& ...

Just starting out with Flexbox - what steps should I take?

In the code, there are three divs arranged in the DOM by div-00#. I am seeking to achieve the following layout using flexbox (for width >= 460px) as shown in the images below: https://i.sstatic.net/I2PFQ.png Added: 18-12-16 - https://i.sstatic.net/v ...

"An ng-repeat directive with a filter applied results in an empty

After successfully implementing the ng-repeat loop below: <div ng-repeat="einschItem in einschaetzungen.alldata | filter: { savedatum: lolatage[tagarrayindex].tagestring } | orderBy : '-savetimestamp'"> I wanted to check if the filtered r ...

Are desktop values taking precedence over the mobile media query?

Currently, I am facing an issue with aligning icons (images) under each title on a page that lists various classes. The icon needs to be centered below the title on both desktop and mobile versions of the site. However, I have encountered a problem where u ...

How can I modify the TextField's borderBottom style to be 'none' when it is disabled?

While working within a data cell, I encountered an issue with stacking text. To workaround this obstacle, I opted to create a text field, assign it a value and helper text, disable the field, and change the text color to black when disabled. My current cha ...

Achieving unique horizontal and vertical spacing in Material UI Grid

In my current Material UI setup, I have set spacing in the Grid Container to space Grid Items vertically. While this works well on larger screens, it causes awkward horizontal spacing between Grid Items on mobile devices. <Grid container spacing={24}> ...

Perform multiple function invocations on a single variable using JavaScript

Is there a way to execute multiple functions on a single object in JavaScript? Maybe something like this: element .setHtml('test'), .setColor('green'); Instead of: element.setHtml('test'); element.setColor('gre ...