Is it possible to create a list item animation using only one div element?

I'm currently working on achieving a dynamic animation effect for list items similar to the one demonstrated in Pasquale D'Silva's design example: https://medium.com/design-ux/926eb80d64e3#1f47

In this animation, the list item disappears while the space it occupied maintains its height momentarily before collapsing to zero height.

To accomplish this effect, I have utilized a div with a transparent background containing another div that encapsulates the actual content.

The approach involves animating the inner div, introducing a pause, and then reducing the height of the outer div to zero.

You can view my implementation on codepen: http://codepen.io/michaellee/pen/Cnpcf (Click on an item to see it disappear.)

I am curious if there is a way to achieve the same effect without nesting divs within each other?

HTML

<div class="stackOne">
    <div class="item-holder">
        <div class="item"></div>
    </div>
    <div class="item-holder">
        <div class="item"></div>
    </div>
    <div class="item-holder">
        <div class="item"></div>
    </div>
    <div class="item-holder">
        <div class="item"></div>
    </div>
    <div class="item-holder">
        <div class="item"></div>
    </div>
</div>

CSS

.stackOne{
  display: inline-block;
  vertical-align: top;
  width: 200px;
  overflow: hidden;
  .item-holder{
    height: 50px;
    margin: 0 0 1px 0;
    .item{
      width: 200px;
      height: 50px;
      background: #ccc;
      position: relative;
    }
  }
}

JS

$('.stackOne .item').click(function(){
  var item = $(this);
  item.animate({
    left: "100%"
  }, 250, "swing", function() {
    item.parent().delay(100).animate({
      height: 0
    }, 50, "linear", function(){
      item.parent().hide();
    });
  });
});

Answer №1

To implement smoother animations, you can eliminate the container and directly apply your desired effects to the child element - here is an updated JavaScript snippet:

$('.stackOne .item').click(function(){
  var item = $(this);
  item.animate({
    left: "100%"
  }, 250, "swing", function() {
    item.delay(100).animate({
      height: 0
    }, 150, "linear", function(){
      item.hide();
    });
  });
});

Check out the revised CodePen example

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

Problem encountered when utilizing the jQuery method .load()

Currently, there is an issue on my website where I am trying to load the content of a PHP file into a <div>. After ruling out any server-side problems, I have come to seek help with this question: Is there anything incorrect in the following code? & ...

Creating multiple nested ng-repeats in an AngularJS table

Managing a large amount of data on users' availability by hour for multiple days can be challenging. The data is structured as follows: availData = { "date1":arrayOfUsers, "date2":arrayOfUsers, ... } arrayOfUsers= [ userArray, userArray, ... ] user ...

What causes an "Internal Server Error" when attempting to use data for a database request with AJAX GET/POST in Laravel?

There's a unique issue that I'm struggling to resolve... Every time I drag and drop an event into the calendar, an Ajax Post Request is sent to my controller. The controller then inserts some data into the database with the event name received v ...

Position the division at the location of the cursor for particular usemap components

Looking for a way to position a division on top of an image using a usemap. Interested in having the div only appear when the mouse hovers over a particular area, and at the precise location of the cursor. Came across some examples using jQuery, similar ...

jQuery element with listener not triggering as expected

I'm facing some confusion while working on this issue. I am attempting to create a dynamic form where users can add descriptions, checkboxes, and number inputs as they please. Currently, I have developed a simple dynamic form using jQuery, which allow ...

Tips on aligning text to the left on mobile devices using CSS and HTML

When viewed on a standard 16:9 computer screen, everything looks perfect with text on the left and an image on the right. However, when it comes to smaller screens like phones, the picture appears on top of the paragraph instead. I am new to this, so any a ...

Error in browser caused by JQuery JavaScript, not Dreamweaver

I need help creating a webpage that can extract and display recent earthquake data based on user input location on Google Maps. I have been using Dreamweaver to test my code, and everything functions perfectly when I use the live webpage feature within th ...

How to make Firefox create a new line in a contenteditable field

I am working with a contenteditable div that is within a parent div with a floating width of 50%. My goal is to set the max width of the content editable to match the container, even if the text spans multiple lines. Here is the CSS I tried: .editable-con ...

How to upload a file in ReactJS with the help of jQuery

I am facing a challenge while trying to upload a file to one of my localhost folders using reactjs and jquery. Upon submitting the file, no errors are reported, however, upon checking the folder, I find that it is empty - nothing has been uploaded. upload ...

Listening for events on jQuery forms

Is it feasible to create an event listener for a form that prevents submission if a specific action, such as selecting an element from a list, is not taken, and then triggers an alert message? Appreciate your help! ...

Steps for generating an observable that mirrors the structure of an observable array

I am in the process of creating an observable array using JSON data retrieved from the server. var ViewModel = function (data) { var self = this; self.list = ko.observableArray(data); self.selected = ko.observable(); } ...

Seamless animation when collapsing element using angular and css exclusively

I am attempting to incorporate a collapsible feature into my application. Rather than relying on external libraries like jQuery, I want to exclusively utilize Angular. For demonstration purposes, I have created a very basic example here: http://jsfiddle.n ...

Changing $scope does not automatically refresh the selected option when using ng-options with <select> in AngularJS

My select element is structured like this: <select ng-model="exportParam" ng-options="item as item.lib for item in allExportParams | orderBy:'lib'" class="form-control"> </select> To save its state for later display, I sto ...

What is the best way to implement a jQuery post for a file upload form?

My form has the following structure: <form id="uploadForm" name="uploadForm" method="post" enctype="multipart/form-data"> <input type="file" id="uploadFile" name="uploadFile" /><br /> <input type="submit" id="process" name="pr ...

The Quirks of jQuery's .load() Method

On my website, I am incorporating a basic jQuery script to load content from one part of a webpage into the 'display container' on the same page. The content being loaded consists of multiple divs enclosed within an outer <div> that is hid ...

Incorporate personalized feedback into a datatable with server-side processing

Trying to implement server-side processing for a datatable loaded from an AJAX response using this specific example The server response being received is as follows: {"msg":null,"code":null,"status":null,"result":[{"aNumber":"3224193861","bNumber":"32159 ...

.NET and Azure integration for asynchronous HTTP requests

My Ajax function is running smoothly on my local machine when the site is accessed locally. However, an issue arises when deploying it to Azure - if one of the parameters contains a line break, I get an error message: "The specified URL cannot be found", i ...

Make the background image come to life by animating it from the bottom to the top in

Upon landing on a page, I aim for the background image to initially focus on the bottom and then pan up to the top before returning back down in a continuous loop. The code I have set up is as follows: <body> <div id="container"></div&g ...

What is the best way to interpret a nested JSON object?

Recently I've crafted an object that looks like this. myObj = { "name":"John", "age":30, "cars": [ "car1":"Ford", "car2":"BMW", "car3":"Fiat" ] } While it's pretty straightforward to read the name and age properties, I find ...

Is the item positioned above another item instead of being positioned to the left of it?

Looking for help with my mobile navigation setup. I want to add text that says ' ...