Spin CSS card with a button press

Is there a way to rotate a CSS card only by clicking on a specific button?

Currently, the card rotates when clicked anywhere on it.

How can I make it so that it changes rotation only on a button click?

I tried modifying the JavaScript code from ('.card') to ('#rotate') and adding an id to the button, but it didn't work.

Answer №1

To update your click handler, all you need to do is:

$('button').on('click', function(){
   $('.card').toggleClass('flipped');
});

Answer №2

$('#rotate').click(function(){
              $(".card").toggleClass('flipped');
            });
.animation {
      -webkit-transition: all 0.3s ease;
      -moz-transition: all 0.3s ease;
      -ms-transition: all 0.3s ease;
      -o-transition: all 0.3s ease;
      transition: all 0.3s ease;
    }

    .cardContainer 
    {
     
      -webkit-transition: all .3s ease;
      -moz-transition: all .3s ease;
      -ms-transition: all .3s ease;
      transition: all .3s ease;

      
      /*depth of the elements */
      -webkit-perspective: 800px;
      -moz-perspective: 800px;
      -o-perspective: 800px;
      perspective: 800px;

      /*border: 1px solid #ff0000;*/
      padding-left: 1%;
    }


    .card 
    {
      width: 99%;
      height: 200px;
      cursor: pointer;
      
      /*transition effects */
      -webkit-transition: -webkit-transform 0.6s;
      -moz-transition: -moz-transform 0.6s;
      -o-transition: -o-transform 0.6s;
      transition: transform 0.6s;
      -webkit-transform-style: preserve-3d;
      -moz-transform-style: preserve-3d;
      -o-transform-style: preserve-3d;
      transform-style: preserve-3d;
    }

    .card.flipped 
    {
      -webkit-transform: rotateY( 180deg );
      -moz-transform: rotateY( 180deg );
      -o-transform: rotateY( 180deg );
      transform: rotateY( 180deg );
    }
    .card.flipped:
    { 
    }

    .card .front,
    .card .back {
      display: block;
      height: 100%;
      width: 100%;
      line-height: 60px;
      color: white;
      text-align: center;
      font-size: 4em;
      position: absolute;
      -webkit-backface-visibility: hidden;
      -moz-backface-visibility: hidden;
      -o-backface-visibility: hidden;
      backface-visibility: hidden;
      box-shadow: 3px 5px 20px 2px rgba(0, 0, 0, 0.25);
      -webkit-box-shadow: 0 2px 10px rgba(0, 0, 0, 0.16), 0 2px 5px rgba(0, 0, 0, 0.26);
      box-shadow: 0 2px 10px rgba(0, 0, 0, 0.16), 0 2px 5px rgba(0, 0, 0, 0.26);
    }

    .card .back {
      width: 100%;
      padding-left: 3%;
      padding-right: 3%;
      font-size: 16px;
      text-align: left;
      line-height: 25px;
    }

    .card .back {
      background: #03446A;
      -webkit-transform: rotateY( 180deg );
      -moz-transform: rotateY( 180deg );
      -o-transform: rotateY( 180deg );
      transform: rotateY( 180deg );
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- begin snippet: js hide: false -->

<script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>

<div class="col-md-3 cardContainer">
                  <div class="card red">
                    <div class="front"><h3 class="cardTitle">Flip me!</h3></div>
                    <div class="back">
                      <div class="content">
                        <h3 class="cardTitle">Back side</h3>
                        <br/>
                        <p id="happy"></p>
                      </div>
                    </div>
                  </div>
                </div>

<br>
     <button id="rotate" type="button">Rotate card</button>

The solution provided rotates the button when clicked using $(this). However, to achieve the desired effect, you need to rotate the div instead.

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

What is a sophisticated method to hide an element from view?

My dilemma involves a dropdown menu and a list of elements that are initially set to hidden using CSS. When an item is selected from the dropdown, it becomes visible. However, I am struggling with finding a way to revert the previously selected element b ...

Selecting options in table is disrupted by filtering in ng-repeat

My table showcases selectable information, featuring parent rows and child rows. I am seeking a solution where only the parent rows are selectable if they have no children; otherwise, only the child rows should be clickable. Essentially, it's a selec ...

Establishing jQuery cookies for a button group

I've been attempting to configure jquery cookies, but I'm struggling to make it function correctly. Here is the code I am currently using... <div id="tabs-5"> <form> <fieldset id="units" name="units"> <legend>Units& ...

The jQuery .load() function does not seem to be functioning properly on secure HTTPS connections

After implementing an SSL certificate through Cloudflare on my website, I encountered an issue where a specific function returned an error code 0 and failed to load the URL content. How can I resolve this issue? $(function () { EnderReact(); }); functi ...

What is the best way to transfer information from a directive to a sibling element within the DOM?

Due to animation requirements, I decided to separate the directive and its value into different HTML elements. Once the directive is loaded, the separated element must be updated from the directive. How can I transmit the information from directive to ano ...

Data assigned to KendoUI chart must have keys that do not consist of numbers

When I input the following simple data into a KendoUI chart, the chart displays the data correctly. var data = [ {"state":"NY","abc":12312}, {"state":"AZ","abc":12312}, {"state":"CA","abc":12312}, ...

Finding out which side of a cube has been clicked---Let's identify the side

Currently, I am in the process of developing a navigation menu and I am looking for a way to detect which side the user has clicked. Is it possible to achieve this using raycasting or is there an alternative method that would work better? If you require i ...

Avoid constantly destroying the Bootstrap Popover

I am facing a challenge with retrieving data from two checkbox inputs inside a popover. The issue arises because the popover appears in the DOM when I click a button, but is removed when I click it again. It seems that the problem lies in the fact that Jav ...

Tips for successfully passing an object as a prop in nextjs

Struggling to understand how to pass an object as a prop using useState in Next JS. My javascript functions include a lorem ipsum generator, housed in a component called Paragraphs. This component requires two properties: Number of paragraphs Sentence le ...

The selected value is visible on the component, but once it is passed to the server action, it disappears

I am utilizing Nextjs14 and Supabase for my project. Currently, I have a select element where users can make a selection. The selected value is then passed as part of the formValue, which is expected to be received in the server actions as part of the For ...

Enhance your Selectize.js experience by retrieving additional data within the "onItemAdd" callback

TL;DR In order to have access to all the data within the selected JSON object in the onItemAdd function, I am seeking clarity. Currently, only the _id and name are accessible via the config variables. Currently, my Selectize.js function successfully retr ...

Flask does not provide a direct boolean value for checkboxes

After struggling for a week, I am still lost on where to make changes in my code. I need the checkbox to return a boolean value in my Flask application. Below are snippets of the relevant code: mycode.py import os, sqlite3 from flask import Flask, flash ...

Is there a way to eliminate the directional tag from the base css by utilizing a separate css file?

css, body, forms { text-align: right; } I need to remove this part of the code because it is causing alignment issues with my slider in IE7. ...

Centering content vertically in CSS

I am facing an issue with aligning buttons vertically inside a div. The number of buttons may vary, but I want them to be aligned vertically in a single column. Using "vertical-align: middle" does not seem to work. How can I achieve this? The height of th ...

Steps for including a class to a div

I recently implemented code from a tutorial found at in my layout. This tutorial provides instructions on how to incorporate fadeout effects into a bootstrap flash message. <% flash.each do |key, value| %> <div class="alert alert-block alert ...

Obtain information from Firebase and display it in a list

I've been struggling to retrieve my to-do tasks from a firebase database, but unfortunately, no data is appearing on my view. Surprisingly, there are no errors showing up in the console. My journey led me to the point of "saving data" in firebase. H ...

What is the process for setting an AMP-video as a full-page background?

My homepage features a captivating hero video in the background, but I've encountered an issue where the AMP video does not scale properly to fit the screen on mobile devices. Here's the code snippet causing the problem: <amp-v ...

Incorporate a section of text to flow around a floated image within a paragraph

https://i.sstatic.net/s5aFq.png How can I achieve the text wrapping above an image similar to the layout in the sunflower picture? I have tried using relative positioning and adjusting the top property, but it doesn't allow the text to wrap around th ...

What is the best way to showcase accurate information retrieved from mySQL?

I'm currently facing an issue with querying a database and displaying the results. The query is supposed to be based on input from an HTML form. Strangely, when I input a name like "John" into the form, even though there are 2 entries in the table wit ...

Showing a loading image before an Action is completed in ASP.Net MVC 5 can greatly enhance the user experience

This project is developed using ASP.Net MVC 5. The following code snippet demonstrates a simple integration of javascript/jQuery: <script> $(document).ready(function () { $("#textBox").focusout(function () { var phrase= $("#textBox").va ...