Sliding down dropdown menu with JQuery activation on click action

This is the code I have for opening a dropdown menu $smallScreenMenu when $iconMenu1 is clicked

Javascript code:

const $iconMenu1 = $('#iconMenu1')
const $smallScreenMenu = $('#smallScreenMenu')
$($iconMenu1.on('click', ()=>{
  if ($smallScreenMenu.css('display') == 'block'){
    $smallScreenMenu.css('display', 'none');
  }
  else{
    $smallScreenMenu.css('display', 'block');
  }

CSS code:

.icon-menu{
    right: 15px;
    position: absolute;
    top: 17px;
    font-size: 30px;
    background-color: transparent;
    border: none;
}

#smallScreenMenu{
    display: none;
    position: absolute;
    right: 0px;
    text-align: right;

HTML code:

<button class='icon-menu' id='iconMenu1'></button>
<div id='smallScreenMenu'>
                <ul> 
                    <li><a href='#'>Products</a></li>
                    <li><a href='#'>About</a></li>
                </ul>
            </div>

It works but I want it to slide instead of instantly appear/disappear. Can I modify this code to achieve that, or do I need to start from scratch? I tried using slideDown() and slideUp() without success. Thank you in advance for your help

Answer №1

Modify your menu class using toggles instead of direct style changes. Utilize CSS transitions to animate properties such as maximum-height:

const $iconMenu1 = $('#iconMenu1');
const $smallScreenMenu = $('#smallScreenMenu');

$($iconMenu1.on('click', () => {

$smallScreenMenu.toggleClass('closed');
  
}));
.icon-menu {
  right: 15px;
  position: absolute;
  top: 17px;
  font-size: 30px;
  background-color: transparent;
  border: none;
}

#smallScreenMenu {
  display: block;
  position: absolute;
  right: 10px;
  top: 60px;
  text-align: right;
  background: rgba(0,255,0,0.1);
  overflow-y: hidden;
  max-height: 360px;
  transition: all 360ms ease-in-out;
  box-shadow: 2px 4px 12px #bfbfbf;
}

#smallScreenMenu.closed {
   max-height: 0;
}

#smallScreenMenu ul {
  list-style-type: none;
  margin: 0.5em 1em;
  padding: 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button class='icon-menu' id='iconMenu1'>Menu</button>
<div id='smallScreenMenu' class="closed">
  <ul>
    <li><a href='#'>Products</a></li>
    <li><a href='#'>About</a></li>
    <li><a href='#'>Another</a></li>
    <li><a href='#'>Another</a></li>
    <li><a href='#'>Another</a></li>
  </ul>
</div>

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

setTimeout executes twice, even if it is cleared beforehand

I have a collection of images stored in the img/ directory named 1-13.jpg. My goal is to iterate through these images using a loop. The #next_container element is designed to pause the loop if it has already started, change the src attribute to the next im ...

Sending jQuery variables to PHP

Is it possible to transfer a jQuery variable to a php file? Let's take a look at the code in my_js.js: function updates() { $.getJSON("php/fetch.php", function(data) { $.each(data.result, function(){ var s_id = this[& ...

Tips for maintaining the parent's width during a resize operation

I have a query: I am facing an issue with an image inside a div. The image is larger than the div, so I set its height to 100% to make it appear correctly. When I resize the image, it adjusts well and looks fine. However, when I resize the browser to make ...

"Discover the method for tallying the quantity of radio buttons based on their names within AngularJS

Can anyone guide me on how to determine the number of radio buttons by name in AngularJS using a directive? <label ng-repeat="elements in [1,2,3,4,5,6,7]"> <input type="radio" name="phone" ng-model="phone" value="example"> </label& ...

Retrieving JSON data with jQuery's Ajax functionality

I'm currently developing a web application to handle the administrative tasks for a restaurant. The main functionalities include creating new orders, adding order items, and managing financial overviews. One of the key features is the ability to view ...

How do you set a background image for the color of the font?

Imagine I have this piece of code: <span>Hello world!</span> Along with the following CSS: span{ color:red; } Is it possible to replace the red value with an image? Something like url(images/text-bg.png);? I am looking to add a texture to m ...

What is the best way to ensure that a child div can expand to fit within the scrollable area of its parent div

I am facing an issue with a parent div that changes size based on the content inside it. When the content exceeds the initial size, causing the parent to scroll instead of expanding, I have a child div set to 100% width and height of the parent. However, t ...

The unrevealed node that is requesting the module is leading to an undefined outcome

I'm facing an issue where I need to import var server = require('../../index.js'); in my foo-dao.js file. This is necessary for accessing a hapi server plugin without passing it through the hapi request object from controller to dao. The pr ...

Create a connection between a div and a React component, allowing for the seamless transfer of

I'm looking to implement a feature where clicking on a specific div will redirect the user to another page, similar to React Router. However, I currently lack the knowledge to make it happen. Below is the code snippet in question: const Card: React.FC ...

I am searching for a tool in ThreeJS that functions similar to AxisHelper, possibly a Helper class or utility

While working with Three.js, I have come across many helpful Helper classes that greatly simplify displaying and modifying the scene. However, there is one particular tool that I am struggling to find again. It is similar to the AxisHelper, but it includes ...

Building better interfaces with Next.js, Styleguidist, and Fela for React applications

Has anyone successfully set up next.js with Fela and Styleguidist? I'm having trouble linking the Next.js webpack configuration to Styleguidist as mentioned in this article: I've been using this example app: https://github.com/zeit/next.js/tree ...

Problem with CSS hovering on images when hovering on links?

While attempting to add 2 hover images to my website, I encountered an issue where a black border appeared in the middle of the images. This was caused by the hover effects on the links. Below is the code snippet: a:hover,a:active { color:Black; o ...

Utilizing AJAX to dynamically populate a dropdown menu with JSON data

Currently, I am using ASP.NET 3.5 to make an AJAX call in order to populate dropdown lists as part of cascading dropdowns. Surprisingly, the JSON formatted results are being retrieved successfully. However, I am encountering difficulties when attempting to ...

What is the correct way to align an InputLabel in Material UI?

https://i.stack.imgur.com/Uafr1.png Looking for advice on styling the InputLabel in my code to improve its appearance. Code snippet below: <FormControl fullWidth> <InputLabel >Select EPE</InputLabel> <Select ...

The scrolltop animation does not appear to be functioning properly on iOS devices

I have implemented a JavaScript effect to increase the line-height of each list item on my website as you scroll. It works perfectly on my Macbook and Android Smartphone, but for some reason, it's not working on an iPhone. Can anyone provide a solutio ...

Updating image source attributes for all images with HTML Agility

I'm facing an issue where I need to change all src attributes in the HTML code to absolute paths instead of relative paths. I attempted to solve this problem using HTML Agility: string html = "<body><div><img src=\"/folder/a.png&b ...

Missing Overlay in jQuery UI Dialog

I have integrated the jquery ui dialogue into my asp.net mvc application. I customized the styles related to ui dialogues by copying and modifying them. For the overlay, I used the following style: .ui-widget-overlay { background: #666666 url(im ...

Initiate animation on command with either Jquery or JavaScript

I have a special notification display box that showcases errors on my website .flash { height: 75px; position: fixed; top: 20px; right: 20px; z-index: 10; background-color: #ffffff; box-shadow: 0 0 30px 2px #dddddd; -webkit-animation: flas ...

Tap on the key within the input field

Completing a login form programmatically: document.getElementById('i0116').value = email; document.getElementById('i0118').value = password; document.getElementById('idSIButton9').click(); A challenge arises w ...

Guide on how to load content into a DIV using JQuery after updating MySQL

I'm currently working on a project that involves AJAX and jQuery. My idea is to design a webpage that showcases information from a table. Additionally, I also plan to include a form where users can input data to add another row to the table. However, ...