Creating a dropdown menu and adjusting the content below

Feeling stuck in one place with my code here: link

The side navbar animation is almost what I want, but clicking one option opens all of them. Hoping for a similar effect like this page: link

$(document).ready(function() {
  $('.interfejs, .procesor, .dane, .grafika, .linux, .sieci').click(function() {
    $('.dropdown-content').toggleClass('visible-dropdown');
  })
})
/*===Site nav content===*/

.nav-header {
  width: 100%;
  height: 65px;
  position: relative;
  text-align: center;
}

.nav-header h2 {
  color: #fcfcfc;
  font-size: 33px;
  font-weight: 800;
  letter-spacing: 1px;
  position: relative;
  top: 50%;
  transform: translateY(-50%);
  padding: 0 13px;
  background-color: #011018;
  z-index: 1;
  width: auto;
  display: inline-block;
  text-shadow: 0px 0px 4px black;
}

.nav-header:after {
  content: '';
  background-color: #f5f5f5;
  height: 1px;
  width: 100%;
  position: absolute;
  top: 50%;
  left: 0
      margin-top: -0.5px;
}

.nav-menu {
  position: relative;
}

.nav-menu a {
  padding: 15px 8px 15px 32px;
  margin-left: 20px;
  text-decoration: none;
  font-size: 20px;
  font-weight: 200;
  letter-spacing: 1px;
  color: #818181;
  background-color: #011018;
  display: block;
  transition: 0.3s;
  position: relative;
}

.nav-menu a:hover {
  background-color: #212E35;
  color: #f5f5f5;
  letter-spacing: 2px;
  font-weight: 500;
}

.fa-angle-double-down {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  left: 5px;
}


/*===dropdown-content===*/

.dropdown-content {
  text-decoration: none;
  color: #fff;
  height: 0;
  transition: 0.3s height;
  background-color: #081d2a;
  box-shadow: inset 4px 4px 10px #05141D;
  margin-left: 20px;
}

.visible-dropdown {
  height: 100px;
}

