Can you help me understand how to ensure the CSS translate feature lands in a specific div, no matter its initial position?

I'm facing a roadblock in my journey to create a card game. The issue arises at the final stage of the Translate function implementation. In this game, the player is dealt 30 cards (I've simplified it to four for ease of programming), and upon clicking on a card, it should perform an animation using translate and rotate to reveal its value. However, as I only need to expose the values of three cards, those cards must end up in a specific location where they are clearly visible, even if the user selects consecutive cards.

The challenge lies in ensuring that the revealed card ends up in the div labeled Card1 because my current approach with translate does not take me directly to the endpoint but instead moves based on the starting point. Is there anyone who can guide me on achieving this through translation or suggest alternative methods?

const card = document.querySelectorAll(".card");
const card2 = document.querySelector('.card2');
let fragment = document.querySelector(".solve");

card.forEach((card) => {
      card.style.display = 'block';
      card.onmousedown = function() {
        card.classList.add('animate');
      };
 });
body {
  background-color: #100e17;
  font-family: sans-serif;
}

.container {
  position: absolute;
  height: 280px;
  width: 1200px;
  top: 60px;
  left: calc(28% - 300px);
  display: flex;
}

.card {
  position: relative;
  transition: all 1s ease;
  transform: perspective(600px);
  transform-origin: 100% 50%;
  transform-style: preserve-3d;
}

.card1 {
  display: flex;
  height: 260px;
  width: 200px;
  background-color: #17141d;
  border-radius: 10px;
  box-shadow: -1rem 0 3rem #000;
}

.card2 {
  display: flex;
  height: 260px;
  width: 200px;
  background-color: red;
  border-radius: 10px;
  box-shadow: -1rem 0 3rem #000;
  backface-visibility: hidden;
}

.animate {
  position: relative;
  transform: perspective(600px) translate(0px, 300px) rotateY(-180deg);
  transform-origin: 100% 50%;
  transition-property: transform;
  transition-duration: 3s;
}

.card .card1,
.animate .card .card2 {
  position: absolute;
  backface-visibility: hidden;
}

.card .card2 {
  transform: rotateY(-180deg);
}

.title {
  color: white;
  font-weight: 300;
  position: absolute;
  left: 20px;
  top: 15px;
}

.answer {
  position: absolute;
  height: 260px;
  width: 300px;
  top: 360px;
  left: 250px;
  display: flex;
}

label {
  color: white;
}

.solve {
  display: flex;
  height: 260px;
  width: 200px;
  background-color: #17141d;
  border-radius: 10px;
  box-shadow: -1rem 0 3rem #000;
}
<div class="container">
  <div class="card">
    <div class="card1">
      <h3 class="title">Card front 1</h3>
    </div>
    <div class="card2">
      <h3 class="title">Card back 1</h3>
    </div>
  </div>
  <div class="card">
    <div class="card1">
      <h3 class="title">Card front 2</h3>
    </div>
    <div class="card2">
      <h3 class="title">Card back 2</h3>
    </div>
  </div>
  <div class="card">
    <div class="card1">
      <h3 class="title">Card front 3</h3>
    </div>
    <div class="card2">
      <h3 class="title">Card back 3</h3>
    </div>
  </div>
  <div class="card">
    <div class="card1">
      <h3 class="title">Card front 4</h3>
    </div>
    <div class="card2">
      <h3 class="title">Card back 4</h3>
    </div>
  </div>
</div>
<div class="answer">
  <label for="solve">Card 1</label>
  <div class="solve" id="solve"></div>
</div>

Answer №1

My solution method goes like this:

const animateCard = (card, posX) => {
  card.classList.add('animate');
  setTimeout(() => {
    card.style.setProperty('transform', `perspective(600px) translate(${posX}px, 321px) rotateY(-180deg)`, 'important');
  }, 2500);
};

const cards = ['card01', 'card02', 'card03', 'card04'];
const positions = [392, 343, 292, 242];

cards.forEach((cardId, index) => {
  let currentCard = document.getElementById(cardId);
  let positionX = positions[index];
  animateCard(currentCard, positionX);
});

This process is repeated for each end point and for every individual card.

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

Symfony: Enhancing array elements with updated bootstrap progress bar

I'm facing a challenging issue that I need help with. Currently, I have a command set up to send a large number of emails (around 300 or more) every 5 minutes using cron in order to distribute a newsletter. My goal is to keep track of how many emails ...

Toggle the active class on the parent element when it is clicked

I'm encountering a problem with my JavaScript - attempting to make this function properly. Firstly, here is the desired functionality: 1.) Add or remove the 'active' class from the parent element when clicked 2.) When clicking inside the ...

Could it be possible that my consecutive POST and GET axios requests are gradually slowing down?

