Place content following a three.js display created within a <div id="mainWindow">

Recently, I created a small Three.js animation and embedded it in my HTML page like this:

  <div id="mainWindow" class="popup_block">

  <!-- JavaScript for simulation -->
  <script src="../temp/webgl-debug.js"></script>
  <script src="../temp/three.min.js"></script>
  <script src="../temp/Detector.js"></script>
  <script src="../temp/TrackballControls.js"></script>
  <script src="../temp/OrbitControls.js"></script>
  <script src="../temp/dat.gui.js"></script>
  <script src="./main_simulation.js"></script>

<br>
This is a test : I want to put text below the "div id="mainWindow", not above like here :<br>

How could I do that ???


</div>

In my main_simulation.js, I manipulate the main "mainWindow" scene in Three.js.

However, I am encountering an issue where any text or additional HTML elements I try to add after the closing </div> tag of "mainWindow" end up appearing above it. You can see the problem in action here.

I suspect that the problem might be related to the fact that I load JS scripts at the bottom of the HTML page, but I'm not entirely sure. When I attempt to move the script loading section to the <head> section, the Three.js scene fails to render.

If anyone has any tips or tricks on how I can solve this issue and successfully append text after the main div of Three.js, I would greatly appreciate it.

Best regards,

UPDATE 1 :

I have also tried the following approach:

  <div id="mainWindow" class="popup_block"></div>

  <!-- JavaScript for simulation -->
  <script src="../temp/webgl-debug.js"></script>
  <script src="../temp/three.min.js"></script>
  <script src="../temp/Detector.js"></script>
  <script src="../temp/TrackballControls.js"></script>
  <script src="../temp/OrbitControls.js"></script>
  <script src="../temp/dat.gui.js"></script>
  <script src="./main_simulation.js"></script>
         <br>
This is a test : I want to put text below the "div id="mainWindow", not above like here :<br>

How could I do that ???

Unfortunately, the issue persists despite trying out different methods.

UPDATE 2 :

To better illustrate my problem, check out this image link showing the discrepancy between the placement of text in the HTML source versus its rendering position.

The text should be positioned below the three.js div, but the rendering displays it above instead.

UPDATE 3 :

For further clarification, refer to this image link

Answer №1

To begin with, the issue at hand has less to do with three.js and more about addressing an important concern.

Whether you are working with simple image positioning or using three.js to place the renderer within a div, it can be easy to lose sight of where elements are being appended.

This confusion tends to arise when the HTML structure is minimal, with most of the page content dynamically created through JavaScript. This can make it difficult for anyone other than the original developer to understand what's happening, which can be impractical.

Instead of creating elements directly in the body using JavaScript, consider appending them to existing wrapper containers. This way, you can easily manage the elements with CSS:

<div id="mainWindow"></div>
  <div id="image"></div>
  <div id="below"></div>

If you shift the target div from the body to #image, you can rearrange elements as needed without modifying the JavaScript code. This approach helps prevent unexpected behaviors.

You can achieve the desired layout using the position:relative; CSS property.

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

Enhance Your Layout with Bootstrap 5 Images and Centered Elements

As I delve into learning Bootstrap 5, I am experimenting with creating a more "advanced" layout. The idea is to have two images positioned next to each other with titles below them, and a div centered between both. Ultimately, they will be displayed in a r ...

Issue with logging objects in an array within a for loop

I am having an issue with a JavaScript for-in loop. Why does console.log(user) display the number "0" when iterating through users? Is it indicating the index of the user object in the array? I would like to log each object individually... Thank you r ...

Focus on selecting each label within a table using JavaScript

