Influencing the movement of one div container using another dynamic div

Apologies for the unclear title. Check out a JsFiddle DEMO to see what I mean.

My goal is to move the black container as soon as it touches the red container. I want to prevent the overlap between the two and adjust the position of the red container relative to the black one. I believe this can be achieved with CSS using float, margin, and display adjustments, but I am struggling to do so at the moment.

Here is the code:

JavaScript

var speed =80, deg=0, center={x:50,y:50},
        moveBox = function(){
            var el = document.getElementById("circle"),
                left = el.offsetLeft,
                moveBy = 3; 
                deg+=moveBy;
                el.style.left =center.x+Math.floor(40*Math.sin(deg/150*Math.PI))+"px";
                el.style.top =center.y+Math.floor(20*Math.cos(deg/150*Math.PI))+"px";
        };

var timer = setInterval(moveBox, speed);

HTML

<div id='square'></div>
<div id='circle'></div>

CSS

#circle{background:red; display:inline-block; width:80%; height:40px; position:absolute; border:1px solid #454545; margin-top:100px;}
#square{width:60px; height:50px; background:black; display:block; position:relative; position:absolute; margin-left:100px; margin-top:100px;}

Answer №1

I've been experimenting with this in your Fiddle and it appears to be quite challenging at the moment. In order to prevent divs from shifting during animations, typically a relative/absolute css position is applied. However, when I tried implementing this in your code, it caused the Circle element to malfunction.

Therefore, if you can come up with a way to make the circle function properly without using a css position like that, then the issue should resolve itself. Apologies for not being able to find a straightforward solution for you.

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 .value property on the form group displays numeric values as either null or an empty string

I'm encountering an issue with extracting data from a form group. Within my code, there is a formGroup named lineitemForm, and I am attempting to structure this form group as follows: private formatTransferData() { const depositDates = this.get ...

Implementing specifications throughout the entire nodejs route

In my Nodejs app, I have a RESTful API where I need to check for a user's role before sending a response with data or 404 error. apiRouter.route('/users') .get(function (req, res) { var currentUser = req.decoded; if(curr ...

Creating perfectly spaced toast messages with CSS styling

I am trying to customize the alignment of my toast messages to the bottom left corner. Ideally, I want them to stack on top of each other with a small gap of 10px between each message, ensuring that they are all visible and readable. Currently, changing ...

Using Django Sessions for User Authentication in React Applications

Not a coding query, but rather a general one: When using default authentication in Django (session authentication), what does the frontend (in my case it's React) require? For instance, upon logging in on the browser via the front end login button, th ...

What is preventing the div container from extending to its full width?

The content on the right side of this div container is struggling to reach full width for some unknown reason. Click here to see an image of the issue. The right column is not extending to full width. This snippet shows my CSS code. The 'main-conte ...

How can I customize the appearance of a disabled radio button within spans?

While attempting to make my radio button square using CSS by incorporating spans and before and after elements, I encountered a new client request. The client now wants the radio buttons to be disabled with a unique style when in this state. Unfortunately, ...

ajax causing issues with comments display on rails platform

Having trouble rendering comments using ajax in my rails app. The comments are only displayed after redirecting the page, even though they are successfully created. The issue seems to be with rendering comments using ajax and jquery! In my application.htm ...

What is the best way to vertically center an amp-img element?

I'm attempting to vertically center an <amp-img> within a div. <div class="outer" style="height: 100vh"> <amp-img class="inner" layout="responsive"></amp-img> </div> I have tried various methods so far, but none seem ...

Delete item from file

In the case that the variable author is defined, an update is made to the mongoDB document to add a new object { assignTo: author }. This will either create or update the existing document. If author is empty, I need to remove the assignTo field from the ...

Challenges with conditional statements in JavaScript

This is a simple code snippet for a ToDo-List application. The function InputCheck() is designed to validate if the input bar contains any value. If the input bar is empty, the function should not trigger the addTodo function. However, in its current stat ...

The date format in AngularJS is not being displayed correctly according to the request

My goal is to retrieve the date in the format of dd/MM/yyyy and pass it along to my URI. However, I am encountering an issue where the date is not being passed in the requested format. Below is the snippet of my HTML code: <script type="text/ng-templat ...

What is causing the lack of HTML formatting in the emails being sent by my PHP contact form?

As I work on creating my custom contact form, I've encountered an issue with the emails not appearing in HTML format during testing. It's puzzling as other sources seem to send HTML formatted emails to my email service provider without any proble ...

How can I implement a CSS property on a child class when the parent class is toggled on and off?

Looking to style a child div within a parent div that toggles classes based on a flag. The parent div can have either the .pricing-section or .new-pricing-section class. Here's the structure of the parent div: const togglePriceSection = isPricingDetai ...

Guide onsetting up an Autocomplete textbox on a webpage using data from a database by utilizing a combination of Java servlet, jQuery, JSON objects, and an ajax request

Despite trying various methods, I am struggling to achieve this task. As a beginner in the realms of jQuery, JSON, and AJAX, I have written a servlet that connects to my database and fetches user IDs from a specific table. Additionally, I have designed a J ...

Adjustable diagonal line within a container using CSS styling

Seeking assistance with a rectangular div that can have its diagonal length adjusted using JavaScript. I am looking to add a diagonal line to label the diagonal length when it is increased or decreased using the +/- buttons. I require the line to resize a ...

Determine whether the object is facing the specified position

I'm attempting to verify whether an object (this.target) is facing towards a particular position (newPosition). Here's what I currently have: new THREE.Matrix4().lookAt( newPosition, this.target.position, this.target.up ) == this.target.matrix ...

The JQuery function assigning a value of 0 to the selectedIndex property is not functioning properly across all selected fields

<select name="wpcf-others" id="abc" class="myzebra-control myzebra-select"> <option value="wpcf-field123">General Work Jobs</option> <option value="wpcf-fields--1">Journalist/Editors Jobs</option> <option value="wpcf-4868b8 ...

Using CSS files in the web directory in CakePHP and connecting them to HTML

Could someone offer some help with cakephp? I'm experiencing an issue related to routes that I can't seem to resolve. I've organized all my css files in webroot/css within my app. In my default layout, I included a html link <link href=" ...

Issues with AngularJS routes not functioning correctly without any errors

Viewing File Structure <-- see attached image I am currently working on developing an AngularJS web application with ASP.NET MVC 4. Despite setting up my routes properly, I am encountering an issue where my partial view is not being injected even thoug ...

The element is implicitly classified as an 'any' type due to the index expression not being of type 'number'

Encountering a specific error, I am aware of what the code signifies but unsure about the correct interface format: An error is occurring due to an 'any' type being implicitly assigned as the index expression is not of type 'number'. ...