Animate a list to expand and collapse the menu options

Here's the concept I'm aiming to achieve:

  1. When "PORTFOLIO" is clicked, smoothly push down everything on the page;
  2. New links should fade in smoothly;
  3. If "PORTFOLIO" is clicked again, reverse all animations.

This is my current code snippet:

$(function(){
$('[data-toggle]').on('click', function(){
var id = $(this).data("toggle"),
$object = $(id),
className = "open";

if ($object) {
if ($object.hasClass(className)) {
$object.removeClass(className)
    } else {
$object.addClass(className)
}
}
});
});
#list{
display: none;
}

#list.open{
display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<nav>
    <ul>
        <li><a href="#">Home</a></li>
        <li><a href="#">About Us</a></li>
        <li><a href="#" class="hide" data-toggle="#list">Portfolio</a>
            <ul id="list">
                <li><a href="#">Commercial Projects</a></li>
                <li><a href="#">Residential Projects</a></li>
                <li><a href="#">Institutional Projects</a></li>
                <li><a href="#">Buildings</a></li>
            </ul>
        </li>
        <li><a href="#">Contact</a></li>
    </ul>
</nav>

Is it possible to achieve this without using JavaScript, only with CSS? I'm struggling with the animation part and have tried using CSS Transitions property without success.

Any suggestions for improving the HTML markup and JS implementation? I feel like there might be a better way to do this... any advice would be greatly appreciated.

Answer №1

Update: Disregard, someone has already provided an answer in the comments.

It seems like your code is functioning well, but I may not completely grasp your question.

If you wish to keep the text unchanged for the + and - symbols, consider adding a new element or tag at the end of the list items to display them.

While I am not proficient in JavaScript, using text() or html() on a span within the list text may prevent it from being altered.

Here is an example of how you could structure your HTML:

<nav>
<ul>
    <li>Home</li>
    <li>About Us</li>
    <li><a href="#" id="item3link" style="text-decoration: none; color: black;">Portfolio <span id="item3icon">+</span></a>
        <ul id="list" class="subClassHide">
            <li>Commercial</li>
            <li>Residential</li>
            <li>Institutional</li>
            <li>Buildings</li>
        </ul>
    </li>
    <li>Contact</li>
</ul>

In case I misinterpreted your query, I hope this response can still inspire some solutions for you.

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

MeteorJS insert function failing to add new data

After spending the last 4 and a half hours scouring the internet, I'm still stuck on why my insert method in my real time messaging app isn't working. It's not throwing any errors, it's just not actually inserting anything. Here's ...

Hold on for the processing of a CSV document

I am attempting to utilize the "csv-parse" library in Typescript to read a csv file by creating an observable. The code provided uses fs.createReadStream to read the file. I am looking to return the observable and subscribe to it, but it seems that the p ...

Include two additional elements for action in a list

In my React project, I've created a list with the following elements: Avatar Text Edit icon Delete icon I managed to set up the structure successfully up until the delete icon. Now, how can I properly add it without overlapping the edit icon? Both ...

The module 'cropit' is not located within Npm's directory

I'm having trouble with npm recognizing my installation of cropit. In my package.json, I have specified the following lines: "cropit": "^0.1.9", "jquery": "~2.1.1" Could it be that I selected the wrong version? Do the versions of jquery and cropit I ...

Please submit the form triggered by a JavaScript event

I am looking to create a form that can be submitted when a user presses a key, such as tab or enter, which would cause them to lose focus. Additionally, clicking outside of the text field should also trigger a submit action. However, if the user clicks on ...

Ways to align list items to the right and left in a stylish manner using HTML and CSS while maintaining alternating bullet points

Is there a way to create a zig-zag list where alternate list elements are right aligned while the others remain left aligned? I want it to look like this: Item1 Item2 Item3 ...

Creating a SVG polygon using an array of points in JavaScript

Consider the following array containing points: arr = [ [ 0,0 ], [ 50,50 ], [ 25,25 ], ]; I would like to create an SVG polygon using this array. Initially, I thought the code below would work, but it doesn't: <polygo ...

Developing Unique Number Formatting in Angular 5 with TypeScript

I am in need of a solution to format numeric values in a specific way. Here is the criteria: If a number has no decimal places, leave it as is. If it has any decimal places, format it with 4 digits after the "," or "." Here are some examples: No Formatti ...

Function with defer that calls itself repeatedly

I am attempting to utilize a recursive function where each function runs only after the previous one has completed. This is the code I have written: var service = ['users', 'news'], lastSync = { 'users' : ...

The perplexing simplicity of closure

Currently, I am endeavoring to enhance my knowledge of JavaScript closures. Let's consider the following two scenarios in Node.js: function A(){ console.log(data); //this will result in a null pointer } module.exports = function(data){ re ...

Using the ng-class directive to dynamically set classes based on the value returned from

I am facing an issue with setting the icon style based on a value returned from the controller. The console log confirms that the value is triggered correctly, but it appears that there is a problem with the Ng-class expression. Any assistance on this matt ...

Having trouble with Ajax not sending data to PHP in JSON format

After attempting various methods using different ajax techniques, I still cannot get this to work. Being new to ajax and json, I initially thought that the issue might be due to the data not being in a proper form. However, other ajax processes structured ...

Having trouble getting PHP to display an image on the webpage

When attempting to output a simple image using PHP, I noticed that it only displayed a white square. This led me to believe that the IMG file could not be found. However, when I used a basic HTML IMG tag, the file was located without any issues. Even try ...

Explore various SVG paths by hovering to reveal or conceal different divs

Hi there, I'll do my best to explain clearly. If this question has already been asked, I apologize as I couldn't find an answer when searching. I've used SVG to create a map on my HTML webpage and it's looking great. I also have hidden ...

Update the default base URL configuration for Axios

My axios configuration looks like this: const configAxios = { baseURL: 'http://127.0.0.1:8000/api', timeout: 30000, }; Vue.prototype.$axios = axios.create(configAxios) When making a call inside my component, I use the following syntax: this ...

Appearing text dialogue

Can you tell me the name of the box that appears on top of a webpage? A couple of examples include: Facebook photos - It dims the background webpage and opens a separate pop-up to display pictures. Advertisements - The box that typically requires clickin ...

Users are reporting that verification emails are not being sent when the Accounts.createUser function is used within

I have a simple meteor method set up to create user accounts. In my server/methods.js file: Meteor.methods({ createUserAccount: function(user) { return Accounts.createUser(user); } }); Then in my server/init.js file: Meteor.startup(function() ...

Using AngularJS, I can bind the ng-model directive to asynchronously update and retrieve data from

I am currently working with Angular to develop a preferences page. Essentially, I have a field in a mysql table on my server which I want to connect my textbox to using ng-model through an asynchronous xhr request for setting and fetching data. I attempt ...

Utilizing an additional parameter in React to dynamically change the API URL

Hello everyone! I'm facing a slight issue with one of my React applications: I'm attempting to retrieve Weather alerts using an API. Here's how my App.js file is set up: import React, { Component } from 'react'; import './App ...

Dealing with distinct form actions for 'new' and 'edit' in Rails using simple_form, remote_true, and custom controller

I've been following a tutorial on how Ajax works with Ruby on Rails. However, I decided to take a slightly different approach. After setting up a scaffold for 'tasks' (including the controller, model, and views), I created a new controller c ...