After chaining the POST and GET calls in my code, I noticed a slight slowdown and was curious if this is normal or if there's a more efficient approach. The delay in displaying the google map marker made me think that pushing the newly created marker ...

Get rid of the "clear field" X button for IE11 on Windows 8

After trying out the suggestions provided in Remove IE10's "clear field" X button on certain inputs? .someinput::-ms-clear { display: none; } I successfully removed the X button on IE 11 running on Windows 7, but unfortunately it didn&a ...

The validation errors in the form of CodeIgniter are not being displayed by the JavaScript variable

Currently, I am utilizing the built-in validation_errors() function in CodeIgniter for my login form. The validation errors are displaying correctly in my view, but when I try to echo them into a JavaScript variable within the script to customize notificat ...

Issue with jQuery Quicksand's CSS rendering in Internet Explorer and Firefox browsers, but not affecting Chrome

I'm attempting to achieve an icon-swapping effect using jQuery+quicksand. Although it works seamlessly in Chrome, I am encountering issues with IE and Firefox. Given that the more intricate quicksand demos function flawlessly across all browsers, I su ...

JavaScript for Acrobat

I am currently creating a form in Adobe Acrobat and incorporating additional functionality using JavaScript. I have been searching for information on the control classes for the form, such as the member variables of a CheckBox, but haven't found any c ...

Do not decode HTML content within an iframe; instead, extract the data directly from the HTML source

To expedite execution time, we have made the decision to not display the table in the iframe as it is invisible to the client. However, we still need to update the main page table by copying its contents. The approach we are taking is that the iframe shou ...

Tips for styling row headers in Vaadin with CSS

I am in need of assistance with a problem I am facing. Recently, I started learning the Vaadin framework and I am trying to apply CSS to the row headers (first cell of each row), but so far I have not had any success. Below is the code that I have been usi ...

Exploring Angular 4: Iterating Over Observables to Fetch Data into a Fresh Array

Context Currently, I am in the process of developing a find feature for a chat application. In this setup, each set of messages is identified by an index. The goal of the `find()` function is to retrieve each message collection reference from the `message ...

Tips for correctly mentioning a user while using message.channel.send in discord.js

Looking for guidance on tagging a user properly using message.channel.send in discord.js? Here's the code I'm currently working with: function replaceAll(str, find, replacer) { return str.replace(new RegExp(find, 'g'), replacer) ...

The task of mapping an array of objects with nested values using JavaScript is proving to

Attempting to convert an array of objects with nested values in child objects like: const objs = [{ "B": { "value": 1, }, "D": { "value": "45" }, "E": { "value": "234" }, ...

What is the best way to position two elements inline?

I was trying to create a container named project-item that looks like the image linked below: https://i.stack.imgur.com/6XzZ9.png My goal was to align the project logo and the title (h3) in one line, but I struggled to find a suitable solution. This is ...

Deleting a record by sending an HTTP request and then removing the corresponding object from an array in Vue.js

After successfully solving this particular issue with the help of @nils, I have encountered a new question that I hope someone can assist me with! My current situation involves having a list of records from which I can select and remove items with just on ...

When I attempt to run several promises simultaneously with Promise.All, I encounter an error

My code contains a series of promises, but they are not being executed as expected. Although the sequence is correct and functional, I have found that I need to utilize Promise.all in order for it to work properly. dataObj[0].pushScreen.map(item => { ...

How do I repeatedly invoke a function in JQuery that accepts two different arguments each time?

I have a collection of folders, each containing various images. The number of pictures in each folder ranges from 8 to 200. Folders are numbered from 1 to 20 and the images within them are also labeled numerically. My goal is to aggregate all these images ...

What is the best way to eliminate extra space at the bottom of glide.js?

Looking for assistance to eliminate the whitespace at the bottom of an image or slider. Can anyone help? Here is a preview of the image: https://i.stack.imgur.com/mEYY3.png If you'd like to take a look, here is the project code link: I had to down ...

Steps for adding a React Class Component into a Modal that is not within the React Tree

Our project is built using PHP MVC Framework and we initially used jQuery as our main JavaScript framework for handling UI activities. However, we have now transitioned to using React.js. My query is about how to inject or append a React Functional/Class-b ...

Show the present category name within breadcrumbs (utilizing angularJS)

Struggling to display category and vendor names on breadcrumbs? Utilizing the ng-breadcrumbs module but encountering difficulties in making curCategory and curVendor globally accessible. Tried various methods without success. Below is the HTML code snippe ...

vm.property compared to this.property

Although it may seem like a simple question, I am in need of some clarification. Currently, I have vuejs running on a single page of my website. The vm app is running in the footer script of the page without utilizing an app.js file or templates/components ...