Implementing a queue with an on-click event

As a self-proclaimed Java nerd diving into the world of jQuery, I'm facing some challenges.

My goal is to create 3 interactive boxes that behave in a specific way: when clicked, one box should come forward while the other two dim and stay in the background. However, I want to implement a queue system so that if multiple boxes are clicked consecutively, they only move forward once the previous animation ends. Right now, everything seems to be happening simultaneously instead of in order.

I've tried using callbacks and deferred methods without success.

Below is the current Javascript code:

var zindex = 1;

$('.box_listener').click(function() {

  $(this).css('z-index', zindex += 1);
  $(this).siblings('.box_listener').fadeTo(3000, 0.5);
  $(this).fadeTo(1, 1);
});

Here is the JSFiddle link for reference:

https://jsfiddle.net/asger/5yvvgoda/14/

Cheers, and any help would be greatly appreciated!

Answer №1

For a more robust solution, consider avoiding jQuery animations and utilizing CSS transitions instead. This approach offers two main advantages: CSS transitions can be easily reversed and they can benefit from GPU acceleration. Additionally, it eliminates the need to artificially delay user input until the animation completes.

To implement this method, create two CSS classes: one defining the transition properties for the elements to be animated, and the other altering the values on the element to trigger the transition. Then, simply use jQuery's addClass() and removeClass() functions to initiate the transitions.

See below for a practical example with key points highlighted in comments.

$('.btn').on('click', function() {
  // Remove 'active' class from all buttons to reverse the transition
  $('.btn').removeClass('active');
  // Apply 'active' class only to the clicked button to start the transition
  $(this).addClass('active');
});
.btn {
  display: block;
  width: 200px;
  padding: 10px 20px;
  margin-bottom: 5px;

  background: cornflowerblue;
  border: 0;
  cursor: pointer;
  
  /* Set up a transition for CSS transformations like translation, scaling, rotation, etc. */
  transition: transform 300ms ease-in-out;
}

/* Adding this class will scale the button while the existing transition ensures smooth scaling */
.active {
  transform: scale(1.2);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h2>Click the buttons</h2>

<button class="btn">First</button>
<button class="btn">Second</button>
<button class="btn">Third</button>

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 need of assistance with posting data on a Captive Portal using Ruckus AccessPoint Javascript

We are currently working on implementing a captive portal using Ruckus AP for our guests. Our setup includes a form where users can input their username and password. Upon clicking "Login", we aim to post this data to the Ruckus AP's user_login_auth. ...

Strange Node.js Issue

I don't have much experience with node.js, but I had to use it for launching on Heroku. Everything was going smoothly until a few days ago when suddenly these errors started appearing. Error: /app/index.jade:9 7| meta(name='viewport', co ...

Learning the basics of JavaScript: Displaying the last number in an array and restarting a function

Hey there, I'm diving into the world of JavaScript and have set myself a challenge to create a simple bingo number machine. Check out my CodePen project here. I've successfully generated an array of 20 numbers, shuffled them, and added a marker t ...

What is the best way to retrieve the title element from a loaded iframe?

Looking for a way to create a dynamic title for my iframe without manually coding it in. My goal is to extract the src of the iframe, load that object, and then retrieve the title element from it. I attempted using iframe.contentDocument but encountered ...

Three.js - Controlling Visibility of Text in troika-three-text with Clipping Planes

Has anyone successfully clipped troika-three-text for threejs using clipping planes? I'm having trouble getting it to work. import { Text } from 'troika-three-text' const minClippingPlane = new THREE.Plane(new THREE.Vector3(0, -1, 0), 1) c ...

Problems with Material UI autocomplete causing destruction of Redux-form values

I'm facing an issue with integrating Material UI's autocomplete component into a Redux wizard form. Although I successfully linked the autocomplete feature, I encountered a problem when navigating back to the second page where the autocomplete f ...

What is the best way to target an HTML element that is only connected to a particular class?

My HTML code includes the following 3 elements: <span class="a b"></span> <span class="a"></span> <span class="a b"></span> I am trying to select the element that only has the class "a". When I use $("span.a"), it sele ...

Suggestions to reduce our website loading time

Query: How can one effectively reduce the file size of a webpage to improve loading speed? What specific optimization practices and coding techniques (in JavaScript and PHP) can be implemented to decrease page weight? Motivation: After reading an article ...

Create a dynamic design with Angular Material's mat-drawer at full height using flexbox, allowing content

Yesterday, I encountered an interesting CSS problem involving the mat-drawer and Angular's router-outlet. My layout consists of a full-page flexbox with two children: a mat-toolbar at the top, and a custom component called app-sidenav at the bottom. T ...

Creating a unique styleset in styled-jsx using custom ruleset generation

TL;DR What's the best way to insert a variable containing CSS rules into styled-jsx (using styled-jsx-plugin-sass)? In my JSX style, I have the following: // src/pages/index.tsx ... <style jsx> {` .test { height: 100vh; width ...

The website is displaying a strange mix of text and HTML when viewed on a PC

While browsing the internet for C programs, I stumbled upon this particular program that caught my eye: <span style='color:#004a43'>#</span><span style='color:#004a43'>include</span><span style='color:#8 ...

The chosen value remains constant even after altering the selected option

I am trying to create a scroll-down bar that allows users to select different options, and once an option is selected, its value should appear in the bar. However, I'm facing an issue where the value does not change according to the selection: const [ ...

Is it possible to transfer data from a JavaScript file to the Express server without relying on a form submission?

As a newcomer here, I apologize if I overlook any best practices. Currently, I am in the process of developing a sudoku game using express, JavaScript, HTML, and mongoDb. My current challenge involves setting up a statistics system. My goal is to send da ...

using css to pass arguments

I need help with a code that I have, here's the structure: <div className="site-col-1"> content 1 </div> <div className="site-col-2"> content 2 </div> Here is how the corresponding CSS file looks... .s ...

Display a loading image as a placeholder while the Block is loading on the Viewport

Despite my extensive search for a solution to my problem, I have been unable to find one that addresses it directly. I am trying to display a "loading" image for 4 seconds before the content of the div is loaded. Unfortunately, I haven't been able to ...

Tips for setting up Craigslist email forwarding through Node.js (receiving emails to a dynamically generated email address and redirecting them)

After extensive searching, I only came across this Stack Overflow post about Email Forwarding like Craigslist in Rails: Email Forwarding like Craigslist - Rails I spent a significant amount of time googling, but couldn't find any solutions using Node ...

Is there a notification system that sparkles like a precious Facebook gem

Is there a system similar to Facebook's request, message, and notification bubble system that can alert users on their profiles about new messages or notifications? I know Facebook is very advanced, but if anyone knows of something similar, please let ...

Embark on a journey through a preorder traversal of a Binary Tree using TypeScript

Hello! I've been tasked with creating a function that iterates over a binary tree and returns all its values in pre-order. Here is the code snippet: interface BinTree { root: number; left?: BinTree; right?: BinTree; }; const TreePreArray ...

Can the state be determined by the presence of other objects in the state?

Currently, I'm delving into the world of ReactJS by constructing a personalized launch page. Brace yourself for a somewhat casual explanation. Within my primary app.js file, I've set up the initial state of the application with some data like th ...

What is the best way to restrict a link to only a specific div element and not a Bootstrap

My HTML conundrum involves a div styled with the class .win_box: This particular div is nested within a Bootstrap column, and the link associated with it extends throughout the entirety of said column. However, I want to restrict user access to this link ...