The functionality of jQuery animation appears to fail upon the second attempt

Can you tell me why, in THIS CODE, the "I" button at the bottom right works perfectly the first time, but on the second click it only shows a pink background?

(function(){
  'use strict';

  var $mainButton = $(".main-button"),
      $closeButton = $(".close-button"),
      $buttonWrapper = $(".button-wrapper"),
      $ripple = $(".ripple"),
      $layer = $(".layered-content");

  $mainButton.on("click", function(){
    $ripple.addClass("rippling");
    $buttonWrapper.addClass("clicked").delay(1500).queue(function(){
      $layer.addClass("active");
    });
  });

  $closeButton.on("click", function(){
    $buttonWrapper.removeClass("clicked");
    $ripple.removeClass("rippling");
    $layer.removeClass("active");
  });

})();
@import url(http://fonts.googleapis.com/css?family=Roboto:400,300);

html {
  height: 100%;
}

body {
  background: url("http://species-in-pieces.com/img/bg/grad-bg.png") no-repeat center center/cover #F9D8AD;
  height: 100%;
}
....
.... (additional code omitted for brevity)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<main id="container">
  <h2>Material Overlay Animation</h2>
  <div class="img-wrapper">
    <img src="https://d13yacurqjgara.cloudfront.net/users/2733/screenshots/741958/dribbble-foxes.jpg" alt="Just Background">
  </div>
  <p class="detail">Click the Button to see the "Material Animation". Works great on modern browser.</p>

  <div class="button-wrapper">
    <div class="layer"></div>
    <button class="main-button fa fa-info">
      <div class="ripple"></div>
    </button>
  </div>

  <div class="layered-content">
    <button class="close-button fa fa-times"></button>
    <div class="content">
      <img src="https://d13yacurqjgara.cloudfront.net/users/332135/avatars/normal/52d614ee44e3e21d2b73894c465773d7.png" alt="Balapa">
      <p>Crafted by balapa.</p>
      <p>See also my <a href="http://codepen.io/balapa/details/gbQbXR/">other pen</a></p>
    </div>
  </div>
</main>
<div class="copyright"><span>Material Overlay Animation by</span><a href="http://dribbble.com/balapa" class="balapa"></a></div>

Answer №1

To clear the queue, use the clearQueue() function.

Update your code as shown below:

...
    $buttonWrapper.addClass("clicked").clearQueue().delay(1500).queue(function(){
            $layer.addClass("active");
        });
    });
...

Alternatively, you can follow this approach recommended in the jQuery documentation:

$buttonWrapper.addClass("clicked").delay(1500).queue(function(){
            $layer.addClass("active");
            jQuery.dequeue( this );
        });
    });

A crucial point directly from the jQuery documentation:

It is essential to ensure that when adding a function with jQuery.queue(), jQuery.dequeue() is eventually called for the next function in line to execute.

Answer №2

Here's an alternative approach:

$buttonWrapper.addClass("clicked");
setTimeout(function() {
   $layer.addClass("active");
}, 1500);

By making this change, the code will function as intended.

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

How to vertically align multiple lines of text using CSS

Struggling with vertical alignment of text in a tabbed layout on my website. Some tabs have single-line text while others span two lines, making it challenging to position the text consistently in the middle of each tab without aligning by the first line. ...

Modernize legacy Angular codebase to the most recent version (v15)

I'm trying to find the smoothest path to upgrading an angular application from v7 to v15. It's a big codebase with numerous deprecated packages and implementations. Is there a simpler approach to tackling this task? I appreciate any advice you ca ...

Can anyone confirm if this is the Facebook Support JSONP callback function?

https://www.facebook.com/feeds/page.php?id=397319800348866&format=jsoncallback=? I'm trying to retrieve post content via this link. I attempted to use the getJSON method, but it didn't work. Someone suggested using the jsonp format on Faceb ...

What is the best way to extract a specific portion of an object array into a new array?

I am working with an Object array containing product information: let products = [{ code: 'a', num: 1 }, { code: 'b', num: 2 }]; My goal is to extract the codes from this array: ['a', 'b']. Currently, I'm ach ...

