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

The Laravel href link will fail to work when the URL does not include the string 'http'

I am encountering an issue with a particular hyperlink tag <a href="{{ $organization->website }}">Link</a> When the URL in $organization->website does not start with http:// or https://, the link fails to direct me properly. Instead, it r ...

Please indicate the decimal separator for the Number() function

UPDATE : TITLE IS MISLEADING as testing showed that Number() returned a dot-separated number and the HTML <input type="number"/> displayed it as a comma due to locale settings. I changed to using an <input type="text"/> and filtered keydown lik ...

Which file extension is superior for SEO: .html, .php, or .aspx?

Which file extension is more SEO-friendly: .html, .php, or .aspx? Or is an extension-less URL the best option for SEO? ...

"Utilizing a Font Awesome icon within the dropdown list of a combobox in Ext JS

I have attempted multiple methods to add an icon in the displayfield when a combo box item is selected without success. Here is the fiddle link for anyone who would like to help me with this issue. Your assistance is greatly appreciated. View the example ...

Shifting a div from side to side

I don't have much experience with coding, so I was hoping for some assistance. My goal was to create a div that moves back and forth using toggleClass; $('div').on('click', function() { $(this).toggleClass('active') ...

Discovering distinct colors for this loading dots script

Looking for assistance with a 10 loading dots animation script. I'm trying to customize the color of each dot individually, but when I create separate divs for each dot and control their colors in CSS, it causes issues with the animation. If anyone ...

Is it possible to create Excel documents containing statistical graphs and pie charts by utilizing PHP and SQL?

I have a database filled with statistical data that I want to export into an excel file. Can anyone recommend any popular libraries or scripts for generating excel files? Additionally, I am interested in displaying some of the dry numerical data in p ...

Incorporating a feature that displays one show at a time and includes a sliding animation into an

Hey there! I have this show/hide script on my website that's been working well. However, there are a couple of things I need help with. I want to modify the script so that only one div can be shown at a time. When a div appears, I'd love for it ...

How is it possible for my search results page to retrieve the words I input?

Currently, I am in the process of creating the search result page and I plan to utilize dynamic routing for implementation. Here is a snippet of my search bar code: <Link href={`/productSearchResult/${searchWord}`}> <a className={styles.navbar_s ...

Automatically reload main page in Javascript upon closing child window (popup)

I am working with 3 php files: view.php, edit.php, and edit2.php. In view.php, I display the content of my database tables. edit.php is used to edit a specific row using textboxes, while edit2.php is responsible for updating changes in the database. Once ...

How to ensure onmouseenter and onmouseleave work properly in Chrome using HTML, JavaScript, and jQuery

<div style="position: relative; left: 50px; top: 30px; width: 300px; height: 150px; background: #222222;" onmouseenter="this.style.background='#aaaaaa'" onmouseleave="this.style.background='#222222';"></div> http://jsfiddle ...

Modify the h:outputText value dynamically with the power of jQuery!

Is it possible to use jQuery to dynamically change the value of my OutputText component? This is the code snippet for my component: <h:outputText id="txt_pay_days" value="0" binding="#{Attendance_Calculation.txt_pay_days}"/> I would apprecia ...

Creating an inverted curve or border-radius for a div element is a design technique that can add

I've been tackling a project that requires me to style the border of a div in an inverted manner. To achieve this, I'm limited to using only CSS and JS without any plugins. I've searched through various online resources and similar questions ...

Is there a way to have a button function as a submit button for a form even when it is located in a separate component within a react application?

I am in the process of creating a user-friendly "My Account" page using react, where users can easily update their account information. I have divided my components into two sections: the navbar and the form itself. However, I am facing an issue with the s ...

Crash in the Android WebView of the Galaxy S3 device due to Signal 11 SIGSEGV

My Android WebView is experiencing issues with memory access and crashes on a Galaxy S3 running Android 4.0.4. The HTML5 content includes interactive battles where enemies appear for the user to slash, interspersed with normal HTML pages. Despite the use o ...

Tips for ensuring all images are the same size within a div element

https://i.stack.imgur.com/EkmWq.jpg Is there a way to make sure all the images fit perfectly inside their respective border boxes without appearing stretched? I've tried setting fixed height and width within a div, but they always end up looking off. ...

Wordpress Sub-Menu Being Eclipsed by Banner

After upgrading my Wordpress to version 3.5.1, I noticed a problem on my site where the Submenus are loading but quickly getting hidden behind the Banner. Despite trying various modifications in the CSS file, the issue remains unresolved (and might have wo ...

Utilizing jQuery to dynamically pass parameters to the YouTube API

When making a request to the YouTube API, I am structuring it as follows: $.get("https://www.googleapis.com/youtube/v3/channels", { part: "contentDetails", id: "somestring", key: "MY-API-KEY" } /*, ...*/ ) A hidden field contains the value id ...

Ways to stop users from inputting incorrect characters into an input field

I need help with restricting user input in a text field to only allow letters, numbers, and dashes (-). For alphanumerics only - "The alphanumeric character set includes numbers from 0 to 9 and letters from A to Z. In the Perl programming language, the un ...

"How to ensure a background image fits perfectly on the screen in IE10

I am facing a problem while trying to set the background image in CSS to fit the screen. It works fine with the latest version of Chrome, but there is an issue with IE 10. html { border-top: 10px solid #8A85A5; background: url("http://www.lifeintempe.com/ ...