Perform a CSS transition following a jQuery fadeOut effect

Check out the red and green boxes on this JSFiddle. Clicking the 'click me' button makes the red boxes fade out using jQuery's fadeOut method, while the green boxes jump to their new position.

I'm looking for a way to make the green boxes move smoothly to their new spot. I've tried adding CSS transitions like this:

-webkit-transition: all 1s ease-in-out;
  -moz-transition: all 1s ease-in-out;
  -o-transition: all 1s ease-in-out;
  transition: all 1s ease-in-out;

However, this causes the jQuery fadeOut function to stop working.


Take a look at the code in the JSFiddle:

HTML

<div id="clickme">Click me</div>
<ul>
    <li><div class='a'></div></li>
    <li><div class='a'></div></li>
    <li><div class='a'></div></li>
    <li><div class='b'></div></li>
    <li><div class='a'></div></li>
    <li><div class='a'></div></li>
    <li><div class='b'></div></li>
    <li><div class='a'></div></li>
    <li><div class='a'></div></li>
    <li><div class='a'></div></li>
    <li><div class='b'></div></li>
    <li><div class='a'></div></li>
    <li><div class='b'></div></li>
</ul>

Jquery

$(document).ready(function(){
    $("#clickme").click(function(){
    $('.a').fadeOut(1000);
                });
   });

CSS

  li{
        list-style-type: none;

        float: left;
    }

    li div{
        margin: 20px;
        height: 100px;
        width: 100px;
    }

    li div.a{
        background-color: red;
    }

    li div.b{
        background-color: green;
    }

    #clickme{
      background-color: blue;
      text-align: center;
      color: #FFF;
      cursor: pointer;
     }
    }

Answer №1

If you want to apply a transition effect to red elements after they fade out, you can do so by adding a specific class.

  $("#clickme").click(function(){
    $('.a').fadeOut(1000, function() {
        $('.a').addClass('hide');
    });
  });

To set the transition for these elements, use the following CSS:

li div.a.hide{
      -webkit-transition: all 1s ease-in-out;
      -moz-transition: all 1s ease-in-out;
      -o-transition: all 1s ease-in-out;
      transition: all 1s ease-in-out;

      opacity: 0;
      display: block !important;
      margin-top: 0;
      margin-bottom: 0;
      height: 0;

    }

If you encounter issues with floating, consider breaking the list into 4 li elements and placing your boxes inside them. This adjustment should resolve any animation problems.

Here is the updated layout using 4 li elements:

    <ul>
      <li>
        <div class='a'></div>
        <div class='a'></div>
        <div class='b'></div>
      </li>
      <li>
        <div class='a'></div>
        <div class='b'></div>
        <div class='a'></div>
      </li>
      <li>
        <div class='b'></div>
        <div class='a'></div>
        <div class='a'></div>
      </li>
      <li>
        <div class='a'></div>
        <div class='a'></div>
        <div class='a'></div>
      </li>
    </ul>

You can view the updated fiddle here: https://jsfiddle.net/htaexnn6/

Answer №2

If you want to add a delay after using jQuery to fade out an element with the .a class and then fade in an element with the .b class, you can achieve it by setting a delay that is longer than the duration of the fadeOut effect:

    $("#clickme").click(function(){
        $('.a').fadeOut();
        $('.b').delay(1500).fadeIn();
    });

It's important to ensure that the fadeOut() effect completes before calling the next transition, such as the fadeIn() effect on the .b class.

Check out how this code works in action: https://jsfiddle.net/examplelinktocodedemo/1/

Answer №3

Using JQuery to Toggle CSS Classes

$(document).ready(function(){
  $("#clickme").click(function(){
    $('div').toggleClass('a');
  });
});

CSS Styling for Classes 'a' and 'b'

li {
  list-style-type: none;
  float: left;
}

li div {
  margin: 20px 0;
  height: 100px;
  width: 0px;
  opacity: 0;
  background-color: red;
  -webkit-transition: all 1s ease-in-out;
  -moz-transition: all 1s ease-in-out;
  -o-transition: all 1s ease-in-out;
  transition: all 1s ease-in-out;
}

li div.a {
  opacity: 1.0;
  margin: 20px;
  width: 100px;
}

li div.b {
  opacity: 1.0;
  margin: 20px;
  width: 100px;
  background-color: green;
}

#clickme {
  background-color: blue;
  text-align: center;
  color: #FFF;
  cursor: pointer;
}

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

The sidebar is not correctly displaying at full height

Having trouble getting the sidebar to fit perfectly between the header/nav and footer in full height. <html> <head> <meta charset="utf-8"> </head> <body> <header> <div class="header"> & ...

I encountered a problem when making multiple JSON calls within a for loop

