Apply a class to a div once another class with animation has been applied

Recently, I created a button that triggers the addition of a class with animation to an image upon clicking. Now, I am trying to make another button add a different class to revert the image back to its original size. However, the second class doesn't seem to be getting added properly. Even after attempting to remove the previous class, it remains stubbornly stuck on the image.

Is there any way to successfully apply the second class to the image?

$(document).ready(function() {
  $('#profile').click(function() {
    $('.imageone').addClass('move');
    $('.imageone').removeClass('imageone');
  })

  $('#back').click(function() {
    $('.imageone').removeClass('move');
    $('.imageone').addClass('goback');
  })
})
.move {
  animation: scale 1s ease-in-out forwards;
  z-index: -1;
}

@keyframes scale {
  from {
    position: relative;
    z-index: -1;
    bottom: 0pt;
  }
  to {
    position: relative;
    bottom: -142pt;
    transform: scale(1.16, 1.23);
    left: 0pt;
    z-index: -1;
    transform-origin: bottom right;
  }
}

.goback {
  animation: goback 1s ease-in-out forwards;
  z-index: -1;
}

@keyframes goback {
  from {
    position: relative;
    bottom: -142pt;
    transform: scale(1.16, 1.23);
    left: 0pt;
    z-index: -1;
    transform-origin: bottom right;
  }
  to {
    position: relative;
    z-index: -1;
    bottom: 0pt;
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="pageone">
  <img src="images.png" class="imageone" alt="liam">
</div>
<div id="textprofile">
  <a href="#" class="button" id="border">
    <p id="profile">Visit Profile</p>
  </a>
</div>
<p id="back">go back</p>

Answer №1

Having two elements with the same id can cause issues when adding an event listener using an ID selector in JavaScript. IDs should be unique on HTML pages to ensure proper functionality. Consider changing your selector and updating the IDs for each element.

Answer №2

To update the styling of an element, you must first remove the existing class and then add a new one. If you simply add a new class without removing the old one, both classes will be applied simultaneously. In this case, trying to remove the class "imageone" after adding it will not work as expected. To fix this issue, make sure to remove the class "imageone" before adding the new class "move". You can achieve this by giving the img element an ID and using JavaScript like this:


$(document).ready(function() {
  $('#profile').click(function() {
    $('#imageone').removeClass('imageone');
    $('#imageone').addClass('move');
  });

  $('#back').click(function() {
    $('#imageone').removeClass('move');
    $('#imageone').addClass('goback');
  });
});

Answer №3

please give this code a try.

$(document).ready(function() {
  $('#profile').click(function() {
    $('.imageone').addClass('move');
    $('.imageone').removeClass('goback');
  });

  $('#back').click(function() {
    $('.imageone').removeClass('move');
    $('.imageone').addClass('goback');
  });
});

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

In my Django html file, I am facing an issue with the IF statement that seems to be dysfunctional

I have a like button and I want it to display an already liked button if the user has already liked the post. Here is my HTML: {% for post in posts %} <div class="border-solid border-2 mr-10 ml-10 mt-3 px-2 pb-4"> & ...

What is the best way to display numerous images on a cube face while also organizing them like a gallery wall?

I'm working on creating a unique gallery wall using three.js. Currently, I have successfully rendered an image on each side like this: My goal is to place multiple images on each wall instead of just one large image. Something similar to this concept ...

How come my items are not appearing on my SharePoint list?

I'm currently working on transferring JSON data to a SharePoint list. The JSON is coming from a database with a 500-item limit per request, so I'm using EPOCH time to make multiple calls and gather all the data. While I can successfully retrieve ...

In what location is it possible to define this CSS property?

After inheriting a website, I am encountering CSS issues that are driving me crazy. There is a mysterious div element with a height of 185px that is causing content truncation problems. The strange part is that this height is not defined anywhere in the st ...

Display a bootstrap toast using a JavaScript class method

One way to display a bootstrap toast is by using the following code snippet: let my_toast = new myToast('title', 'hello world'); my_toast.show(); Currently, I am loading the HTML content from an external file named myToast.html: < ...

Tips on appending a search URL with "&*":

I need to create a search bar that connects to google.no (Norway). When searching for music, the URL should look like this: *. Can anyone help me understand how to add the (& *) at the end of a search? .search { border: solid 1px grey; border-ra ...

Guide to aligning a component in the middle of the screen - Angular

As I delve into my project using Angular, I find myself unsure about the best approach to rendering a component within the main component. Check out the repository: https://github.com/jrsbaum/crud-angular See the demo here: Login credentials: Email: [e ...

Updating CSS stylesheets dynamically when onchange event occurs

I am currently able to dynamically change style rules using JS, but I feel that this is limiting. I would prefer to be able to swap entire CSS stylesheet files easily. Do you have any suggestions on how to accomplish this? Here is my code: <label>T ...

The dreaded Heroku Node.js error H10 strikes again: "Application crashed"

I recently embarked on my journey to learn NodeJS and attempted to deploy it on Heroku. However, when I used 'heroku open,' the following error log appeared: 2020-10-08T14:19:52.778660+00:00 app[web.1]: at Module.load (internal/modules/cjs/loa ...

Organizing NodeJS modules in a way that mirrors Maven's groupId concept

I am making the switch to NodeJS after working extensively with Maven. In Maven, we are familiar with the groupId and artifactID, as well as the package name when starting a new project. However, in NodeJS, I only see a module name. The concept of groupI ...

What could be the reason why document.getElementsByClassName does not seem to be functioning properly within

I've come across this code snippet const desc_object = document.getElementsByClassName("singlePostDesc"); console.log(desc_object[0]); This is how it appears in JSX {updateMode ? ( <> ...

Encountering difficulty when trying to parse an HTML file as JSON using AJAX, resulting in an 'unexpected token' error

I have a Siemens s7 1200 PLC with a webpage on it that is structured like a json file (even though it is in html format to allow the PLC to modify variables). I am attempting to retrieve this data from an external web server using an ajax request. The only ...

How to update state value from axios response using useState() and useEffect() hooks in React

While running my test, I noticed that the axios call is properly mocked, but for some reason, setParticipant() never seems to update the value of the participant variable. I suspect this might be due to its asynchronous nature. How can I ensure that the se ...

The box shadow feature does not function properly when the position is set to sticky

Applying position sticky to th and td in the example below seems to be causing issues with the box shadow not working. Can anyone provide insights on why this is happening? <div class="overflow-x-auto lg:overflow-visible"> <div> <t ...

Mixing elements from the entire webpage

My goal is to create a cohesive look for the entire page by applying a background gradient to all layers. #myDIV { width: 400px; height: 400px; background-image: linear-gradient(-45deg, rgba(251, 234, 188, 1) 0%, rgba(0, 114, 136, 1) 100%); } .bl ...

Using lxml to extract data from dynamic HTML elements

Struggling to extract information from a dynamic field on an HTML page using the lxml library. The code snippet I've been working with is as follows: from lxml import html import requests page = requests.get('http://www.airmilescalculator.com/d ...

Display the names of the files and give the user the option to delete them prior to finalizing the form submission

I'm currently developing a webpage where users can upload files and send them along with a message. One issue I've come across is that when attempting to upload a file or send an attachment (single or multiple), the code snippet below is required ...

Is there a way to download and store the PDF file created using html2pdf in Node.js on my local machine?

I have successfully generated a PDF using html2pdf, but now I want to either send it to my server in Node.js or save it directly onto the server. Currently, the PDF is downloaded at the client's specified path, but I also need a copy saved on my serve ...

Tips for filtering array elements in MongoDB based on element dataIf you want to eliminate certain elements from an array in MongoDB based

Seeking guidance on writing a Mongo query to remove elements from an array based on specific data. { "_id": ObjectId("ajdi293akjf83rhfsf398"), "one": "oneData", "two": [ { "_id":ObjectId("akjf82ijikfj83jkfkj3"), "valu ...

Adding the target='_parent' attribute to the infowindow of a Google Fusion Map by utilizing a click event listener

As someone who is relatively new to Google Fusion, I am eager to expand my knowledge and skills. Recently, I created a customized map with an infowindow and embedded it onto my website using the FusionTablesLayer Wizard. I wanted to avoid using the target= ...