Is there a way to reverse the image without physically clicking on it?

I've created a JavaScript code for matching images in a 4x4 grid. The goal is to flip the images when clicked and then flip them back if they don't match. I've managed to flip the images initially, but there's an issue with flipping them back when they are not equal. Despite trying various approaches, the images aren't flipping back. Can you assist me in resolving this problem?

Below is my code:

HTML:

<div class="container">
    <div class="f1_container" onclick="choose(0)">
        <div class="shadow f1_card">

            <div class="back face center"><img src="A.png" onclick="Click()"></div>
        </div>
    </div>
    <!-- Add more image containers here -->
</div>

Javascript:


// JavaScript code goes here

CSS:


/* CSS styles go here */

Answer №1

If you're looking to control the flipping animation by changing a class, here's a suggestion. Start by setting up your card's normal styles in your CSS and then add transform: rotateY(180deg) when the extra class is applied.

This approach allows you to add the class when a card is clicked and remove it after a set timeout. By including a transform transition (e.g., transition: transform 500ms) on your card, you'll experience smooth transitions as the card flips both ways.

You can toggle this class using the click function and a predefined timeout in your script.

Below is an example of how you can dynamically add and remove classes using vanilla JavaScript along with a timeout function. Take note of how we add a flipped class to #wrapper using JavaScript for the desired animation effect.

Keep in mind that this solution utilizes element.classList, which is compatible with Internet Explorer 10 and above. If you need to support versions earlier than IE 10, explore options mentioned in this thread.

document.getElementById('wrapper').addEventListener('click', function() {
  var wrapper = this;

  wrapper.classList.add('flipped');

  setTimeout(function() {
    wrapper.classList.remove('flipped');
  }, 1500);
});
#wrapper {
  perspective: 1000px;
  width: 100px;
  height: 150px;
}
#wrapper.flipped .card {
  transform: rotateY(180deg);
}
.card {
  width: 100%;
  height: 100%;
  position: relative;
  transition: transform 500ms;
  transform-style: preserve-3d;
}
.card .front,
.card .back {
  width: 100%;
  height: 100%;
  border-radius: 15px;
  position: absolute;
  top: 0;
  left: 0;
  backface-visibility: hidden;
}
.card .front {
  background-color: midnightblue;
  z-index: 1;
}
.card .back {
  background-color: firebrick;
  transform: rotateY(180deg);
}
<div id="wrapper" style="float: left;">
  <div class="card">
    <div class="front"></div>
    <div class="back"></div>
  </div>
</div>
<p style="float: left; margin: 65px 0 0 10px;">&larr; click me</p>

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

Tips for parsing a JSON file within my node.js application?

