Generate a dynamic animation by combining two images using jQuery

My attempt to animate two images is not working. I received some help on Stack Overflow but still facing issues with my CSS and HTML code.

This is the code I am using:

$(document).ready(function() {
    $(".animar").click(function() {
        $("#img4").addClass("uno");
    });
});
#img4 {
    width: 7%;
    height: auto;
    margin: auto;
    display: block;
    background-size:20%;
    float: left;
}

#img5 {
    width: 3%;
    height: auto;
    margin: auto;
    display: block;
    background-size:20%;
    position: relative;
    right:20%;
}

.animar {
    width: 123px;
    height: auto;
    margin-right: 15%;
    display: block;
    background-size:0%;
    float:right;
    border-color: white;
    background-color: rgba(43,86,162, 1.00);
    font-family: Arial, "Helvetica Neue", Helvetica, sans-serif;
    font-size: 20px;
    color: white;
    border-radius: 12px;
    border-width: 10px;
}

uno {
    animation-name: uno;
    animation-duration: 10s;
} 

@keyframes uno {
    20% {left:80px;}
    50%{left:190px;}
    70%{left:220px}
    100%{left:350px;}
}

#gradiente2 {
     background: rgba(43,86,162, 1.00);
     position: relative;
     margin-bottom: 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="col-md-12" id="gradiente2">

    <p id="t1" class="typewrite" data-period="2000" data-type='[ "En nuestra institución siempre nos preocupamos por brindarte lo mejor"  ]'><span class="wrap"></span> </p>

    <p id="t2" class="typewrite" data-period="2000" data-type='[ "Síempre le Ponemos Corazón, a lo que hacemos" ]'><span class="wrap"></span> </p>

    <div>
        <img src="imagenes/kangura.png" class="img-responsive" id="img4">
        <img src="imagenes/corazon.png" class="img-responsive" id="img5">
        <button class="animar">Entregar Corazón</button>
    </div>

</div>

Answer №1

  • Don't forget to include a . before the class name uno in your CSS, like this: .uno.
  • Make sure to add position:relative to the #img4 element.

$(document).ready(function() {
  $(".animar").click(function() {
    $("#img4").addClass("uno");
  });
});
#img4 {
  position: relative;
  width: 7%;
  height: auto;
  margin: auto;
  display: block;
  background-size: 20%;
  float: left;
}

#img5 {
  width: 3%;
  height: auto;
  margin: auto;
  display: block;
  background-size: 20%;
  position: relative;
  right: 20%;
}

.animar {
  width: 123px;
  height: auto;
  margin-right: 15%;
  display: block;
  background-size: 0%;
  float: right;
  border-color: white;
  background-color: rgba(43, 86, 162, 1.00);
  font-family: Arial, "Helvetica Neue", Helvetica, sans-serif;
  font-size: 20px;
  color: white;
  border-radius: 12px;
  border-width: 10px;
}

.uno {
  animation-name: uno;
  animation-duration: 10s;
}

@keyframes uno {
  20% {
    left: 80px;
  }
  50% {
    left: 190px;
  }
  70% {
    left: 220px
  }
  100% {
    left: 350px;
  }
}

#gradiente2 {
  background: rgba(43, 86, 162, 1.00);
  position: relative;
  margin-bottom: 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />

<div class="col-md-12" id="gradiente2">

  <p id="t1" class="typewrite" data-period="2000" data-type='[ "En nuestra institución siempre nos preocupamos por brindarte lo mejor"  ]'><span class="wrap"></span> </p>

  <p id="t2" class="typewrite" data-period="2000" data-type='[ "Síempre le Ponemos Corazón, a lo que hacemos" ]'><span class="wrap"></span> </p>

  <div>
    <img src="https://1.img-dpreview.com/files/p/TS1200x900~sample_galleries/7214830437/4039259235.jpg" class="img-responsive" id="img4">

    <img src="https://2.img-dpreview.com/files/p/TS1200x900~sample_galleries/7214830437/8857557974.jpg" class="img-responsive" id="img5">
    <button class="animar">Entregar Corazón</button>
  </div>

</div>

Answer №2

Check out my inline notes for the modifications I've made:

  1. added #img4{position: relative;}
  2. changed uno{...} to .uno{...}

$(document).ready(function() {
    $(".animar").click(function() {
        $("#img4").addClass("uno");
    });
});
#img4 {
    width: 7%;
    height: auto;
    margin: auto;
    display: block;
    background-size:20%;
    float: left;
    position: relative;/* NEW */
}

#img5 {
    width: 3%;
    height: auto;
    margin: auto;
    display: block;
    background-size:20%;
    position: relative;
    right:20%;
}

.animar {
    width: 123px;
    height: auto;
    margin-right: 15%;
    display: block;
    background-size:0%;
    float:right;
    border-color: white;
    background-color: rgba(43,86,162, 1.00);
    font-family: Arial, "Helvetica Neue", Helvetica, sans-serif;
    font-size: 20px;
    color: white;
    border-radius: 12px;
    border-width: 10px;
}

.uno { /* modified "uno" to ".uno" */
    animation-name: uno;
    animation-duration: 10s;
} 

@keyframes uno {
    20%{left:80px;}
    50%{left:190px;}
    70%{left:220px}
    100%{left:350px;}
}

