Smooth transition back to the initial state

I recently implemented a CSS3 transition on a <div> element that is meant to toggle when clicked. I successfully achieved the desired transition on even clicks using jQuery's .toggleClass method, but I am struggling with smoothly reverting back to the original state on odd clicks.

Essentially, I am looking to have the <div> rotate back to its normal position and smoothly return to its original coordinates (using the top property) on odd click events, mirroring the smoothness of the animation on even clicks.

You can view a live demo in this FIDDLE

Here is my code :

HTML :

<div></div>

CSS :

  div {
    width:300px;
    height:100px;
    border-radius:2px;
    background:gold;
    position:absolute;
    left:40px;
    top:50%;
    margin-top:-1px;
}
div.anim {
    top:30%;
    -webkit-transform: rotate3d(0, 0, 1000, 45deg);
    -moz-transform: rotate3d(0, 0, 1, 45deg);
    -ms-transform: rotate3d(0, 0, 1, 45deg);
    -o-transform: rotate3d(0, 0, 1, 45deg);
    transform: rotate3d(0, 0, 1000, 45deg);
    -webkit-transition: top 0.5s ease-out, transform 0.5s ease-out 0.5s;
    -moz-transition: top 0.5s ease-out, transform 0.5s ease-out 0.5s;
    transition: top 0.5s ease-out, transform 0.5s ease-out 0.5s;
    -webkit-transition: top 0.5s ease-out, -webkit-transform 0.5s ease-out 0.5s;
    -moz-transition: top 0.5s ease-out, -webkit-transform 0.5s ease-out 0.25s;
    transition: top 0.5s ease-out, -webkit-transform 0.5s ease-out 0.25s;
}

JS :

$(document).ready(function () {
    $('div').click(function () {
        $(this).toggleClass('anim');
    });

});

Answer №1

Make sure to incorporate the transition within the base element. It appears you have duplicate transition declarations, so be cautious as the latter will take precedence over the former.

div {
    /* ... Other properties ...*/
    -webkit-transition: top 0.5s ease-out, -webkit-transform 0.5s ease-out 0.5s;
    -moz-transition: top 0.5s ease-out, -moz-transform 0.5s ease-out 0.25s;
    transition: top 0.5s ease-out, transform 0.5s ease-out 0.25s;
}

Check out the demo here

Answer №2

I discovered a clever way to reverse the animation:

Check out this FIDDLE

Here is the CSS code snippet:

div {
    width:300px;
    height:100px;
    border-radius:2px;
    background:gold;
    position:absolute;
    left:40px;
    top:50%;
    margin-top:-1px;
    -webkit-transition: top 0.5s ease-out 0.5s, -webkit-transform 0.5s ease-out;
    -moz-transition: top 0.5s ease-out 0.5s, -moz-transform 0.5s ease-out;
    transition: top 0.5s ease-out 0.5s, transform 0.5s ease-out;
}
div.anim {
    -webkit-transition: top 0.5s ease-out, -webkit-transform 0.5s ease-out 0.5s;
    -moz-transition: top 0.5s ease-out, -moz-transform 0.5s ease-out 0.25s;
    transition: top 0.5s ease-out, transform 0.5s ease-out 0.25s;

    top:30%;
    -webkit-transform: rotate3d(0, 0, 1000, 45deg);
    -moz-transform: rotate3d(0, 0, 1, 45deg);
    -ms-transform: rotate3d(0, 0, 1, 45deg);
    -o-transform: rotate3d(0, 0, 1, 45deg);
    transform: rotate3d(0, 0, 1000, 45deg);
}

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

Invoking a JQuery function when clicked

While this question may appear similar to others on this platform, I am facing a unique situation and struggling to make it work. It may be a simple solution that I am overlooking. Here is the basic dialog function that I want to trigger onclick with an "a ...

Creating a new grid row when changing the order of elements in the grid column arrangement

