Dynamic Javascript Accordion Navigation Bar Opening and Closing Motion

I'm currently working on a horizontal accordion dropdown navbar and I'm attempting to make it so that only one section can be expanded at a time. For instance, if subsection A is already expanded and you click on section B, then subsection A should collapse while subsection B expands. At the moment, both can be expanded simultaneously.

Any assistance would be greatly appreciated!

(I also acknowledge that this may not strictly be a dropdown, but I'm unsure if using sideslide would be any less confusing)

$(function () {

  $(".item").on("click", function () {
    $(this)
      .next().toggleClass("active");
  });
  
  $("#body").css("min-height", "100%");

  
});
    

$(document).ready(function() {

  var $overlay = $('<div class="overlay"></div>');
  var $href = $('this').attr('href');
  var $popup = $('.pop-window');

  $('<div class="close">x</div>').appendTo($popup);
  $popup.hide();

  $('a').click(function(e) {
    e.preventDefault();
    $overlay.fadeIn(800).appendTo('body');
    $popup.delay(800).slideDown();
  });

  $overlay.click(function() {
    $overlay.delay(800).fadeOut();
    $popup.slideUp();
  });

  $('.close').click(function() {
    $popup.slideUp();
    $overlay.delay(800).fadeOut();
  });
});
*{
  box-sizing: border-box;
}
body{
  background: #eaeaea;
  color: #333333;
margin: 0 0 100px;
bottom: 1rem;

}

body.background{
  background-image: url("lab3.jpg");
  background-repeat: no-repeat;
  background-attachment: fixed;
  background-position: center; 
  -webkit-background-size: 100% auto;
  -moz-background-size: 100% auto;
  background-size: 100% auto;
  color: ...

html{
overflow-x: hidden;
width:100%;
min-height:100%;
position: relative;
margin:0;


}



.header{
height:100px;
width:110%;
background-color:#3a1f51;
color:white;
display: block;
margin:-10px;
top:-10px;
padding:-10px;
margin-bottom:-20px;
position:relative;
    -webkit-box-shadow: 0 0 5px rgba(0, 0, 0, 0.15);
    -moz-box-shadow: 0 0 5px rgba(0, 0, 0, 0.15);
    box-shadow: 0 0 5px rgba(0, 0, 0, 0.15);
z-index:2;
}

h1{
margin:0;
margin-bottom:10px;
margin-left:40px;
padding:0;
position: absolute;
bottom:0;
color:#d1d1d1;
font-family: Roboto, sans-serif;
letter-spacing: 5px;
font-size:18;
...



...
<!doctype html>
<html>
<head>
  <meta charset="UTF-8">
  <title>Title</title>
  
  
  
      <link rel="stylesheet" href="css/style.css">

  
</head>

<body>
  

 


 


   <div class=header>


   
<h1>Title</h1>
   
   </div>



   
<div class="slidebox">
  <section>
  <div class="item"> header &#8250;</div>
  <div class="info"><a href="https://codepen.io/SideSlaw/pen/vWZVVN" class="bolded2" style="text-decoration: none; color:#6a27b2;">subheader</a>   |   <a href="https://codepen.io/SideSlaw/pen/vWZVVN" class="bolden" style="text-decoration: none; color:#6a27b2;">subheader</a></div>

  <div class="item"> header &#8250;</div>
  <div class="info"><a href="https://codepen.io/SideSlaw/pen/vWZVVN" class="bolden" style="text-decoration: none; color:#6a27b2;">subheader</a>   |   <a href="https://codepen.io/SideSlaw/pen/vWZVVN" class="bolden" style="text-decoration: none; color:#6a27b2;">subheader</a></div>
  
  <a href="https://codepen.io/SideSlaw/pen/vWZVVN" style="text-decoration: none; color:#6a27b2;"><div class="nitem"> header</div></a>
 
 ...

Answer №1

To ensure the toggling functionality works correctly, you must remove the 'active' class from all elements with the class '.info' on each click before adding a new class to the clicked element. However, do not remove the 'current info active' class to maintain the toggle effect:

$(function () {

  $(".item").on("click", function () {
    var $nextInfo = $(this).next();
    $('.info').not($nextInfo).removeClass('active');
    $nextInfo.toggleClass("active");
  });
  
  $("#body").css("min-height", "100%");

  
});
*{
  box-sizing: border-box;
}
body{
  background: #eaeaea;
  color: #333333;
margin: 0 0 100px;
bottom: 1rem;

}

... (CSS code continues)

$("#script").attr("src", "https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">

<html >
<head>
  <meta charset="UTF-8">
  <title>Title</title>
  
      <link rel="stylesheet" href="css/style.css">

  
</head>

<body>
  <head>

<script type="text/javascript">
  <!--
  if (screen.width <= 800) {
window.location = "mobile";
  }
  //-->
</script>



  <meta charset="UTF-8">
  <title>Title</title>
<link href="https://fonts.googleapis.com/css?family=Raleway|Roboto" rel="stylesheet">
  
<link rel="stylesheet" href="css/style.css">

</head>

... (HTML code continues)

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

Vue component lacking Scss styling

One of the components in my project is an Img component, which includes a figure with a nested image element: <figure> <img @dragstart="stopDrag" :src="src" /> </figure> When using this component within another comp ...

How can I turn off a CSS transition for a particular line of code?

Hey there! I'm dealing with a div that has a fixed height, but when hovered over, it expands by a specific amount. I've also managed to make the background color change upon hovering over the div. The issue arises when both the height and backg ...

What could be causing the issue with object-fit not functioning properly within a container with a max

My goal is to insert a video into a container that has a width of 100% and automatically adjusts its height while maintaining the aspect ratio, but with a maximum height set. I want the video to completely fill the container even if some parts are cropped ...

Implement a sub-routine in Office.js Javascript Word add-in that utilizes asynchronous execution context

Lately, I've been exploring the world of developing an Office.js add-in for Word and have found some success. One task that seems to come up frequently in my add-in is searching and replacing text, which needs to be performed by various action buttons ...

Steps to transfer a JavaScript variable to a PHP session

Currently, I am in the process of retrieving the value of a checkbox and assigning it to a PHP session variable. Below is the relevant portion of the code: The checkbox: $html .= '<span><input type="checkbox" id="checkbox' ...

Designing a nested box layout with bootstrap HTML/CSS styling

Can anyone provide guidance on how to create a HTML/CSS layout similar to the image below? I am specifically struggling with designing the silver "Add corkscrew" box, a white box beneath it, and then another silver box nested within. Any suggestions on ho ...

Guide to establishing intricate conditions for TypeORM insertion

When attempting to insert data based on a specific condition, such as if shopId = "shopA", I want to include the shopdetail. In order to achieve this, I have implemented the following business logic, which is somewhat complex. Is there a more ef ...

Avoiding searching for the ID in the database table when using Sails.js, due to the absence of an ID column in the table

I’m a beginner in Sails JS (Node JS based) and encountered an unexpected error: [Error (E_UNKNOWN) Encountered an unexpected error] Details: error: column MyTable.id does not exist The error occurs when I run this code : MyTable.find().then(funct ...

Encountered a node-pre-gyp installation error while running npm install

As I attempted to follow the React in Action book, I encountered an issue with the project repo - book's project repo Upon running npm install on Ubuntu, I encountered the following error: npm ERR! errno 1 npm ERR! <a href="/cdn-cgi/l/email-prote ...

Challenges migrating from Bootstrap 3 to Bootstrap 4

My current challenge involves updating some code that previously worked with a version of Bootstrap 3 and now I am transitioning to version 4.3.1. Specifically, I am working on altering the navbar code, which has undergone significant changes from version ...

Having issues with implementing the Bootstrap form-check-inline feature

I have been attempting to utilize the form-check-inline feature from Bootstrap Forms without success, as it seems to be stacking checkboxes vertically instead of inline: https://i.sstatic.net/paLTC.png This is the code snippet I am working with: <h1& ...

Keep the div element steady as you scroll to the bottom

Currently, I have a collapsible side navigation whose height is unknown, with a div underneath it. The goal is for the div to change its position to 'fixed' when scrolled to its bottom. I have successfully achieved this functionality; however, I ...

Using jQuery to substitute a matched word with the each method

Looking for ways to update the text using different methods? Is this correct? It seems like it's not replacing or finding the text accurately. $(function(){ var style_find = ['tab-1','tab-2','rounded-1','rounde ...

Adding a fresh dependency to the current package.json file

Introduction: I am relatively new to JavaScript and have encountered a basic issue regarding adding dependencies to an existing JavaScript project. Despite double-checking the installation instructions, I seem to have encountered some errors in my terminal ...

calculating the rotation angle from the origin to a vector within a three-dimensional space using three.js

I am working with a vector in three-dimensional space. Is there a method to determine the angle of rotation from the origin to this vector on each axis? For example, if the vector is located at x=6, y=-10, z=20, what angle does the vector make with resp ...

Can you explain the significance of this?

Some may find this question silly, but I'm curious about the meaning behind certain elements in an ASPX page. For example, when there is code like this: <%@ Page Title="[$TITLES_listevents{uneditable}]" Language="C#" MasterPageFile="~/sitebase ...

Sending data from a form to a PHP script via a variable transfer

i have a form structured like this: <form action="del.php" method="post" enctype="multipart/form-data"> Number of field to display:<input type="text" name="limit" /><br /><br /> Top category: <select name="topcat"> <opt ...

What is the process for creating a blinking, overlapping image using Javascript?

I'm trying to achieve a visually appealing effect with this countdown. The goal is to have it count into negative numbers, at which point a skull and crossbones image should become visible and start flashing. To do this, I am utilizing JavaScript func ...

Implement jQuery Styling using .toggleClass on Freshly-Added DOM Elements

I am currently developing a webpage with a unique "notepad" widget, and I have made some adjustments to the interface: The notepad is essentially an aesthetically modified <ul> where each line on the notepad page is represented by its own <li> ...

Issues have been reported regarding the paramMap item consistently returning null when working with Angular 8 routing

I am encountering an issue with Angular 8 where I am trying to fetch some parameters or data from the route but consistently getting empty values. The component resides within a lazy-loaded module called 'message'. app-routing.module.ts: ... { ...