HTML, CSS, and JavaScript Menu

Tiempo de lectura: 3 minutos

To create a horizontal menu with different options and change the content based on the selected option, you need to follow these steps.

First, create an unordered list <ul> that represents your menu and give it some styles to remove bullets and give it a dark background.

Each list item <li> represents an option in the menu and is floated to the left (float: left) to keep them in line.

The links <a> within each list item <li> are used as menu items and styled to have white text and a button style when hovered over (:hover).

Then, select all the link elements (<a>) within the list items <li> and store them in the variable enlaces as shown below:

var enlaces = document.querySelectorAll("ul li a");

Next, get the dynamic content element with the id “texto-contenido” into the variable contenidoDinamico:

var contenidoDinamico = document.getElementById("texto-contenido");

Add a click event handler for each link in the menu. When a link is clicked, it executes a function that does the following:

  • Removes the “selected” class from all links to deselect any previously selected link.
  • Adds the “selected” class to the currently selected link.
  • Gets the text of the selected link, which represents the selected menu option.
  • Updates the dynamic content with a title (<h1>) displaying the selected option and a paragraph (<p>) with dummy content related to the option.
enlaces.forEach(function(enlace) {
    enlace.addEventListener("click", function() {
        // Remove the "selected" class from all links
        enlaces.forEach(function(enlace) {
            enlace.classList.remove("selected");
        });

        // Add the "selected" class to the clicked link
        this.classList.add("selected");

        // Get the text of the selected option
        var opcionSeleccionada = this.textContent;

        // Update the dynamic content based on the selected option
        contenidoDinamico.innerHTML = "<h1>" + opcionSeleccionada + "</h1><p>Content of " + opcionSeleccionada + "</p>";
    });
});

Finally, add a function called menuopciones that is triggered when the user scrolls the page (window.onscroll). This function adds or removes the “opcionselect” class from the menu depending on whether the user has scrolled the page enough from the top. This is done to create a menu effect that sticks to the top when the user scrolls down.

Reading Time:
    3
    minutes

To create a horizontal menu with different options and change the content based on the selected option, you need to follow these steps.

First, create an unordered list <ul> that represents your menu and give it some styles to remove bullets and give it a dark background.

Each list item <li> represents an option in the menu and is floated to the left (float: left) to keep them in line.

The links <a> within each list item <li> are used as menu items and styled to have white text and a button style when hovered over (:hover).

Then, select all the link elements (<a>) within the list items <li> and store them in the variable enlaces as shown below:

var enlaces = document.querySelectorAll("ul li a");

Next, get the dynamic content element with the id “texto-contenido” into the variable contenidoDinamico:

var contenidoDinamico = document.getElementById("texto-contenido");

Add a click event handler for each link in the menu. When a link is clicked, it executes a function that does the following:

  • Removes the “selected” class from all links to deselect any previously selected link.
  • Adds the “selected” class to the currently selected link.
  • Gets the text of the selected link, which represents the selected menu option.
  • Updates the dynamic content with a title (<h1>) displaying the selected option and a paragraph (<p>) with dummy content related to the option.
enlaces.forEach(function(enlace) {
    enlace.addEventListener("click", function() {
        // Remove the "selected" class from all links
        enlaces.forEach(function(enlace) {
            enlace.classList.remove("selected");
        });

        // Add the "selected" class to the clicked link
        this.classList.add("selected");

        // Get the text of the selected option
        var opcionSeleccionada = this.textContent;

        // Update the dynamic content based on the selected option
        contenidoDinamico.innerHTML = "<h1>" + opcionSeleccionada + "</h1><p>Content of " + opcionSeleccionada + "</p>";
    });
});

Finally, add a function called menuopciones that is triggered when the user scrolls the page (window.onscroll). This function adds or removes the “opcionselect” class from the menu depending on whether the user has scrolled the page enough from the top. This is done to create a menu effect that sticks to the top when the user scrolls down.

window.onscroll = function() {
    menuopciones();
};

var menu = document.querySelector("ul");
var opcionselect = menu.offsetTop;

function menuopciones() {
    if (window.pageYOffset >= opcionselect) {
        menu.classList.add("opcionselect");
    } else {
        menu.classList.remove("opcionselect");
    }
}

Here’s the complete code and the result:

Leave a Comment