How can I make a div move to the position of another div and stay with it when the screen is resized?

In my card game project, I am dealing with an issue where cards are not aligning properly with the designated "dropZonePositions" when the screen is resized. Despite creating animations for the card movement that I'm satisfied with, the problem arises during resizing.

The "dropZonePositions" are being calculated using the CSS Grid Layout Module which adjusts to screen size changes. However, the positioning of the cards is handled by jQuery's .animate() function, which sets static values for "top" and "left". These values do not dynamically adjust as the screen size changes.

I've attempted various solutions such as appending the card-divs to the dropZonePosition-divs after animation or removing the style attributes added by .animate(), but none have provided a satisfactory fix.

My query: How can I ensure that the cards align correctly with their "dropZonePositions" even when the screen is resized?

Check out the code example here

let deck =[ 
  // Array of card objects omitted for brevity
  
]

function shuffleCards(array) {
    // Function logic for shuffling array elements
}

// Rest of the JavaScript code handling card animation and placement

/* CSS code styling the game elements */
#topCardContainer {
  // CSS rules for styling the top card container element
}

// More CSS styles for various game elements

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="topCardContainer">
  // HTML structure for card game layout, elements, and containers

Answer №1

This approach eliminates the need for a resize event. Instead, you can utilize an animate callback function to execute specific actions. I employed appending the animated card within the container and then setting its top and left values to zero. Additionally, make sure to set the parent container to relative in order to contain the cards effectively.

Take a look at this sample:

let deck = [{
    card: 1,
    value: 1,
    color: "hearts"
  },
  // other card data here
]

function shuffleCards(array) {
  // shuffling logic here
}

// additional JavaScript code here
#topCardContainer {
  display: grid;
  /* other styling properties here */
}
/* more CSS styles here */
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="topCardContainer">
  <div id="deckZone" card=""></div>
  <div class="dropzone" id="dropzone_1" data-row="1" data-column="1" card=""></div>
  <!-- other HTML elements here -->
</div>

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

Prevent jQuery from hiding elements when hovered over. Implement clearTimeout when hovering over an element, and use setTimeout when moving

I am attempting to create a sidebar with two navigation buttons that hide after a delay when the mouse cursor stops moving. However, I'm encountering an issue - how can I prevent these elements from hiding when they are in a hover state? Where should ...

Is the value of a field still accessible after it has been hidden using jQuery?

Hey there! I'm currently using jQuery('').hide() to conceal a field in one of my applications. I have a question: If the hide function is used, can we still access the value within the hidden field? The purpose of my code is to extract the v ...

Deleting the previous ion-view from DOM in Ionic v1 with Angular

I have been working on modifying an app built in Ionic v1. Within the app, there is a primary View titled 'Client's Profile' that contains links to two other Views: Support Request and Complaints. In the Support Request view, there is a Te ...

What is the best way to pass the "$user" variable in a loadable file using ajax?

Is there a way to send the variable user to the mLoad.php file before executing this script? Any suggestions would be greatly appreciated. Thank you! $(document).ready(function(){ $("#message_area").load('mLoad.php'); $("#userArea").submit(func ...

The CSS formatting is not being properly applied within the innerHTML

I have a scenario where I am trying to display a Bootstrap card using innerHTML in my TS file, but the styles are not being applied to this content. I suspect that the issue might be because the styles are loaded before the component displays the card, cau ...

Integrating Labels on External Websites with ASP.NET

I have been developing a website that needs to display a counter on the home page. To accomplish this, I used an asp label that counts and displays the database records (e.g. 180000) with no issues at all. However, now I am facing a new challenge. I want ...

Using AJAX to send both data input values and file attachments simultaneously to a PHP server

I am currently having an issue with sending form values to my PHP page using AJAX. My form includes text inputs and a file input for image uploads. Below are the AJAX codes I have been working on: function sendval() { var form = $('#user_update_for ...

Ways to implement schema.org and JSON-LD for data labeling on a homepage of a website

After reading extensively on utilizing schema.org to mark structured data, I have a couple of questions. Firstly, is it advisable to use json-ld considering that it's relatively new and not fully supported yet? Secondly, how can I implement schema.org ...

The majority of the sliding banner is crafted using 90% HTML and CSS3, with just a touch

I devised a clever script to smoothly slide an image back and forth across the screen using CSS3. Unfortunately, I encountered a problem with CSS animations - they do not support changing the background-image directly. Attempting to overcome this limitatio ...

In React Typescript, the input type="checkbox" does not show any value in the value attribute

I'm facing an issue with displaying text next to a checkbox in React Typescript. When I try to use the value attribute, it doesn't seem to work as expected. Also, attempting to set innerHTML throws an error stating that input is a void element ta ...

Steps to dynamically populate a datatable with JSON data by triggering a click event in jQuery

I am facing an issue with feeding JSON data into datatables when the search button is clicked. The JSON data structure is as follows: [ { "port_code":"BOM", "cont_details_id":"9", "price":"44.000", "cont_price":"500", "c ...

Use jQuery to extract data from the URL instead of relying on PHP

http://h3gsat.altervista.org/DettagliCompleto.php?id=4567 I attempted to extract the id parameter without success. I am attempting to accomplish this using JavaScript (jQuery) instead of PHP, but I am unsure of how to proceed. ...

Is it possible to redirect any web traffic using an authentication script?

Scenario: I'm currently working on a social networking project and I'm looking for a way to validate every redirect or refresh by directing the user through another script before reaching their final destination. I've considered using a &apo ...

Choosing a single item from multiple elements in React using React and typescript

In this particular project, React, TypeScript, and ant design have been utilized. Within a specific section of the project, only one box out of three options should be selected. Despite implementing useState and toggle functionalities, all boxes end up bei ...

Implementing the Applet Param Tag using JavaScript or jQuery

<applet id="applet" code="myclass.class"> <param name="Access_ID" value="newaccessid"> <param name="App_ID" value="newappid"> <param name="Current_Url" value="newticketurl"> <param name="Bug_ID" value="newbugid"&g ...

I am experiencing issues with the interpolation of my SASS variables when they are placed into

I am relatively new to the Nuxt ecosystem and I must say it's an awesome package that really simplifies our lives. Currently, I am attempting to incorporate sass into my project. Despite following the steps outlined in the documentation, my build is ...

The system has removed all content within the fields

I have created a code to generate a dynamic table. The problem I am facing is that when there are multiple rows in the table, clicking on the delete button deletes all field values instead of just deleting the data for the specific row where the delete b ...

"Send the selected radio button options chosen by the user, with the values specified in a JSON format

My current task involves inserting radio button values into a MySql database using Angular. The form consists of radio buttons with predefined values stored in a json file. Below is an example of how the json file is structured: //data.json [{ "surve ...

What is the process for generating and parsing an XML file using jQuery?

I've come across a lot of information on creating and sending XML files with Ajax, but I'm feeling pretty confused. My goal is to generate an XML file and send it to the server using Ajax. In return, I expect to receive another XML file. Below ...

utilizing jQuery to iterate through JSON data with a random loop

Is there a way to modify this code to display only one image randomly selected from a JSON file that contains multiple images? Here is the code in question: $(document).ready(function() { $.getJSON('https://res.cloudinary.com/dkx20eme ...