for(var j = 0; j < tempArray.length; j ++){ $.getJSON('http://localhost:8080/predict', tempArray[j]) .fail(function(data,textStatus, error) { Probability = data.responseText; console.error("getJSON failed, status: " + textStat ...

Different ways to generate DOM element using jQuery

When it comes to creating a new DOM element from JavaScript or jQuery code, there are numerous methods to consider. Some examples include: $('<div>').prop('id','someid').addClass('someclass'); $('<div& ...

Unable to apply nativeElement.style.width property within ngOnChanges lifecycle method

My current task involves adjusting the width of containers while also monitoring changes in my @Input since the DOM structure is dependent on it. @Input('connections') public connections = []; @ViewChildren('containers', { read: Elemen ...

Challenges with nesting radio button groups in jQuery validation

Trying to implement a custom validation method for a group of radio buttons in v1.9 of the jQuery validate plugin. Despite this article, it is indeed feasible to have radio buttons with the same name attribute, as long as different class rules are applied ...

Centering an image vertically in a fluid navigation bar

I'm currently working on creating a fluid navigation bar in Dreamweaver that adjusts the size of the logo when changing the viewport. Everything looks great, except for one issue - the logo image doesn't stay vertically centered when it gets smal ...

Set a dynamic Active Class on various divisions by utilizing their respective indexes

I have two divs with the same class. When I click on an anchor tag within one of the elements, I want to add a class to the corresponding position in the second div as well. Mirror: JSFiddle Here is the jQuery code snippet: $(document).on('click ...

Ways to eliminate a draggable div using a droppable div

I have a table with div elements that have different classes - drag and drop. The drag element is capable of being dragged and dropped into a drop element. My issue is that I need to change the origin position of the drag element when it is dropped into ...

Using jQuery Ajax GET in Rails

Why is my ajax code only outputting data to the console and not making any requests in the server log? I'm confused because my other click handlers are functioning properly. Any insights would be appreciated. # routes.rb get 'organize/process ...

What steps can be taken to handle and proceed with any outstanding AJAX requests in the event a

I am currently working on a script to automatically refresh all CSS/JS files that are marked with the attribute-data when any changes occur on the server side. Initially, I attempted to achieve this using php and jquery/javascript but now I am focusing sol ...

Creating visually appealing Highcharts.js visualizations for both mobile and desktop devices

Has anyone had success implementing a responsive design with Highcharts to ensure charts look great on both mobile and desktop screens? By default, Highcharts do adjust when resizing the browser window, but sometimes the X-axis can become cluttered by tic ...

Optimize Page Speed by Delaying the Loading of Slideshow Images

My main issue with reducing my pagespeed is the slideshow I have implemented. I currently have 17 rather large images in the slideshow, but for simplicity, I will only show the code for 3 images below. I am trying to find a way to load the first image as a ...

Switch Button JavaScript Library

My project involves creating a mobile website with a simple interaction where clicking on a div opens another div underneath it. The HTML structure consists of two stacked divs, with the CSS for the bottom div initially set to 'none'. Using JavaS ...

Does anyone else have a peculiar problem with css width?

Either I have been designing web pages for an extensive period of time without taking a break or something truly bizarre occurred. <div style="background-color:#0F0; margin:5px; height:5px;"></div> This will generate a lengthy bar with a 5-pi ...

Including file format extension in the absence of one

I have created a script that loops through every image source on the page, extracting the extension. If the extension is not recognized or does not exist, I automatically add .jpg to the end. Strangely, the if statement always evaluates to true no matter ...

What are some ways to personalize the depth graph in amcharts?

I am having trouble modifying the depth graph amchart and I'm struggling to grasp how it functions and how to design it like the image below. Below is the link to the original graph that I am trying to customize: Link https://i.stack.imgur.com/nbWd ...

Delivering a collection of data

Can anyone help me figure out how to send two arrays to my PHP script using jQuery and JSON? This is the code I have so far: $.ajax( { url: "script.php", type: "POST", data: ???, dataType: "html", success: function(tablehtml){ alert(tablehtml); } }); I ...

Instructions on displaying the filename as the input for width and height dimensions

Running into an issue with Bootstrap 4 - whenever I try to upload a file with a long name, it ends up overflowing beyond the input field. ...

Maintain the div height as the image loads

Is there a way to prevent the collapse of the .img-container while an image is loading? Currently, when the image takes a few seconds to load, the container collapses and only expands to full height once the image has loaded. I would like the container to ...

The project is experiencing difficulties in loading the Module CSS

Currently, I am working on a React module and have set up a React project to demonstrate the functionality of this module. Initially, everything was working smoothly until I encountered an issue with the webpack configuration related to the CSS loader. { ...