Reversing JQuery animations can be achieved by using the `

As someone who is relatively new to JQuery and web development, I find myself in need of assistance. Despite researching for a few days, I have not been able to find a solution that meets my requirements. While browsing through various forums, I noticed that similar questions have been asked numerous times, but most of the answers provided are outdated given the current release of JQuery.

The issue at hand is that I have a 'div' element that I want to be centered on the screen when clicked. This functionality is working as expected. However, I am struggling to make the same 'div' return to its original position when clicked for the second time.

$(document).on("page:change", function() {
  $("#profile").on ("click", function() {
    $("#profile").animate({right: '15%', height: '+=50px', width: '+=25%'});
    $("#profile").css("position", "absolute"); // To maintain normal position within bootstrap grid during animation
    $("#fadeback").css("opacity", 0.6).fadeIn(300); // Fades other elements on the page
  });
});

I am primarily looking for a way to revert these operations upon a second click. If you have an alternative approach that differs from my current implementation, I am open to exploring it. Thank you in advance for your assistance!

Answer №1

Here is a suggestion for you to try:

function initialAction() {
    alert('Execute your animation here');
    $(this).one("click", reverseAction);
}
function reverseAction() {
    alert('Reverse your animation here');
    $(this).one("click", initialAction);
}

$("#profile").one("click", initialAction);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="profile">
Click me
</div>

Integrate this code with yours:

function initialAction() {
   $("#profile").animate({right: '15%', height: '+=50px', width: '+=25%'});
    $("#profile").css("position", "absolute"); // Maintain div's position in Bootstrap grid during animation
    $("#fadeback").css("opacity", 0.6).fadeIn(300); // Fades other elements on the page
  });
}
function reverseAction() {
// Reverse the animation here
}


$(document).on("page:change", function() {
$("#profile").one("click", initialAction);
});

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

Icon: When clicked, initiate a search action

I am looking to make the icon clickable so that it can be used as an alternative to pressing the "return key" for searching. Check out this bootply example at . You simply need to click on the magnifying glass icon and it will initiate the search. ...

What is the best way to retrieve the items stored within the array generated by a JavaScript function?

This particular function is responsible for calling a PHP script that outputs JSON data. It then iterates through the JSON object, creates a new object, and adds this new object to an array which is ultimately returned by the function. function getTestQues ...

The attribute selector appears to be malfunctioning due to a syntax error, resulting in an unrecognized expression

I have a function in SSJS that goes through an array called "ps_data" and one of the key value pairs contains a URL encoded value which is causing issues with my Jquery code. It's important for this value to be passed exactly as it is. $(document).r ...

Tips for customizing WTForm fields with unique CSS classes

I've recently put together a basic form snippet: class PostForm(FlaskForm): # for publishing a new blog post title = StringField("Title", validators=[DataRequired()]) submit = SubmitField('Post') The structure of my HT ...

dompdf - adjust page margins starting from the second page

Currently, I am in the process of converting an HTML document to a pdf file using dompdf. Everything is running smoothly, except for one issue - I need to implement page margins for all pages, except the very first one. The first page should ideally have a ...

Using Jquery to dynamically adjust select options based on the value of another select option

My goal is to create a short form where each select box dynamically populates the next one. The options for the first select box are hardcoded in the body, while the subsequent options are defined as an HTML string in a variable corresponding to the value ...

Styling with CSS in Zend Framework

In the process of creating a website using Zend Framework, I have successfully implemented the header, footer, and all the necessary pages. However, I am facing an issue with integrating the CSS file located at public/css/styles.css. When running the webs ...

What are the best ways to incorporate EMs and percentages into responsive web design?

As I delve into the world of responsive design, I am eager to understand the most effective practices when it comes to utilizing em and percentage units of measurement. In a scenario where I set the font-size of the body to 10px, could this potentially le ...

Can you explain the distinction between <P> and <p> tags in HTML?

Currently, I am examining HTML documents by parsing them through Java and I have noticed that the p tag is one of the most frequently utilized tags. While I understand that it is used to create a new line, I am puzzled by the fact that in some documents I ...

Apply styles specifically to elements that contain a child element

I have a scenario where there are multiple <p> elements with the same class names, but only one of them has a child element. My objective is to highlight only the <p> that contains a child, however, my current code ends up highlighting all of t ...

Divergent behavior of SVG elements in Firefox versus Chrome with HTML5

I am trying to position a 'div' under a 'textarea' within an 'svg' and 'g' element. This works fine in Firefox, but not in Chrome. When I apply a transform on the 'g' element, it moves correctly to the des ...

Changing text size on the fly with CSS

Here are the HTML and CSS codes I am currently using: HTML : <div> <a href="#">ITALIANO</a> <a href="#">ENGLISH</a> <a href="#">FRANÇAIS</a> <a href="#">DEUTSCH</a> </div> CSS ...

How can I make a div move to the position of another div and stay with it when the screen is resized?

In my card game project, I am dealing with an issue where cards are not aligning properly with the designated "dropZonePositions" when the screen is resized. Despite creating animations for the card movement that I'm satisfied with, the problem arises ...

polygon with five or more sides

Can divs be created with shapes such as five or six corners? I am looking to create clickable zones on a map and any alternative methods would be greatly appreciated. ...

a fixed width layout with two columns aligned at the top

I've been struggling to design a two-column, top-aligned layout with fixed width for data that will be inserted into text in a WebView for an iPhone app. The first column needs to have a set width so the items in the second column can align perfectly. ...

Align the content at the center within a Bulma column

I am working with multiple columns, each column containing a circle in the CSS depicted as a font awesome icon centered within it. However, I am facing an issue where the circle remains aligned to the left while the text itself gets centered. HTML <di ...

How to implement downloading a PNG image with a click using Bootstrap 4 and JavaScript

Whenever I click on a picture, my goal is to immediately download it. The image is SVG, but there's an URL for a PNG version. It has been confirmed that the content disposition is set to attachment, and when the URL is directly copied, a download proc ...

Instructions on adding a class to the parent element when the child has a particular class

Here is the html structure I am working with: <section> <div class="v-middle"> <div class="row"> <h5 class="heading">Heading goes here</h5> </div> </div> </section> ...

What is the best way to send a List in the model as a parameter for an AJAX request?

I'm currently using MVC 4 ASP.net with Razor views and I have an issue with refining my search results using AJAX calls. The current approach involves creating methods in the controller that include all the search filters, which has resulted in a meth ...

When the page height is altered in real-time, it prompts a scroll event

When a slider on the page automatically switches slides and changes its height, it causes a scroll event to trigger unexpectedly. I want to modify the scroll event so that it only activates when the user physically interacts with the page by scrolling, r ...