Tips for animating a clicked list item from its current location to the top position and simultaneously hiding all other list items

Is it possible to create a dynamic list where clicking on an item will move it to the top position using jquery animate and hide the rest of the list items simultaneously?

$(document).ready(function() {
  $(".menu").click(function() {
    $("li").animate({
      top: '2px'
    }, 'slow');
  });
});
ul>li {
  list-style-type: none;
  line-height: 34px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul class="menu">
  <li>Account</li>
  <li>Profile</li>
  <li>History</li>
</ul>

Answer №1

If you want to display a clicked element, you can use the addClass method along with CSS for the added class. Simultaneously, you can hide other list items using jQuery's hide() function. This code snippet below demonstrates how to achieve this effect:

 $(document).ready(function() {
  $("li").click(function() {
        $(this).addClass('selected');
        $('li').hide();
    });
});
ul>li {
  list-style-type: none;
  line-height: 34px;
}
.selected{
  display:inline-block !important;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="menu">
  <li>Account</li>
  <li>Profile</li>
  <li>History</li>
</ul>

Answer №2

To create an animation effect on the element, it must support the CSS property top. I have successfully enabled the element to use the top property and animated it by moving it to the top while simultaneously decreasing its opacity using a transition. This jQuery implementation is specifically applied to the sibling li elements of the one that is clicked.

$(document).ready(function() {
  $(".menu li").click(function() {
    $(this).siblings('li').animate({
      top: '-60px'
    }, 'fast');
    $(this).siblings('li').css({
      'opacity': '0'
    });
  });
});
ul>li {
  list-style-type: none;
  line-height: 34px;
  position: relative;
  top: 10px;
  transition: all 2s ease;
}

.menu {
  position: absolute;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul class="menu">
  <li>Account</li>
  <li>Profile</li>
  <li>History</li>
</ul>

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

"Integrating `react-textarea-code-editor` with Remix: A Step-by-Step Guide

Upon loading the root of my web app, I encountered an error. The react-textarea-code-editor component is accessed via a separate route. The same error persisted even after following the suggestions provided here: Adding react-textarea-code-editor to the ...

PHP and mysqli combine to create a versatile image slider with changing images

I am looking to implement an image slider that can be updated by the admin using php and mysqli. The admin should have the ability to easily add or remove images as needed. ...

Create a list using ReactJS

I've been working on rendering a dynamic list in JSX, but I'm facing issues displaying the items. Below is my code snippet where I attempted to use useState const [orderList, setOrderList] = useState([]) and setOrderList(prev => [...prev, chil ...

Using Jquery and AJAX to dynamically fill out an HTML form based on dropdown selection

My goal is to populate a form on my webpage with information pulled from a MySQL database using the ID of the drop-down option as the criteria in the SQL statement. The approach I have considered involves storing this information in $_SESSION['formBoo ...

Update two distinct object properties using a singular reference in Vue's set method

I'm facing an issue while trying to insert an object into an array using the Vue.set function. The problem is that it adds the item to a different object with the same array property. Here's a snippet of my code: data() { return { ... ...

How can I specifically disable the next month in MUI Datepicker?

Employing MUI version 5, how can I disable only the next month in the datepicker while keeping the others functional? ...

Implementing an OnChange Event for a Multi-Select Feature in a Vue.js Application

Here is the HTML code for my multi-select input: <select id="invitees_list1" class="form-select" multiple name="multi"> @foreach ($seatedTable->invitees as $invitee) <option> {{ $invitee ...

Storing Boolean values in MongoDB with Express JS: A Step-by-Step Guide

I am encountering an issue where a Boolean value that I am trying to store in Mongodb always returns false. Below is the schema of my database: const UserSchema = new Schema({ name: String, password: { type: String, required: true }, isAdmi ...

Utilizing nested flex containers with overflow-x property

I'm looking to set up a layout with 2 columns side by side. The first column should have a fixed width of 200px, while the second column should take up the remaining space on the screen. Additionally, I want the content in the second column to auto-sc ...

Visualizing Data with Flot.js - A Comprehensive Guide

Does anyone know how to organize values in a flot histogram by day, merging them into one bar per day? I've been searching for a solution but can't seem to find one. __ If you have any ideas, please share! ...

The Sequelize error message states: TypeError: an array or iterable object was expected, but instead [object Null] was received

I am encountering an issue with the findOne method from sequelize in my model. The error message I am getting states that the table referenced by the model is empty. How can I resolve this? Unhandled rejection TypeError: expecting an array or an iterable ...

Unable to assign headers once they have already been sent to the recipient - a Node.js error

Encountering an error message stating "Cannot set headers after they are sent to the client." I've researched and it seems like multiple callbacks may be causing this issue. However, I'm struggling to find a solution. Any assistance in resolving ...

Maximizing the efficiency of threejs through combining and selecting items

In my exploration of three.js optimization, I discovered that reducing the number of draw calls is crucial for improving performance. One way to achieve this is by consolidating geometries through the use of GeometryUtils.merge. Although this optimization ...

Split the input string into individual characters for each entry

As a beginner in Angular, I am currently exploring ways to split a single input field into separate inputs for each word. I attempted to achieve this using jQuery but struggled and also learned that mixing jQuery with Angular is discouraged. Here's th ...

Creating a promise to write data to a file

When executing the function, it creates a series of files but fails to write data to them. Strangely, omitting Promise.all at the end and not resolving the function actually results in the data being written to the files. It's puzzling that no matter ...

Encountering a node globby error when implementing multiple patterns

Currently, I am successfully using node glob for folder1 as shown below: glob('folder1/*.js'), function(err, files){ if (err) { console.log('Not able to get files from folder: ', err); } else { files.forEach(function (file) ...

Filtering by labels

I need to search for the label "1125" and if it is not found, I want to show an alert box. Here is my code: if (var id = $('mobilelabel_2:contains("1125")').attr('for');) { alert ("works"); } else { alert ("try again"); } ...

Updating image URL for grouped objects with Fabric JS

Check out this link :http://jsfiddle.net/mishragaurav31/ang58fcL/#base I created two groups of objects; one consisting of a circle and text, and the other with two images. When I try to change attributes for group 1, it works fine. However, when attempt ...

Debugging TypeScript on a Linux environment

Approximately one year ago, there was a discussion regarding this. I am curious to know the current situation in terms of coding and debugging TypeScript on Linux. The Atom TypeScript plugin appears promising, but I have not come across any information ab ...

What is the integration process for jTable and Symfony 2?

After creating a Datagrid using jTable, I have included my JavaScript code in twig: <script type="text/javascript> $(document).ready(function () { jQuery('#grid').jtable({ title: 'Table of products', ...