#gradiente2{
     background: rgba(43,86,162, 1.00);
     position: relative;
     margin-bottom: 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="col-md-12" id="gradiente2">

    <p id="t1" class="typewrite" data-period="2000" data-type='[ "In our institution, we always strive to provide you with the best."  ]'><span class="wrap"></span> </p>

    <p id="t2" class="typewrite" data-period="2000" data-type='[ "We Always Put Heart into What We Do" ]'><span class="wrap"></span> </p>

    <div>
        <img src="images/kangaroo.png" class="img-responsive" id="img4">
        <img src="images/heart.png" class="img-responsive" id="img5">
        <button class="animate">Deliver Heart</button>
    </div>

</div>

Feel free to drop a comment if further assistance is required...

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

Trouble displaying certain HTML code and its output on the browser?

I am in the process of developing a user profile page. I have designed a form with various fields, but the code within the section is not displaying correctly in the browser. I attempted to inspect the elements, but the code within that section remains inv ...

Tips for preventing multiple counter buttons from conflicting with one another

Currently, I am in the process of creating an online restaurant platform that allows customers to place food orders. To streamline this process, I am developing individual cards for each food item available on the menu. In addition, I am implementing butto ...

Steps to execute an external Python script in Django by clicking a dropdown button in the HTML

I am trying to set up a Django+Python frontend button that can trigger a Jenkins job. I have created a header with HTML containing a dropdown button. After researching various resources, it seems like AJAX is the way to go for triggering the Jenkins job. C ...

Combine React 17 with webpack 5 and babel-plugin-react-css-modules, enabling the use of CSS modules with stylename functionality

I am in the process of setting up a new React application using the latest technologies available, including React 17, Webpack 5, and other essential modules. My goal is to implement CSS modules utilizing the styleName concept with the help of babel-plugi ...

Declaring a function within a conditional statement

I recently came across a code sample in the book You Don't Know JS: Scope & Closures that is puzzling to me. "Function declarations that appear inside of normal blocks typically hoist to the enclosing scope, rather than being conditional as this ...

Angular.js has encountered an error due to exceeding the maximum call stack size

Hello everyone! I attempted to create recursion in order to extend my $routeProvider in Angular.js with the following code: var pages = { 'home': { 'url': '/', 'partialName': 'index', ...

Using headers in the fetch api results in a 405 Method Not Allowed error

I am facing an issue while attempting to make an ajax request using fetch. The response I receive is a 405 (Method Not Allowed) error. Here is how I am trying to execute it: fetch(url, { method: 'get', headers: { 'Game-Toke ...

Bringing in functions - NodeJS - non-HTML

Currently, I am in the process of learning automation for my job and have hit a roadblock. In times like these, stackoverflow has proven to be a life-saving resource :) My current task involves writing a Selenium test in VisualStudio using JavaScript (nod ...

The class "slick" in <col class="slick"> does not add any styling or effects

My interpretation of the col element is that it can be used to assign a class to all elements in a column of a table. However, I am experiencing difficulties with this approach. While I am able to apply the class to individual td elements, I am looking for ...

Unexpected token error occurs when making cross-domain AJAX requests to the server and receiving a JSON object

I have set up an express server to handle get requests to specific url endpoints. When responding to these requests, I am sending back data in JSON format to enable making Ajax calls and retrieving data from the page. To allow for cross-domain requests, I ...

Switching between Login Form and Register Form in VueJS template

Recently, I attempted to switch between the 'Login Form' and 'Register Form' using code that I found on codepen Flat HTML5/CSS3 Login Form. While the code functioned properly on its own, when integrated into a Vue Template, the form fai ...

Having issues with Facebook's login API for JavaScript?

Apologies for the improper formatting. I am encountering errors in my JavaScript compiler while working with the Facebook Login API... Error: Invalid App Id - Must be a number or numeric string representing the application id." all.js:53 "FB.getL ...

What is the most efficient method for creating and adding an element in jQuery?

When it comes to appending div elements to a page, there are different approaches that can be taken. Let's explore two methods: $('#page123').append("<div id='foo' class='checkbox' data-quesid='foofaa'>&l ...

Issue with Vue.js where the v-if directive placed inside a v-for loop does not properly update when

I want to dynamically show elements from an array based on boolean values. However, I am facing an issue where the element does not disappear even after the boolean value changes. <li v-for="(value, index) in list"> <span> {{ value[0] }} & ...

Every character entered in JSP should trigger an instant retrieval of the corresponding string in the servlet

Having a JSP file that contains a text field: <form action="someServlet" method=post> <input type ="text" name="user" id="uname"> <button type="submit" id="submit">Submit</button> </form> When typing each letter in the JSP, ...

PHP script for conducting sensitivity analysis

I am working with a PHP application that calculates a final result based on user input in multiple forms. Imagine a scenario where a user enters a value of 5 on the first page, which is passed through _POST to the next page. Then, they enter a value of 2 ...

Plupload is not compatible with ng-dialog

I'm currently attempting to integrate plupload into a modal window that is generated by ng-dialog. Here is the code I am using: $scope.showManager = function(){ ngDialog.open({ template: '/template/fmanager.html', controller: &apo ...

Basic HTML Audio Player Featuring Several Customizable Variables

I have a unique API that manages music playback. Instead of playing audio in the browser, it is done through a Discord bot. Achievement Goal https://i.stack.imgur.com/w3WUJ.png Parameters: current: indicates the current position of the track (e.g. 2:3 ...

Unable to upload gathered email to Mailchimp mailing list through Nodejs and express API

Seeking guidance on integrating data collection with Mailchimp in a Nodejs backend project. I am currently working on an email signup list page where users input their first name, last name, and email. The HTML file contains fields named firstName, lastN ...

Utilizing client-side properties in an onClick event within Next.js framework

class homeNotLoggedIn extends React.Component { componentDidMount() { function logout(){ localStorage.clear() location.reload() } } render() { return ( <div> < ...