How to make a simple website menu in Javascript.

Keep it simple: A complex menu can be overwhelming for users. Stick to a simple layout and use clear, descriptive labels for each menu item.

 There are a few key things to consider when designing a menu for your website:

Keep it simple: A complex menu can be overwhelming for users. Stick to a simple layout and use clear, descriptive labels for each menu item.

Use hierarchy: Organize your menu items into categories, with the most important items at the top. This will help users find what they're looking for more easily.

Make it responsive: Your menu should be easy to use on both desktop and mobile devices. Consider using a responsive design that adjusts to the size of the screen.

Use color and typography effectively: Use colors and typography to make your menu visually appealing and easy to read. Avoid using too many colors or fonts, as this can be distracting.

Consider the user experience: Think about how users will interact with your menu. Will they be using a mouse, keyboard, or touch screen? Make sure your menu is easy to navigate no matter how it is being accessed.


Here is an example of a simple JavaScript code that you can use to create a menu for your website:



<nav>

  <ul>

    <li><a href="#">Home</a></li>

    <li><a href="#">About</a></li>

    <li><a href="#">Contact</a></li>

  </ul>

</nav>


<style>

  nav {

    background-color: lightblue;

  }

  ul {

    list-style-type: none;

    margin: 0;

    padding: 0;

    overflow: hidden;

  }

  li {

    float: left;

  }

  a {

    display: block;

    color: black;

    text-align: center;

    padding: 14px 16px;

    text-decoration: none;

  }

  a:hover {

    background-color: lightgray;

  }

</style>

Here is the result


This code creates a navigation bar with three menu items: "Home", "About", and "Contact". The style element contains CSS code that controls the appearance of the menu, such as the background color and the hover effect.


You can customize the menu by modifying the HTML and CSS code to suit your needs. For example, you can add more menu items, change the colors, or modify the layout.

Here is a bit more advanced menu:

Here’s a simple horizontal scrolling menu with dropdown functionality and interactive lighting effects using HTML, CSS, and JavaScript.

HTML:


<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Horizontal Scroll Menu with Dropdown</title> <link rel="stylesheet" href="styles.css"> </head> <body> <div class="menu-container"> <ul class="menu"> <li class="menu-item"> <a href="#">Home</a> </li> <li class="menu-item dropdown"> <a href="#">Services</a> <ul class="dropdown-content"> <li><a href="#">Web Design</a></li> <li><a href="#">SEO</a></li> <li><a href="#">Marketing</a></li> </ul> </li> <li class="menu-item"> <a href="#">Portfolio</a> </li> <li class="menu-item dropdown"> <a href="#">About</a> <ul class="dropdown-content"> <li><a href="#">Team</a></li> <li><a href="#">Contact</a></li> </ul> </li> <li class="menu-item"> <a href="#">Blog</a> </li> </ul> </div> <script src="script.js"></script> </body> </html>

---------------------------------------------------------------------------------------------------

CSS (styles.css):