In my current setup, I am attempting to customize radio buttons and checkboxes. Array.from(document.querySelectorAll("tr")).forEach((tr,index)=>{ var mark=document.createElement("span"); Array.from(tr.querySelectorAll("input")).forEach((inp,index ...

My JavaScript functions are not compliant with the HTML5 required tag

I am currently developing input fields using javascript/jQuery, but I am facing an issue with the required attribute not functioning as expected. The form keeps submitting without displaying any message about the unfilled input field. I also use Bootstrap ...

Adjust the PHP variable's value when loading content via AJAX

Seeking assistance after attempting to create a PHP template within the Foundation 4 framework without clear guidance. The framework is quite basic, so I used jQuery to incorporate page transitions by making an AJAX call to retrieve "content" from another ...

After extended periods of use, the website experiences frequent crashes

Currently, I am developing a website using a free provider (000webhost) and focusing on integrating a chat feature. To achieve this, I have implemented an interval every 500 milliseconds that reads a file to check for new messages. When a new message is de ...

What's the best way to create an onclick event for a li element that includes a pseudo-element ::before

I have successfully created a Timeline using HTML and CSS elements. The visual result is as follows: My goal is to enable users to click on the second circle, triggering a color change in the line that connects the first and second circles. This color tra ...

Avoid transitioning to a different page using Vue-router

I have recently ventured into the world of Vue.js and my current project involves fetching data from an API, implementing pagination, and creating a detailed view for each post upon clicking. I have successfully implemented the pagination feature, however, ...

Is there a way to send map data using props in React?

I just want to store and pass the current props.url to the videomodal so I can show the same active video on the video modal. I can't use useState in the map. How can I pass it? Or is there any other solution? Videos.tsx ( props.url must be in the &l ...

What are some strategies for stopping Knex.js from executing a query object upon return from an asynchronous function?

My node.js backend utilizes Knex.js to construct dynamic DB queries based on various inputs. The challenge I'm facing is handling asynchronous processing of certain inputs. When returning a knex query object from an async function or a Promise resolve ...

`Custom transitions between sections using fullpage.js`

Has anyone used the fullpage.js extension to achieve an animation similar to this one, where sections are destroyed on scroll? ...

What is the best way to reference an ImageButton using jquery?

I created an HTML page with a special div in the body that contains a collection of buttons and images. <div id="div_one"> <button id="button_one"> <asp:Image id="image_button_one" ImageUrl=".." runat="server"> </button> </div& ...

Common causes leading to my header component experiencing hydration errors in Next.js

I am currently developing a job portal and encountering an issue with user authentication using JWT in combination with local storage. The problem arises in my header component when the user is authenticated, and if the web page is reloaded, I receive a hy ...

Creating a table from data in your database using Firebase

Can anyone guide me on how to craft a data table similar to this examplehttps://i.sstatic.net/xiUNn.pngusing information from a Firebase database like shown here https://i.sstatic.net/YBzjz.png The table should have columns for ID, Title, Number of Answer ...

Guide to transferring parameters from one function to another in Javascript

For my automation project using Protractor and Cucumber, I have encountered a scenario where I need to use the output of Function A in Function B. While I was able to do this without Cucumber by extending the function with "then", I am facing difficulties ...

Unexpected outcomes from the Jquery contains method

Take a look at this example: http://jsfiddle.net/SsPqS/ In my HTML, there's a div with the class "record" to which I've added a click function. Within the click function, I have the following code snippet (simplified): $(".record").click(funct ...

Disregard periods in URLs when configuring nginx servers

While developing with Vite in development mode via Docker on my Windows 10 machine, I encountered an issue where the local custom domain would not load due to a required script failing to load. The specific script causing the problem is /node_modules/.vit ...

Developing a Prototype for an Angular Directive

After following instructions from a question on Stack Overflow, I have updated my application configuration with the code snippet below: $provide.decorator('formDirective', function($delegate) { var directive = $delegate[0]; directive.contro ...

Create text that remains hidden behind an image while ensuring the content stays fully

In my website design using HTML/CSS, I am working on achieving a specific layout goal: https://i.sstatic.net/6UUwK.png My aim is to have a div of text positioned behind an image. Although I have successfully accomplished this on a laptop screen, I am fac ...

Exploring the contrast between 'completed' and 'upcoming' in callback functions within node.js

Within the passport documentation for authentication configuration, there is a function that appears rather intimidating as it utilizes the enigmatic function "done." passport.use(new LocalStrategy( function(username, password, done) { User.findOne( ...