Animating UL/LI elements using the power of CSS3 transformations

I am interested in creating animations using CSS3 transitions. Let's consider the following UL/LI element:

<ul>
    <li class="green" id="green" style="display: none;">Contents 1</li>
    <li class="red" id="red" style="display: none;">Contents 2</li>
    <li class="yellow" id="yellow" style="display: none;">Contents 3</li>
</ul>

It is important to note that these elements are horizontally positioned next to each other (display: inline-block).

Upon clicking a button, I can show these elements without any issue. Here is the HTML code snippet for achieving this:

<a href="#" onclick="$('#green').show();">Make contents 1 visible</a>
<a href="#" onclick="$('#red').show();">Make contents 2 visible</a><br/>
<a href="#" onclick="$('#yellow').show();">Make contents 3 visible</a>

To add animation effects, I can simply apply a specific class to the element with the desired CSS properties like so:

.animate { transition: all linear 5s; opacity: 1; display: inline-block; }

Now, let's change the LI elements to absolute positioning so they all appear at the same location.

The intended animation effect I aim to achieve is as follows:

  • Enabling item 2 will cause it to fade in smoothly.
  • Subsequently enabling item 1 should trigger a fading in motion along with item 2 moving to the right until item 1 occupies its necessary space.

Moreover, both items do not have fixed widths as their content dynamically changes.

You can refer to this fiddle for a better grasp of the concept:

http://jsfiddle.net/dw4Lz8qe/

In summary, I intend to implement an animation where a new item fades in while already visible items move to make room for it accordingly.

I trust that my explanation was clear and concise.

Best regards,

Answer №1

One possible solution to your issue is to use an animation on the font-size:

ul li {
  display: inline-block; 
  opacity: 0;
  font-size: 0px;
  transition: all linear 0.5s;
}

.green {background-color: green;}
.red {background-color: red;}
.yellow {background-color: yellow;}

.animate {
  opacity: 1;
  font-size: 16px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button onclick="$('#green').toggleClass('animate') ">Toggle contents 1</button>
<button onclick="$('#red').toggleClass('animate')   ">Toggle contents 2</button>
<button onclick="$('#yellow').toggleClass('animate')">Toggle contents 3</button>

<ul class="padding: 0px; margin: 0px;">
  <li class="green" id="green">Contents 1</li>
  <li class="red" id="red">Contents 2</li>
  <li class="yellow" id="yellow">Contents 3</li>
</ul>

Answer №2

With CSS, your <li> element can have a fixed width to make it work effectively. Take a look at the updated fiddle for more insights on this topic. However, transitioning between width: 0 and width: auto is not supported by any browser that I am aware of.

Explore the demo here

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

JavaScript/jQuery: What is the best way to assign an element as the context ('this') of a function?

Is there a way to pass something to a function and have it act as if it's calling the function itself? Consider this function: function ShowId() { alert($(this).attr('id')); } and this block of HTML: <div id='div1'> & ...

Adjust Vue FilePond dimensions

I am currently working with a Vue Filepond and trying to ensure that it fills the height of its container. My Vue Filepond setup also involves Vuetify. Whenever I attempt to set values in the CSS, they are constantly overridden by 76px. Although fill-hei ...

Renderer's Delayed Electron Loading

I am in the process of developing a Electron application using TypeScript and React. This application serves as an account manager, allowing users to retrieve and view data for specific accounts. However, I have encountered an issue with the ipcMain.on(&a ...

"Setting an index to a button's ID using Jquery: A step-by-step guide

My goal is to assign incrementing index values to button IDs in a loop. For example, if the first button has an ID of 'unique', the second button should have an ID of 'unique0', and the third button should have an ID of 'unique1&ap ...

When anchor is set programmatically, the focus outline is not displayed

I am currently facing an issue with my application where I am programmatically setting focus on elements in certain scenarios. While it generally works well, I have noticed that when I set the focus on an anchor element using $("#link1").focus(), the focus ...

JavaScript Error: Unable to determine the value of a null property

Is there a way to validate the user's input in real-time on an HTML form? Take a look at the following HTML code: <div class="col-md-2"> <input class="form-control" name="nbequipement" id="nbequipement" placeholder="" type="text"> ...

What is the best way to add up the attributes of objects within an array and save the total to the main

I have a collection of objects, illustrated here: var obj = { "ABC" : { "name" : "ABC", "budget" : 0, "expense" : 0, "ledgers" : [{ "Actual1920": 10, "Budget1920": 20, }, { "Actual1920": 10, ...

I am looking to both enlarge and shrink a table row

I found this code snippet on a forum, and it almost does what I need. The JQuery code provided collapses the table rows by default and expands them when the 'heading id' is clicked. However, it lacks the functionality to collapse the rows again w ...

Using Node.js and TypeScript to define custom data types has become a common practice among developers

I offer a variety of services, all yielding the same outcome: type result = { success: boolean data?: any } const serviceA = async (): Promise<result> => { ... } const serviceB = async (): Promise<result> => { ... } However, th ...

What steps are involved in developing a quiz similar to this one?

Check out this interesting quiz: I noticed that in this quiz, when you answer a question, only that section refreshes instead of the entire page. How can I create a quiz like this? ...

Having trouble launching Cypress on my Mac - stating that it cannot find Cypress

Despite searching through multiple answers on S.O, none of them have solved my issue. To better explain my question, I've created a video. You can view it here Everything was working perfectly just yesterday, so what could have possibly gone wrong? ...

PHP integration with input forms

Is there a way to have multiple text fields that input data into various sections on a single webpage? If necessary, I could split the content over two pages. I've heard that this may involve using a PHP script, but I'm unsure of the steps to imp ...

A guide on defining the width of an element within a flex container when it overflows

Here is the code snippet I have: .divA { background: red; width: 500px; display: flex; flex-direction: row; overflow-y: scroll; } .epi { width: 50%; background: blue; } <div class="divA"> <div class="epi"> <h1>dsds ...

What might be causing issues with the functionality of my user registration form?

I have been working on creating a registration form for users using PHP and MySQL. However, I'm facing an issue where no new records are added to the database when I submit the form. It's strange because the database has worked perfectly fine wit ...

The issue of the jQuery dialog failing to close after an AJAX login

Currently, I am implementing a JQuery UI Dialog for a popup login box on my MVC3 application. The functionality is working to log the user in, refresh the "login-status" div on the parent page, and display validation errors in the popup if the login is uns ...

Using Elasticsearch's bulk feature to set unique identifiers(_id) for documents

Whenever I attempt to insert documents into elasticsearch with a set _id, I encounter the following error: The field [_id] is considered a metadata field and cannot be included within a document. It should be utilized in the index API request parameters in ...

Display Issue on Android Devices

I've been experimenting with an HTML file on Android tablets and phones to adjust the viewport for different resolutions. I used the meta viewport tag: <meta name="viewport" content="width=device-width, target-densityDpi=device-dpi, user-scalable ...

Input information into the system from the successful response found on a different page

After receiving my ajax response successfully, I want to redirect to a new aspx page where there are 30 input type text controls. My goal is to populate all the values in those controls. How can I achieve this? Any suggestions? This is what I have attempt ...

Tips for adjusting the color using inline CSS

Currently, I have a div styled with blue color using CSS. However, I now require the ability to dynamically change this color. My plan is to retrieve the desired color from a database and use its hexadecimal code in place of the default blue. What is the ...

Understanding the assignment and control flow mechanisms in JAVASCRIPT code

Can you explain how the following code snippet functions? It is a part of a JavaScript file. this.isTabSelected = function(tabToCheck) { return (this.tab === tabToCheck); } ...