Customize Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorized as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customized advertisements based on the pages you visited previously and to analyze the effectiveness of the ad campaigns.

No cookies to display.

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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
var enlaces = document.querySelectorAll("ul li a");
var enlaces = document.querySelectorAll("ul li a");
var enlaces = document.querySelectorAll("ul li a");

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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
var contenidoDinamico = document.getElementById("texto-contenido");
var contenidoDinamico = document.getElementById("texto-contenido");
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.
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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>";
});
});
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>"; }); });
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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<span class="rt-label rt-prefix">Reading Time:</span>
<span class="rt-time">3</span>
<span class="rt-label rt-postfix">minutes</span>
<span class="rt-label rt-prefix">Reading Time:</span> <span class="rt-time">3</span> <span class="rt-label rt-postfix">minutes</span>
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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
var enlaces = document.querySelectorAll("ul li a");
var enlaces = document.querySelectorAll("ul li a");
var enlaces = document.querySelectorAll("ul li a");

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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
var contenidoDinamico = document.getElementById("texto-contenido");
var contenidoDinamico = document.getElementById("texto-contenido");
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.
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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>";
});
});
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>"; }); });
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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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");
}
}
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"); } }
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:

0

Leave a Comment