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>

https://i.sstatic.net/hlvXV.jpg

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

Enable the parsing of special characters in Angular from a URL

Here is a URL with special characters: http://localhost:4200/auth/verify-checking/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="59663c34383035643230383d2b606a6e6b686d6e6e193e34383035773a3634">[email protected]</a> ...

Trying to understand the strange behavior of HTML parsing with jQuery in Javascript and Firefox

I have been working on a script to parse an HTML page using jQuery. The script runs smoothly in Chrome, IE, and Safari, but I'm facing some unexpected behavior while testing it in Firefox (version 36.0.1). Here's the code snippet: $.ajax({ u ...

Stable element placement in relation to its container

While dragging an element, it obtains a position: fixed. This particular element is located within a modal that is directly nested inside the body element. In the image below, the modal appears in gray, while the rest of the body is black, and the button ...

No output is displayed in the absence of errors. The program is functioning correctly

app.js angular.module('app', ['ionic', 'ui.router']) .config(('$urlRouterProvider', '$stateProvider', function($urlRouterProvider,$stateProvider){ $urlRouterProvider.otherwise('/'); $sta ...

How to avoid property sharing in Angular recursive components

I am currently working on a recursive component that generates a tree structure with collapsible functionality. However, I am facing an issue where the state variable active is being shared among child components. Is there a way to prevent this from happen ...

Reorganize child JSON objects into a new object that includes a parent ID

Exploring the realm of JavaScript, I am currently delving into Node.JS to interact with an API and save the data in a SQL Server. Utilizing the "request" and "mssql" Node packages for this task as they possess robust documentation and support. My query re ...

What is the best way to elegantly finish a live CSS animation when hovering?

Currently, I am working on a planet orbit code where I want to enhance the animation speed upon hover. The goal is for the animation to complete one final cycle at the new speed and then come to a stop. I have been successful in increasing the speed on hov ...

Filtering Results Efficiently Based on Various Criteria in React

To view my project in action, please visit my sandbox here. Due to limitations, I am unable to paste all of the code directly here. The goal of my project is to create a tours filtration system based on multiple criteria. Each tour is represented as an ob ...

Utilizing AngularJS to calculate elapsed time and continuously update model and view accordingly

Situation Currently, I am interested in developing a web application that tracks a specific data set based on the time since the page was loaded. For example, "how many calories have you burned since opening this webpage?" I am still trying to grasp the ...

Every time I invoke a module, non-global variables are utilized

Within a module.exports file, I am facing an issue where the variables are shared among different instances of the module. Instead, I want each call to the module to have its own set of values for cycle number, original data, new product, etc., similar to ...

- Challenges with internal systems

I have a dialog window where I want to display a confirm dialog when clicking Cancel. To achieve this, I am creating a div element with some text that should be shown in the confirm dialog. However, the issue I'm facing is that the text intended for t ...

Working with iFrames through Splinter/Selenium in Python

Just a heads up: I can work with either Selenium or the Splinter API wrapper for Selenium! I've encountered some hurdles while trying to navigate iframes on Twitter.com using the Python Splinter API. For instance, with Browser('firefox', ...

My website is being cut off

After creating this website with a viewport setup, I noticed that it is not fully visible on certain screens. When viewed on a CRT monitor at 800x600 desktop resolution or lower than 1280x800, such as on mobile devices, the content gets clipped. Is there a ...

Using tokens to make consecutive API calls in JavaScript/Node.js

After generating the token, I need to make sequential calls to 5 different APIs. The first API used to generate the token is: POST https://idcs-xxxx.identity.c9dev2.oc9qadev.com/oauth2/v1/token Using the username and password, I will obtain the token from ...

Problem with keyframe animations in Internet Explorer 10

My CSS animation works flawlessly in all modern browsers, except for IE10 which is causing some trouble. It appears that IE is having difficulty properly rotating 360deg. I am still searching for a workaround that won't compromise the functionality in ...

Is it possible to retrieve the width value when using layout=fill in next/image?

<Image alt="image_example" src="/image_example.png" layout="fill" objectFit="none" /> <Test width={?} height={?}/> I developed a straightforward component using next/image. I am interest ...

Adjust the Placement of Images in Relation to Mouse Movements

Is there a way to creatively animate the positions of images or background images based on mouse movement? Let's take Github, for instance: https://github.com/thispagedoesntexist The 404 page on Github is truly impressive. I aim to captivate my use ...

The Wonders of Flex Box and Media Queries

Can a website still be made responsive without relying on media queries when flexbox is already implemented? Are there specific scenarios where the utilization of media queries offers enhanced control? ...

Interact with a JSON API using JavaScript by incorporating dynamic user input

Recently, I delved into tutorials on utilizing JSON data and JavaScript. Feeling inspired, I decided to create a simple app using an API. However, I encountered a hurdle and I'm struggling to identify its cause. The problem seems to be related to how ...

Why is jQuery not defined in Browserify?

Dealing with this manually is becoming quite a hassle! I imported bootstrap dropdown.js and at the end of the function, there's }($); Within my shim, I specified jquery as a dependency 'bootstrap': { "exports": 'bootstrap', ...