I am working on a node.js service that involves validating JSON data against a schema defined in jsonSchema. Below is the code snippet for my service: app.post('/*', function (req, res) { var isvalid = require('isvalid'); ...

Modify the width measurement from pixels to percentage using JavaScript

Looking for some help with a content slider on my website at this link: . I want to make it responsive so that it adjusts to 100% width. I managed to make it responsive by tweaking some code in the developer tools, but now I'm stuck. The bottom two p ...

Once upon a time in the land of Storybook, a mysterious error message appeared: "Invalid version. You must provide a string

I keep encountering an issue while attempting to set up storybook. Can anyone help me figure out what's causing this problem? npx storybook@latest init • Detecting project type. ✓ TypeError: Invalid version. Must be a string. Got type "obje ...

Eliminate Source Mapping via Gulp

Using Gulp and Bower to incorporate JQuery into my project has been great, but I've run into an issue with the jquery.min.cs file. It contains a SourceMappingURL at the end of the precompiled file that I need to remove. My current Gulp setup simply c ...

Uploading information by merging form data with a fetch API reply

In my project, I am looking to merge two sets of data into a specific format and then make a post request. After using the fetch API, I received the following response: { "id":"some-id" } The JSON format of my form data is as follows: { "origin_ ...

Issue with Passing 'key' Prop to React Component in a Mapped Iteration

https://i.sstatic.net/qeFKT.pngI'm struggling with adding a key prop to a React component in a mapped array within a Next.js project. The issue lies in a Slider component and an array of Image components being mapped. Even though I have provided a uni ...

The server encountered a 500 Internal Server Error because it could not read the 'username' property of an undefined object

When attempting to register a user in a mongodb database using express, a POST call was made to localhost:3000/users/register The request body included: { "firstName": "Jason", "lastName": "Watmore", "username": "jason", "email": "<a ...

Receiving the Outcome of q's Decision - Resolving or Rejecting

I found an interesting example involving $q on the book Mastering Web Application Development with Angular. In this code snippet, I am curious about how to obtain the String result of either pizzaOrderFulfillment.resolve(...) or pizzaOrderFulfillment.reje ...

Ways to trigger an onClick event multiple times in React

I have a code where I pass it to the onClick event and I want it to execute 3 times: (this code basically selects a random product from products and then updates the state using Hooks to display it in the shopping cart. Instead of just one product, I want ...

Angular Directive - introducing a fresh approach to two-way binding and enable "pass-by-value" functionality

In a previous question, I inquired about the possibility of incorporating an attribute on a directive to allow for values to be passed in various formats, such as: <my-directive att> //Evaluates to true <my-directive att="true"> ...

Implementing jQuery for cross-domain simple authentication

I am currently facing an issue with my system development where I have created a dynamic data retrieval feature in Grails. Upon making a request to a specific controller action, such as "http://localhost:8080/foo/bar", a JSON list is returned containing th ...

In JavaScript, create a function that returns an object instead of an array when using

In the code snippet below, I am trying to work with file names: var completename = file.name; var regex = '/-\w+_/'; var filenameTest_components = completename.match(/-\w+_/); console.log(completename); console.log(typeof filenameTest_ ...

Why does the outcome of running this code vary each time?

I've been working on a code project to create 10 bouncing balls of different colors, but I'm encountering an issue where only one or two balls appear with different colors or the animation works perfectly only 1 out of 10 times. Any thoughts on w ...

Error message 'command not found' pops up after successfully installing a package like mocha or pm2 in nodenv

For instance, I encountered an issue with the pm2 command on my nodenv nodejs setup. Whenever I try to run the pm2 command, I get the following error message: $ pm2 -v $ bash: pm2: command not found Moreover, when I run: $ which pm2 No output is returned ...

Obtain the data from the hyperlink destination

Having some trouble extracting a value from an href link to use in my database operations. Unfortunately, I couldn't retrieve the desired value. Displayed below is the code for a button: <a class="btn btn-info" href="scheduleSetTime.php?id=&apo ...

Tips for updating a list of orders using keywords entered in a text input field

I have a collection of car objects and an input field that triggers a change event. When the value in the input field is updated, I want to reorganize the order of the cars. For example, if I enter "Tau" in the input box, I want the ford object to be plac ...

Data streaming using Node.js

Is it feasible to continuously stream data from the server to the client using Node.js? My goal is to send a single AJAX request to Node.js, establish a persistent connection, and stream data to the client for real-time updates on the page. Latest Update: ...

What is the best way to remove headers and footers programmatically in print pages on Safari using JavaScript?

Struggling with eliminating the header and footer on print pages in Safari using JavaScript? While disabling the header and footer manually can be done through print settings in most browsers, my aim is to automate this process with code to ensure that use ...

Customizing the appearance of columns in an antd table

Below is the table column configuration I am working with, where notes are rendered using a custom function: fieldDefs: (results, isJsonData) => [ { title: 'Notes', name: 'notesHTML', table: { render: SectionNotes, sear ...

Exploring ways to utilize functions and operators with the output of custom VueJS filters?

I have implemented a Vue filter called timeconverter to alter the date format of a string. My goal is to verify if the outcome of {{ time | timeconverter }} is before the present time. Is there a way to utilize JavaScript functions on the output generated ...