/* Menu Styling */ body { margin: 0; padding: 0; font-family: Arial, sans-serif; background-color: #222; } .menu-container { overflow-x: auto; white-space: nowrap; background-color: #111; padding: 10px 0; box-shadow: 0 4px 10px rgba(0, 0, 0, 0.3); } .menu { display: flex; list-style-type: none; margin: 0; padding: 0; } .menu-item { display: inline-block; padding: 15px 30px; position: relative; text-align: center; } .menu-item a { text-decoration: none; color: white; font-size: 18px; transition: color 0.3s ease; } .menu-item a:hover { color: #f39c12; box-shadow: 0px 4px 8px #f39c12; } /* Dropdown Styling */ .dropdown-content { display: none; position: absolute; top: 60px; left: 0; background-color: #333; min-width: 200px; box-shadow: 0px 8px 16px rgba(0, 0, 0, 0.3); } .dropdown-content li { padding: 10px; } .dropdown-content li a { text-decoration: none; color: white; display: block; } .dropdown-content li a:hover { background-color: #555; color: #f39c12; box-shadow: 0px 4px 8px #f39c12; } .menu-item.dropdown:hover .dropdown-content { display: block; } /* Interactive Lighting Effect */ .menu-item a::before { content: ''; position: absolute; top: 0; left: 50%; width: 0; height: 3px; background-color: #f39c12; transition: width 0.3s ease, left 0.3s ease; } .menu-item a:hover::before { width: 100%; left: 0; }

-----------------------------------------------------------------------------------------------------------

JavaScript (script.js):


// Optional JavaScript for smooth scrolling effect
const menu = document.querySelector('.menu-container'); menu.addEventListener('wheel', (e) => { e.preventDefault(); menu.scrollLeft += e.deltaY; });

Features:

  1. Horizontal Scrolling: The entire menu can scroll horizontally when it overflows the container, making it responsive to many items.
  2. Dropdowns: Hover over any menu item with a dropdown to reveal sub-menu options.
  3. Interactive Lighting: Hover effects provide interactive lighting on both the main menu and dropdown items.

You can adjust and enhance the lighting, transitions, or even make the scroll smoother depending on your preference!

How you can edit it or improve it and how the code works explanation

Basic Structure

The menu is structured with three key components: HTML for the layout, CSS for styling and effects, and JavaScript for adding scrolling behavior. These components work together to create a user-friendly, interactive horizontal navigation bar that includes dropdown menus and a lighting effect when hovering over items.

HTML

The HTML part defines the structure of the menu. It contains an unordered list (<ul>) of menu items (<li>). Each list item can either be a single clickable link or a link with a dropdown. The dropdowns are created using nested lists inside the main menu item. This simple structure is easy to understand and can be edited by adding, removing, or rearranging menu items.

CSS

The CSS is where the visual magic happens. It defines the menu's layout, positioning, colors, and interactive effects. The CSS also handles two key features: the dropdown menus and the interactive lighting effect.

  • Dropdowns: These are hidden by default using display: none; but become visible when the user hovers over the menu item containing the dropdown.
  • Lighting effect: When you hover over a menu item, a small bar (like a "light") appears below the text and expands across the width of the menu item. This creates a visual feedback effect that makes the interface feel more dynamic and interactive.

JavaScript

The JavaScript component adds a smooth scrolling feature to the menu. When you scroll with your mouse wheel or touchpad, the menu slides horizontally instead of the usual vertical scrolling. This is useful for navigation menus that have many items, making it easier for users to browse without crowding the page.


2. How to Edit the Menu

Editing this menu is straightforward. Here's how you can adjust each part to suit your needs.

Adding or Removing Menu Items

  • To add new menu items, go to the HTML section and copy an existing <li> block. Paste it where you want the new item to appear and change the text inside the <a> tag.
  • To remove a menu item, simply delete the <li> block that contains that menu item.

Adding or Removing Dropdowns

  • To add a dropdown, use the structure of an existing dropdown as a template. Copy the code for a dropdown (<li> with a nested <ul>) and place it where you want the new dropdown to be. Change the links inside the dropdown as needed.
  • To remove a dropdown, delete the nested <ul> and its contents, leaving only the main <li> item.

Changing Colors and Fonts

  • In the CSS file, you can easily modify colors. Look for properties like background-color, color, and box-shadow to adjust the look of the menu.
    • For example, changing the background color of the menu from dark gray to blue can be done by replacing background-color: #111; with background-color: #007BFF;.
  • You can also change the font size and style by adjusting the font-size or font-family in the .menu-item a section of the CSS. This will alter the appearance of the text in the menu.

Modifying the Lighting Effect

  • The lighting effect appears when you hover over a menu item. The effect is controlled by the CSS ::before pseudo-element. You can change the color of this light by modifying the background-color property.
    • If you want the lighting to be red instead of orange, replace background-color: #f39c12; with background-color: #FF0000;.
  • The width of the lighting effect is animated using CSS transitions. You can make this effect faster or slower by adjusting the transition property. For example, changing transition: width 0.3s ease, left 0.3s ease; to transition: width 0.5s ease, left 0.5s ease; will slow down the animation.

Modifying the Scroll Behavior

  • The horizontal scroll is controlled by JavaScript. By default, the script makes the menu scroll horizontally when the user moves the mouse wheel. You can adjust the scrolling speed by modifying the value menu.scrollLeft += e.deltaY;. For example, multiplying the deltaY value (e.g., menu.scrollLeft += e.deltaY * 2;) will make the scrolling faster.
  • If you want to disable scrolling entirely, you can simply remove the JavaScript part. The menu will still function, but users will need to use a scrollbar to navigate if the menu overflows.

3. How to Improve the Menu

There are several ways to enhance this menu to make it more visually appealing or add new functionality:

Add Animations to Dropdowns

Currently, the dropdowns appear instantly when hovered over. You can add a smooth animation for a more polished feel by using CSS transitions. For example, you can add a fade-in effect to the dropdown by applying a transition to the dropdown-content class. Adding a transition: opacity 0.3s ease; property and adjusting the opacity from 0 to 1 will create a fade-in effect.

Add Mobile Responsiveness

The current menu works well on desktop, but on smaller screens, you might want the menu to stack vertically or turn into a hamburger menu. You can achieve this by adding media queries in CSS. Media queries allow you to apply different styles depending on the screen size.

  • For example, in a mobile view, you can hide the horizontal scroll and instead make the menu items stack vertically.
    • Use @media (max-width: 768px) to apply different styles for screens smaller than 768 pixels wide. You could change the .menu class to flex-direction: column; so the items appear in a vertical list.

Add Active States to Menu Items

You can improve user experience by adding an "active" state to the menu items, highlighting the current page the user is on. To do this, add an active class to the <li> or <a> element of the menu item that corresponds to the current page. In CSS, style the active class to change the color or background of the active menu item.

Improve Accessibility

For better accessibility, consider adding ARIA (Accessible Rich Internet Applications) labels to the dropdowns to help screen readers understand the structure of the menu. You can also ensure that the dropdowns can be navigated using the keyboard by adding tabindex attributes to make them focusable and use JavaScript to open them when the user presses the "Enter" or "Space" keys.


Conclusion

The horizontal scrolling menu with dropdowns and lighting effects offers a sleek, interactive design suitable for various websites. You can customize it by adding or removing menu items, modifying colors and animations, or enhancing it with responsive design and improved accessibility. The flexibility of HTML, CSS, and JavaScript allows for easy edits and upgrades to create a truly unique menu that matches your website’s style and functionality.

Post a Comment

Cookie Consent
Zupitek's serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.