Dealing with HTML that is not under my control, I am attempting to rearrange the elements using CSS grid. Despite setting grid-template-rows: 1fr, the layout shows 2 rows with 3 columns each instead of a single row with 3 columns. .grid-container { b ...

stop unauthorized entry to the jquery post URL

I have a jQuery script that sends data to a .php script using post or get methods. However, I want to prevent direct access to the PHP script. If a user looks at the HTML source code, they could potentially access the PHP script by copying the URL from ...

I am interested in creating a text box that has the ability to gather user input and connect it to a search

I have been attempting to develop a text box that can collect answers and link them with the search URL. However, I am encountering issues where even though I can input text into the textbox, it is not being recognized by the script. The error message sh ...

Safari experiencing delays when repeatedly playing audio in quick succession

I'm attempting to overlay a sound on itself when a key is pressed. To achieve this, I discovered that cloning the sound and playing the new instance works: var audioPromise = sound.cloneNode(true).play(); Here is the live example: https://jsfiddle.n ...

Having trouble with the page layout in AngularJS

I am currently delving into Angular JS in order to fulfill some academic requirements. The issue I am facing is with the rendering of a landing page after successfully logging in through a login portal that caters to three types of users. Strange enough, w ...

Would using base64_encode be a safe choice when sending information to an API via an Ajax post request?

Looking for advice on securely sending details to an API using PHP and jQuery. The issue I'm facing is that sensitive information like usernames and passwords can be easily viewed in the source code. Currently, my code looks like this: <?php ...

Implementing transparency to clip-path using HTML and CSS

How can I apply opacity to the clip-path/clip area, similar to the image shown below? Please find my code snippet for reference: .item--clip .demo { width: 200px; height: 250px; } .item--clip .has-mask { position: absolute; clip: rect(10px, 19 ...

The CSS cursor attribute is failing to function as intended

My goal is to change the cursor for all the <a> tags, but when I check the website, the property doesn't seem to be taking effect. CSS a { color: #333333; /* color: #787878; */ text-decoration: none; cursor: pointer } Website Previe ...

Display the HTML/CSS layout following the JavaScript window.open action

Encountering issues with printing output in an opened window using JavaScript. I'm creating a new document through window.open and including CDN links to Bootstrap files. While everything appears fine in the opened window, when attempting to print (XP ...

Is it possible to enable auto-completion for IDs in a separate CSS file using WebStorm 7?

I am new to using WebStorm 7 and currently working on building a simple HTML/CSS website. Initially, I included my CSS within the HTML file using the style tag, but now I have decided to move it to a separate file. Auto completion is functioning for all h ...

Can you explain the distinction between "javascript:;" and "javascript:" when used in the href attribute?

Can you explain the distinction between using "javascript:;" and just "javascript:" within an anchor tag's href attribute? ...

"Encountering a strange issue where submitting a form with Jquery and AJAX in Rails does not work as expected, causing the index

Currently facing a unique issue with a jQuery/Ajax HTML update form. The application in question is a single-page TODO App that utilizes a Rails controller, where all changes to the DOM are made through jQuery/Ajax. After rendering the TODOs on the page v ...

Automatic updates are enabled for Google Chrome extensions

I noticed that all the extensions I analyzed had a specific code in their manifest.json, even though Google says you don't have to include it if you host your Chrome app on their servers. Should I also include this code in my Chrome extension manifest ...

Straightforward jQuery hover effect

I have created a simple jQuery image rollover function that was inspired by another one I found here: . This function identifies all images with "_off" in their name and swaps them with an image that has the same name but with "_on" instead of "_off". It& ...

Tips for determining the zoom factor through mouse scrolling using jQuery

Is there a way to zoom in on a page when the user scrolls using ctrl + ,? If I determine the Zoom factor, can I then zoom in on the current page based on that factor? For example, if the zoom factor is 1.44, can I convert this to 144% and carry out the ...

The iOS website won't fully load until the button is tapped two times

- (IBAction)saveButton:(id)sender { NSURL *yourWebURL = [NSURL URLWithString: webpageURLLabel.text ]; NSURLRequest *webRequest = [[NSURLRequest alloc] initWithURL:yourWebURL]; if ([self checkIfValidURL] == YES) { [webpagePreview loadReq ...

JavaScript allows you to export a variable from a function

I simply want to access the value of the userId variable for any use. async function getMyData(){ const token = ''; spotifyApi.setAccessToken(token); const data = await spotifyApi.getMe(); let userId = data.body.id; retu ...

What causes certain webpages to experience delays? (Advertisement impact)

Whenever I visit low-quality websites filled with ads, I often experience a delay where part of the site loads and then stalls for seconds before loading the rest. However, when I use AdBlock, the site loads much faster. What exactly is causing this stal ...

Interested in mastering the art of storing data in forms using PHP? Ever wondered why this functionality doesn't seem to function in an HTML file?

Hello, I'm new to coding and I need some help. Recently, I learned how to create forms in HTML and now I want to save and display the data using PHP. I followed along with some PHP exercises on a website called W3Schools, but when I tried to run the f ...