.dropdown-content li {
  padding: 2px 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<nav class="site-nav">
  <header class="nav-header">
    <h2>Content List</h2>
  </header>
  <section class="nav-menu">
    <a href="#" class="interfejs"><span class="fa fa-angle-double-down fa-fw" aria-hidden="true"></span>Interfaces</a>
    <ul class="dropdown-content">
      <li>one</li>
      <li>two</li>
      <li>one</li>
      <li>two</li>
    </ul>
    <hr>
    <a href="#" class="procesor">Processors<span class="fa fa-angle-double-down fa-fw" aria-hidden="true"></span></a>
    <ul class="dropdown-content">
      <li>one</li>
      <li>two</li>
      <li>one</li>
      <li>two</li>
    </ul>
    <hr>
    <a href="#" class="dane">Data Storage<span class="fa fa-angle-double-down fa-fw" aria-hidden="true"></span></a>
    <ul class="dropdown-content">
      <li>one</li>
      <li>two</li>
      <li>one</li>
      <li>two</li>
    </ul>
    <hr>
    <a href="#" class="grafika">Graphics<span class="fa fa-angle-double-down fa-fw" aria-hidden="true"></span></a>
    <ul class="dropdown-content">
      <li>one</li>
      <li>two</li>
      <li>one</li>
      <li>two</li>
    </ul>
    <hr>
    <a href="#" class="linux">Linux<span class="fa fa-angle-double-down fa-fw" aria-hidden="true"></span></a>
    <ul class="dropdown-content">
      <li>one</li>
      <li>two</li>
      <li>one</li>
      <li>two</li>
    </ul>
    <hr>
    <a href="#" class="sieci">Computer Networks<span class="fa fa-angle-double-down fa-fw" aria-hidden="true"></span></a>
    <ul class="dropdown-content">
      <li>one</li>
      <li>two</li>
      <li>one</li>
      <li>two</li>
    </ul>
  </section>
  <!-- <button class="hamburger">
<div class="bar1"></div>
<div class="bar2"></div>
<div class="bar3"></div>
</button> -->
</nav>

Answer №1

To ensure you only have the clicked item open while closing any others that may be open, implement the following code snippet:

    <script>
        $(document).ready(function(){
          $('.interfejs, .procesor, .dane, .grafika, .linux, .sieci').click(function(){
            // Close any open dropdown content
            $('.dropdown-content').removeClass('visible-dropdown');
            // Open the clicked dropdown content
            $(this).next('.dropdown-content').addClass('visible-dropdown');
          })
        })
    </script>

Answer №2

Gerard's response is accurate, however, it would be more efficient and scalable to target just one class instead of each individual menu class.

$('.js-dropdown-toggle').on('click', function(e) {
  e.preventDefault();

  var $dropdownContent = $('.dropdown-content');
  $dropdownContent.removeClass('visible-dropdown');
  $(this).next('.dropdown-content').toggleClass('visible-dropdown');
});

https://jsfiddle.net/q5qv6ejj/

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

Calculate how frequently an element showcases a particular style

I attempted to tally the occurrences of a specific class on an element, but I keep encountering the error message: $(...)[c].css is not a function Does jQuery have it out for me? Below is the code snippet in question: // hide headings of empty lists le ...

Modifying the appearance of a text area through the adjustment of a single file

I am in the process of creating a unique code box style that I frequently update to keep it fresh. My goal is to have all the script external so that editing one file will automatically update all code boxes, eliminating the need to edit each one individua ...

Unable to get Bootstrap IMG-RESPONSIVE to function properly in Firefox and IE within the navbar

I recently encountered an issue with the Bootstrap IMG-RESPONSIVE class not functioning properly in the navigation bar on Firefox and IE. Oddly enough, it was working perfectly just a few days ago until I made some unknown changes, causing it to only work ...

Tips on updating the text of a chosen item in a dropdown list using jQuery

In the following code snippet, the "my_theme" element represents a select list populated with directory listings. Upon triggering the "change" event, jQuery makes a request to determine if the selected folder contains a style.css file. The response will b ...

Does width change based on position?

When I set position:absolute for an element, its width remains constant regardless of the text inside. However, if I set position:relative for the same element, the width will adjust based on the text content. I have created a fiddle with two menus, each ...

Tweaking the placement of the dropdown menu in the subnavigation area

Just getting back into coding after a long hiatus - I've dabbled in the past but it's been about 6 years since I've worked with any code. Currently, I'm working on replicating a website for practice. I have created a nav bar and a drop ...

"Troubleshooting: How to Fix Issues with document.getElementById on Dynamic Div

Struggling to incorporate div elements and generate graphs with Google charts? The issue arises in the draw function, where attempts to access a div element using document.getElementById() result in null values and an error message stating "container not ...

Is there a way to incorporate multiple classes/IDs into this jQuery script?

I'm wondering how I can add multiple classes or IDs in the following code. Any ideas on how to achieve that? $("#main-content").customScrollbar(); For example, let's say I want customScrollbar(); to be applied to both #main-content and .song-li ...

Converting HTML code to JSON using PHP scripting

Would appreciate your help in resolving a small issue. Although I came across this post, I am still encountering some errors: How can I convert HTML to JSON using PHP? I have created a PHP file that extracts a post from WordPress with the following forma ...

Tips for utilizing dynamic variables when setting props in React Native styles

I am encountering an issue while trying to set props in the stylesheet to dynamically change the background color based on data received from the server. undefined is not an object (evaluating 'this.props.colorCode') Below is the snippet of my ...

Utilize Jquery, Web Api, and Sql Server to easily store and access PDF and DOC files on your server in binary format

To achieve the task of uploading PDF or DOC files to a server using jQuery, Web API, and SQL Server, we must store them in Binary format in the database. Additionally, we need to be able to retrieve these files and download them onto the client system. ...

Looking to rearrange the layout of jqgrid

I need to reposition the jqgrid away from its default top left position. I have attempted some adjustments but they are not working as expected. Below is the code snippet for your review and suggestions: Here is the HTML code snippet: <div> &l ...

Bootstrap navbar partially collapsed with inner items concealed not at the edges

Many discussions have come up regarding partially collapsing Bootstrap navbars, such as: Item1 Item2 Item3 Item4 Item5 Is it possible to achieve the following collapsed format: Item1 Item2 Item3 =menu= Or: =menu= Item3 Item4 Item5 This method ...

Move your cursor over the top and bottom of the image to experience distinct effects

Trying to create an effect where the top 50% of an image shows a different color and text when hovered over, and same for the bottom 50%. Currently, only able to achieve this effect for the lower half of the image. Is there a way to make it work for both h ...

What could be causing the issue preventing me from updating my SQL database through AJAX?

$(document).ready(function(){ $('.button').click(function(){ var clickBtnValue = $(this).val(); var ajaxurl = 'functions/delivered.php', data = {'action': clickBtnValue}; $.post(ajaxurl, da ...

What is the best way to update a form after it has been submitted?

i have a form that looks like this <form id="abc"> <div> <select > <option id= "1"> ha1 </option> <option id= "1"> ha2 </option> <option id= "1"> ha3 </option> <option id= "1"> ha4 </option> ...

Unspecified data returned from PHP script via jQuery AJAX

Encountering an issue while trying to use AJAX to get a PHP response from a form. The JavaScript appears to be correct as it functions when the content of login_ajax.php is reduced to just: echo 'CORRECT' //or echo 'INCORRECT' Howev ...

What is the best way to create a sliding animation on a div that makes it disappear?

While I may not be an expert in animations, I have a question about sliding up the "gl_banner" div after a short delay and having the content below it move back to its original position. What CSS properties should I use for this animation? Should I use css ...

Angular 6 - detecting clicks outside of a menu

Currently, I am working on implementing a click event to close my aside menu. I have already created an example using jQuery, but I want to achieve the same result without using jQuery and without direct access to the 'menu' variable. Can someon ...

"Exploring the world of remote_form_tag in Rails with jrails

After transitioning to jQuery with jRails for my application, most of my previous RJS code is working perfectly. However, I am facing an issue with the :loading => callback when using the remote_form_tag. <% form_remote_tag :url => '/hostels ...