Trigger CSS transformations when a button is clicked

I have a form with a button on the left side and an image on the right side. When the user clicks the button, I want the image to rotate around its Y axis and display another div showing the result. Here is the code snippet:

<div class="col-lg-4 col-md-12 col-sm-12 calculate__content">
              <form>
                <div class="calorie__button__area">
                  <button
                    type="submit"
                    class="button-secondary button__calculate"
                  >
                    BUTTON
                  </button>
                </div>
              </form>
            </div>
            <div class="col-lg-6 col-md-12 col-sm-12 calculate__content">
              <img
                  src="./assets/img/calculate.png"
                  alt="Weight Calculation Image"
                />
            </div>
            <div class="col-lg-6 col-md-12 col-sm-12 calculate__content__result">
              <div class="row">
                <div class="col-lg-8 col-md-12 col-sm-12 calculate__content">
                  <div class="result__box">
                    <div class="title">RESULT:</div>
                  </div>
                </div>
                <div class="col-lg-4 col-md-12 col-sm-12"></div>
              </div>
            </div>

Currently, the element calculate__content__result is hidden, but upon clicking the button, it should be displayed while hiding the image with a CSS effect that rotates it on its Y axis.

Can you assist me with this implementation?

Answer №1

If you want to rotate a div using CSS, you can utilize the CSS rotate transform.

This code snippet simplifies the process to demonstrate the basics. It includes setting the image as the background of the div instead of adding it as an HTML element.

The initial state of the div is set to rotateY(0) to establish a starting point for the transition. When the button is clicked, a new class is added to the div, changing it to rotateY(90deg). The transform origin is positioned on the left side to ensure rotation occurs around that point (default is center).

.container {
  position: relative;
  width: 40vmin;
  height: 30vmin;
}

#turnEl {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  transform: rotateY(0deg);
  transform-origin: 0 0;
  transition: transform 2s linear;
  background-image: url(https://picsum.photos/id/1042/768/1024);
  background-size: cover;
}

#turnEl.turnover {
  transform: rotateY(-90deg);
}
<button onclick="document.getElementById('turnEl').classList.add('turnover');">
  BUTTON
</button>
<div class="container">
  <div id="turnEl">
  </div>
  <div class="title">RESULT:</div>
</div>

Feel free to adjust the formatting and experiment with CSS perspective to achieve your desired result.

Note: This code assumes the result appears below the rotating element. If this is not the case, please provide further clarification.

Answer №2

To track customer button clicks, simply add a class to the parent element using Javascript.

Then, target the parent element and apply CSS styles to the child image class.

For example, if you add the class "active" to the parent element with the class "calculate__content", your CSS would look like:

.mychildclass1{opacity:1;}
.mychildclass2{opacity:0;}
.calculate__content.active .mychildclass1{opacity:0;}
.calculate__content.active .mychildclass2{opacity:1;}

You can also add CSS animations to enhance the effect.

Answer №3

Just a heads up, make sure to test how it functions. Feel free to customize the CSS according to your needs. I have simply provided instructions on applying CSS when clicked.

function myFunction() {
  document.getElementById("myDIV").className = "mystyle";
}
.mystyle{
 width: 300px;
    border-radius: 50%;
    height: auto;
   transform: rotateY(45deg);
}
<div class="col-lg-4 col-md-12 col-sm-12 calculate__content">
              
                <div class="calorie__button__area">
                  <button
                    type="button"
                    onclick="myFunction()"
                    class="button-secondary button__calculate"
                  >
                    BUTTON
                  </button>
                </div>
              
            </div>
            <div class="col-lg-6 col-md-12 col-sm-12 calculate__content">
              <img 
                  src="https://hover.blog/wp-content/uploads/2015/08/dot-online-1280x720.png" id="myDIV"
                  alt="Weight Calculation Image"
                />
            </div>
            <div class="col-lg-6 col-md-12 col-sm-12 calculate__content__result">
              <div class="row">
                <div class="col-lg-8 col-md-12 col-sm-12 calculate__content">
                  <div class="result__box">
                    <div class="title">RESULT:</div>
                  </div>
                </div>
                <div class="col-lg-4 col-md-12 col-sm-12"></div>
              </div>
            </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

Gatsby: A guide on inserting unadulterated HTML within the head section

Is it possible to insert raw HTML into the <head></head> section of every page in a Gatsby.js project? I need to add a string of HTML for tracking purposes, including inline and external script tags, link tags, and meta tags. For example, here ...

Unraveling JavaScript Object Literal Strings that resemble JSON

I'm encountering difficulty with decoding an array of strings that I initially thought were in JSON format. var result = [ "{gene: 'PEX2', go_bp: '0.766500871709', CombinedPvalue: '9.999999995E-4'}", "{gene: 'PE ...

