Women in Tech Speaking Tech

I just had the opportunity to speak at a technical conference where all the presenters and attendees were women. I was so honored to participate and hope my presentation on the vulnerabilities I find…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Responsive Drop Down Menu With Less Than 20 Lines Of Code By Example

Everyone At this point has visited a web page that uses a button to open a menu, View An Image, or just show more information.

Even this website/app Medium.com uses this piece of UI. Notice the 3 dots in the upper right corner. When clicked a menu suddenly appears. Click it again and it disappears.

Let's go over how simple it can be to code this yourself by looking at a few examples.

Let's start out with a basic HTML page layout. You find one Here!

The key for everything to work is having a link to JQuery in your head section:

Next, Let us add some HTML in the body section to represent the button and the menu that appears when it is clicked.

We are not going to add any styling, but just add a class to any of the HTML elements and add some CSS.

Now we need to link our HTML to an external Javascript sheet in a directory named JS. It is very important that this link is after Jquery is called. Our code relies on JQuery and will throw an error if it is not loaded first.

Now lets put some code in the main.js file:

You can save and reload your HTML page and test it out. This a great example of the beauty of object-oriented Javascript. The code is almost self-explanatory. In the second line of the code, we declare what HTML elements we want initially want to be hidden when the page renders.

In this example, we want the drop-down menu and the button that closes it to be hidden. JQuery makes this easy by using the CSS class names to reference the HTML elements in the Javascript code. Then it's as simple as calling the hide() function for each element we want to hide.

The next group of code opens the menu.

We want the menu to be visible when the hamburger button is clicked. We also want the hamburger button to be hidden and the close button to become visible.

We call the click() function on the button with the class ‘hamburger’. Inside the click function brackets, we call the slideToggle function on the ‘menu’ class. The first parameter of the slideToggle function is the speed in which the menu appears. The second parameter is an anonymous function that includes hiding the ‘hamburger’ button then showing the close button with the class ‘cross’.

The last group of code in the main.js file closes the opened menu, hides the close button and returns the hamburger button.

We call the click() function on the button with the class ‘cross’. Inside the click function brackets, we call the slideToggle function again on the ‘menu’ class. I set the first parameter of the slideToggle function to slow this time just so you can see how different parameters can affect the animation. The second parameter is an anonymous function that includes hiding the ‘cross’ button then showing the close button with the class ‘hamburger’.

This simple code opens up a bunch of possibilities for restructuring your user interface. Besides opening a menu, this can be used just to hide extra information or even adapted to view images in a gallery set up. I’ll add some links soon to more examples.

Add a comment

Related posts:

HOW TO CHECK PAN CARD STATUS?

Permanent Account Number, commonly called PAN, is a way to identify various taxpayers in our country. It is a unique 10 alphanumeric identification numbers or code that acts as identification for an…

39 Australian Social Entrepreneurs to Watch in 2021

2020 has certainly been a trying year for the social sector, but that doesn’t mean that there aren’t things in the works for next year. We’re at a point in time where organisations committed to a…

From Idea to App Markets with no Dev or Coding background.

Tell me you never had a millionaire idea coming up on your brain, tell me that you never thought of bringing to life this idea with an app, or even tell me you have never said to friends or…