The issue with single quotes causing problems in JSON when generated from PHP

IMPORTANT: I initially had a different question in mind, but I've now pinpointed where the actual issue lies. The current problem I'm facing involves generating JSON output from PHP for jQuery use across domains using "JSONP." The culprit seems ...

What steps can be taken to ensure a dropdown selection is required when a specific variable is true?

How can I create an AngularJS dropdown that requires a selection only when a specific variable, such as 'x', is set to true? If 'x' is false, it should allow saving without a selection. Below is the code for my dropdown: <select cla ...

Contrasting $(document).ready and directly writing jQuery statements at the beginning of a script

What distinguishes coding with and without $(document).ready? Consider the following: $(document).ready(function() { $("#button").click(function() { //Code }); }); Versus: $("#button").click(function() { //Code }); Also : <inp ...

Having trouble updating textures when clicking a button in Aframe

I'm attempting to load a gltf model and change the texture on button click using the register component. The model loads and displays as expected, but I'm having trouble reflecting any changes when the button is clicked for the texture change. H ...

Troubleshooting focus problems with ng-if

Currently experiencing a focus issue while using Angular 1.6. Any suggestions on how to resolve this would be greatly appreciated. HTML <button type="button" data-ng-click="showPan()"> Show</button> <div data-ng-if="showPanDiv"& ...

designing an uncomplicated button that incorporates the column size feature of Bootstrap

My concept involves crafting a minimalist green button with no text or border, just a sleek green shape that would automatically match the size of col-md-12 from Bootstrap. While I've come across similar inquiries, my limited understanding is hinderin ...

Employing chained async/await function executions

I am currently troubleshooting a straightforward code. The problem lies within an async function I've created: async function updateResult(event){ let result = db.fetchResult(event.ProcessId); return result; } This function is being called from ...

"Step-by-Step Guide on Displaying Icons from One Folder to Another Page via a Link

I have a query regarding displaying icons (images). The icons are stored in the "img/icons" path. However, when I try to display them on a different page with the link "xxx/xxx/xxx/xxx", the icons do not show up. They only appear with the link "xxx/xxx" be ...

conceal a targeted section from the bottom of the iframe

I have a website embedded inside an iframe for use in a webview application. <body> <iframe id="iframe" src="http://www.medicamall.com"></iframe> </body> This is the CSS code: body { margin:0; padding:0; height:10 ...

New ways to style Links in NextJs 13

I am relatively new to Next.js and I am attempting to create a menu with a hover effect using the following code: import Link from 'next/link'; const MenuItem = ({ href, label }) => ( <Link href={href} className="menu-item"> ...

Express-hbs: Dynamic Helper Function with Additional Features

I am currently utilizing express-hbs and Async Helpers in my project. However, I am facing an issue with passing options to the helper as async helpers do not seem to support this feature (or maybe I am unaware of how to do it correctly). In the code snipp ...

Convert this text into HTML code: "Textarea to

I have a basic <textarea> element that I want to transform links (www.google.com, msn.com, etc) and line breaks (\r\n) into HTML code. I found one library for converting links into <a hrefs>. Another library can convert line breaks i ...

The post controller functions properly when an image file is present, but encounters issues when trying to create a

I'm struggling with my controller when trying to create a post. The issue seems to be related to the condition "if (req.file !== null)" which still executes even if no file is being uploaded (specifically in the picture field, as it should represent ...

How can we create a two-dimensional image with random dimensions using Math.random?

I am struggling to create a variety of flowers with different sizes from an Array. The issue I'm facing is that every time I click to add a new flower, it changes the size of all existing flowers as well. What I would like is for each added flower to ...

Exploring Ways to Navigate to a Component Two Steps Back in Angular

Let's say I have three routes A->B->C. I travel from A to B and then from B to C. Now, is it possible for me to go directly from C to A? ...

By incorporating JavaScript, clicking on an image will trigger a shift in the position of a different element

Just starting out with JavaScript and experimenting with creating an event where clicking on an image triggers the movement of another hidden image. The hidden image is positioned off-screen to the left, while there is a table of images in the center of th ...