Leverage socket.io in various routes within a node.js application

In my Node.js application, I have various routes defined in the router.js file. Now, I want to implement socket.io in every route to enable real-time communication between my Node.js and React.js applications. However, the structure of my Node.js applicati ...

Guide on transforming the popup hover state into the active state in vue.js through a click event

My code currently includes a popup menu that shows up when I hover over the icon. However, I need to adjust it so that the popup changes its state from hover to active. Intended behavior: When I click the icon, the popup should appear and then disappear w ...

What is the correct format for the data section in an AJAX request to ensure it functions properly?

I've been encountering some difficulty with this section. I have noticed that my list is receiving 'personName' and 'personLastName' inserted rather than the actual values from the text fields, but I am struggling to rectify this i ...

Using asp.net and Knockoutjs to make an asynchronous call to a JavaScript library prior to

I'm currently working on an old asp.net web application and integrating Knockoutjs. I have a specific requirement where, upon clicking the asp.net submit button, I need to call a JS library asynchronously to get a response before proceeding with the a ...

How can I alter the background color of the entire header in Ionic?

Having some trouble changing the color of my header. I successfully changed the color of the white header, but not the black header where the time is displayed. I know this is beyond Ionic and part of Android, but I have seen other apps that are able to ch ...

Retrieve CSS height using Jquery, based on the declared value rather than the computed value

In a previous version of jQuery, the default behavior was different. When I use .css("height") and .height(), it gives me the computed height instead of what I actually want. I only need the height value if it is explicitly declared in the CSS for that ele ...

The input field within the button stubbornly resists aligning with the right corner

Can anyone help me with using the class=float-right on the button labeled "Add to cart"? It seems to be not aligning properly, especially when I increase the width of the card. The "Add to cart" button is sticking to the "Descripti ...

Reposition the navbar nav to the top right corner of my website using Boostrap

I'm completely new to this subject. Currently, I've been following an online tutorial on HTML/CSS/Bootstrap. One thing I want to do is move the navbar to the top right of the page, like what you see in the image below. I manually added a mockup ...

Transform a string date in the format of YYYY-MM-DD into a Date object while maintaining the original date within the user's specific timezone

Currently, I am facing a challenge in trying to convert a string date formatted as YYYY-MM-DD into a date object that can be manipulated with matDatePicker. The issue that arises is that the date displayed always shows yesterday's date. After some inv ...

Updating choices in dropdown list depending on the selected options in another dropdown list

Is there a way to dynamically change the options in a second dropdown based on the selection made in the first dropdown using jQuery? <select id="Manage"> <option value="a">A</option> <option value="b">B</option> < ...

Tips for Storing Your Data Collection: A Guide on Different Storage Options

In the different parts of my app, I require access to a dataset containing timezones. Traditionally, I have stored such data in a class method like Timezone.all_zones (Ruby) for easy accessibility anywhere in my code. Is there a way to achieve this same ...

Is it a Mozilla Firefox glitch or something else?

After writing the code, I noticed a bug in Firefox and a small bug in Chrome. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> ...

Displaying a Bootstrap modal with empty editing information

I am facing an issue with my modal bootstrap where the data is not loading into the bootstrap even though it is being passed from the controller. The Grid on my page pulls in data from a list for Notes Object @foreach (var notes in Model) { <tr> ...

AngularJS Error: Attempting to access 'then' property of an undefined value

I have a function set up in my AngularJS service: var readFile = function (path, file) { console.info("Attempting to read the file with URI: " +path+ " "+file); $cordovaFile.checkFile(path, file).then(function (success) { ...

PHP can be time-consuming when echoing a large amount of HTML data

I am facing a challenge with processing 15k JSON products using PHP. When I var_dump the array, everything works fine. However, when I reduce the JSON data to 1000-1500 items, it only takes half a second. The user interface consists of two buttons that tri ...

"Creating a user-friendly time selection feature similar to Google Analytics in

Does anyone know of a time range selector that functions like the draggable time line section in Google Analytics? Google offers a time line selection feature using three blocks, which can be replicated with Jquery UI and other methods. However, achieving ...

choose to display on mobile devices as a dropdown menu rather than a modal window

On mobile devices, my select elements are currently displayed as modals with options. However, I need to change this appearance to resemble the dropdown list view observed on desktop devices. Your assistance is greatly appreciated. ...

What is the process for making a custom texture map to use in ThreeJS materials?

Recently, I acquired a fbx model that utilizes a UV map to efficiently texture the model. The model is composed of a single mesh that needs to be colored using 4 quadrants as shown in the image below: https://i.sstatic.net/RR6m3.png To achieve this color ...