The animated GIF background will play only one time

When I click on these images that resemble polaroids, I use jQuery's .replaceWith() to replace them with

<div class="poof"></div>
. The purpose of this is to display a poof animated gif animation through JavaScript.

However, every time I click on the .polaroid images, the .poof div replaces the HTML content but its background remains invisible.

You can try it out for yourself here:

Click on an image, and it will be replaced by the .poof div. However, the poof animation fails to appear.

I would greatly appreciate any assistance in figuring out why this happens and how to ensure the animation displays each time.

This is the JavaScript code I am using:

   $(function(){
        var time=1;
        $('.polaroid').click(function(){
            $(this).replaceWith('<div class="poof"></div>');
            $('.poof').css('height', $(this).height()).css('width', $(this).width()).css('background', 'transparent url(poof.gif?t='+time+') center no-repeat');
            time++;
        });
});

Below is the CSS for .poof:

.poof{
    height:32px;
    width:32px;
 }

Answer №1

You are retrieving the height and width of a removed or replaced object in the DOM

Try this instead:

var heights = 32;
var widths = 32;
$('.polaroid').click(function(){
    heights = $(this).height();
    widths = $(this).width();
    $('.poof').css('height', heights).css('width', widths).css('background', 'transparent url(http://i.imgur.com/FsVe2LC.gif?t='+time+') center no-repeat');
    time++;
    setTimeout(function(){
        $('.poof').remove();
    }, 1500);
});

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

Using CasperJS, learn how to effectively utilize the jQuery find() function

I'm looking to implement something similar to Cabybara within a function in CasperJS. My goal is to select parent divs and extract text from their child elements. Here's an example of what I want: $('div.education').find('h4&apos ...

Troubleshooting: Difficulty getting CSS `position: absolute` to correctly position a div above all other elements

In my current project, I am utilizing Javascript, specifically the VueJS framework, to create a page named mainTextEditor. This page consists of a text field and a 'popup' that is implemented using a div with a v-if directive to show the div when ...

Does a DOM API exist specifically for querying comment nodes within the document?

My document contains a debugging comment that appears as follows: <!--SERVER_TRACE {...}--> Is there a method to search the DOM and access this specific node? I would prefer a vanilla JavaScript solution, without relying on any external libraries. ...

Develop an onclick function with an href attribute

I am interested in transforming links into AJAX versions whenever possible. To achieve this, I plan to implement a function called replaceLinks that will add an onClick handler to each link on the page and trigger ajaxPageWSM(href). This is what I currentl ...

Utilizing CSS inheritance in React app to style multiple classes simultaneously

My app features two buttons that serve similar functions on different pages. One button directs users to the search page, while the other takes them back to the home page. The challenge I'm facing is that I need the background image for each button t ...

JavaScript checking the current page's URL is crucial for maintaining accurate and dynamic

I've attempted to verify the URL using this specific function. It functions properly with single text, but fails when a URL is inputted. jQuery(document).ready ( function () { //var regExp = /franky/g; //It works fine ...

I'm getting a JS error saying that the variable "var" is not defined. Does anyone know how I can

Here is the code I am using to dynamically create a sitemap.xml file when accessing /sitemap.xml database = firebase.database(); var ref = database.ref('urls'); ref.on('value', gotData, errData); function errData(err){ ...

What is the best way to dynamically create jest.mock(file)?

Attempting to simulate react-router-dom like this: jest.mock('react-router-dom', () => ({ ...jest.requireActual('react-router-dom'), useLocation: () => ({ pathname: '/random/path', }), })) While this ...

Combining two animated gifs using JavaScript: A step-by-step guide

In my current project, I have developed a web application that allows users to upload animated gifs. Through this application, users can place the gifs on the webpage using <img> tags and move them around as they please. After the user finishes arran ...

Dynamic image gallery with a flexible layout and interactive scrollbar using Flexbox

I'm looking to incorporate a scrolling image gallery into a flexbox layout so that as the browser page size changes, the gallery will scale according to the flex-grow index. My goal is to ensure all images remain accessible via a scrollbar without usi ...

Controlling date selection with Material UI's datepicker functionality

I have two date pickers, one for the start date and one for the end date. My goal is to have the end date picker disable all dates prior to the selected start date. For example, if you choose December 15th as the start date, then all dates before December ...

Editing HTML using the retrieved jQuery html() content

I need to modify some HTML that is stored in a variable. For example: var testhtml = $('.agenda-rename').html(); console.log($('input',testhtml).attr('name')); I also tried the following: console.log($(testhtml).find(' ...

States following form submission in an HTTP request may vary

When attempting to submit a form and receive the results on the same page using AJAX, a JavaScript function is called onsubmit, initiating the AJAX call and including the following code: request=new XMLHttpRequest(); /* set up request here ...

Using the `setTimeout` function to swap components

As I work on implementing a setTimeout method, the goal is to trigger an event after a .5 second delay when one of the three buttons (drip, french press, Aeropress) is pressed. This event will replace {{ShowText}} with {{ShowText2}}, which will display &ap ...

What's stopping me from being able to "map" this collection of mongooses?

I have been attempting to map a group of user documents within mongoDB. Here's the code snippet I've written for this purpose. User.retrieveUsers = function() { return new Promise(async (resolve, reject) => { try { let ...

Ajax and Mongodb make sure to utilize the unique identifier _id

Recently, I've been working on a test project using express.js where I am displaying all items from a mongo database on a webpage. Each entry in the database is shown on the page along with a delete button next to it. -each i in docs p ------ ...

HTML filtering using the <select> element

Is there a way to implement this script for options instead of buttons? The goal is to filter the HTML section, but it seems to not work with <option> tags. Instead of displaying the all class, it shows nothing. CSS .show { display: block; } .filt ...

Navigate through React components using React-Router to access class components

I'm encountering an issue when trying to route to a class component in React. Surprisingly, the routing works perfectly when using functional components. How can I successfully route to class components? As a beginner with react-router, I initially s ...

Modifying the value property of the parent element

Here is an example of the HTML code I am working with: <td value='3' style='text-align: center'> <select class='selection' onchange=''> <option value='1'>1</option> <opti ...

Experience the combined power of addthis, isotope, and nicescroll - all in a single

I am utilizing a WordPress template that includes a set of share buttons from AddThis. <ul class="addthis extra"> <li class="addthis-hold"> <div class="addthis_toolbox" addthis:url="<?php the_permalink( ...