Adding toggle effect in HTML without using any external libraries or scripts is relatively simple. Here

How can I implement a dropdown toggle effect on my website using the code above?

I want the first box to display the dropdown content by default when the page is refreshed. When another box is clicked, the dropdown content of the previous box should hide itself.

Any suggestions on how to achieve this?

.maindrop {
    width: 49%;
}
.bar {
    padding: 20px;
    color: white;
    background: #1FB5AC;
    display: block;
    font-family: Times;
    text-decoration: none;
    font-size: 16px;
    transition: .2s ease-out;
    margin-bottom: .1cm;
}
.bar:hover {
    background: gray;
}
.dropbox {
    max-height: 0;
    transition: .5s ease-out;
    overflow: hidden;
    width: 100%;
}
.dropbox:target {
    max-height: 300px;
}
<div class="maindrop" style="float: right">
        <div class="fold default">
            <a class="bar" href="#tab5">Speaker 5</a>
            <div class="dropbox" id='tab5'>
                Insert your content here
            </div>
        </div>
        <div class="fold">
            <a class="bar" href="#tab6"> Speaker 6</a>
            <div class="dropbox" id='tab6'>
                Insert your content here
            </div>
        </div>
        <div class="fold">
            <a class="bar" href="#tab7"> Speaker 7</a>
            <div class="dropbox" id='tab7'>
                Insert your content here
            </div>
        </div>
        <div class="fold">
            <a class="bar" href="#tab8"> Speaker 8</a>
            <div class="dropbox" id='tab8'>
                Insert your content here
            </div>
        </div>
    </div>
    
 

Answer №1

I have modified Shivkumar's response to incorporate the necessary requirements and designs with the assistance of PaulOB. The code now supports two columns effectively.

The advantages of this edit include:

  1. Two column mode
  2. Clicking on the heading background triggers a toggle effect
  3. Only one dropdown box will be visible at a time

function toggleChevron(e) {
      var theAccordion = $('#accordion');
  $(e.target)
        .prev('.panel-heading')
        .find('i.indicator')
        .toggleClass('glyphicon-minus glyphicon-plus');
        
theAccordion.find('.panel-heading').removeClass('highlight');
$(e.target).prev('.panel-heading').addClass('highlight');
}
$('#accordion').on('hidden.bs.collapse', toggleChevron);
$('#accordion').on('shown.bs.collapse', toggleChevron);
$('#accordion').on('show.bs.collapse', function () {
    $('#accordion .in').collapse('hide');
});
 .panel-heading .accordion-toggle:after {
/* symbol for "opening" panels */
    font-family: 'Glyphicons Halflings';  
content: "\e113";    
float: right;        
color: white;         
}
.panel-heading .accordion-toggle.collapsed:after {
/* symbol for "collapsed" panels */   
    content: "\e114";    
    color: white;
}
.accordion-toggle:hover {
text-decoration: none;
}
.panel .panel-heading {
font-weight: bold;
font-size: 20px;
font-family: verdana;
border-radius:0;
padding:0;
}
.panel .panel-heading a{
display:block;
background: #1FB5AF;
color: white;
padding:10px 15px;
border-radius:3px;
text-decoration:none;
}
.panel .panel-heading a:hover {
background: grey;
color: white;
text-decoration:none;
}
#accordion .highlight a{
background:#1FB5AC;
box-shadow:0 0 10px rgba(0,0,0,0.3);
color:white;
transition:background 2s ease;
}
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
...
...

Answer №2

If you're looking for a way to create collapsible content, consider using Bootstrap's accordion component. Here is an example of how you can implement it:

<!DOCTYPE html>
<html>
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>

<div class="container">
  <h2>Accordion Example</h2>
  <p><strong>Note:</strong> The <strong>data-parent</strong> attribute makes sure that all collapsible elements under the specified parent will be closed when one of the collapsible item is shown.</p>
  <div class="panel-group" id="accordion">
    <div class="panel panel-default">
      <div class="panel-heading">
        <h4 class="panel-title">
          <a data-toggle="collapse" data-parent="#accordion" href="#collapse1">Collapsible Group 1</a>
        </h4>
      </div>
      <div id="collapse1" class="panel-collapse collapse in">
        <div class="panel-body">Lorem ipsum dolor sit amet, consectetur adipisicing elit,
        sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
        quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</div>
      </div>
    </div>
    <div class="panel panel-default">
      <div class="panel-heading">
        <h4 class="panel-title">
          <a data-toggle="collapse" data-parent="#accordion" href="#collapse2">Collapsible Group 2</a>
        </h4>
      </div>
      <div id="collapse2" class="panel-collapse collapse">
        <div class="panel-body">Lorem ipsum dolor sit amet, consectetur adipisicing elit,
        sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
        quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</div>
      </div>
    </div>
    <div class="panel panel-default">
      <div class="panel-heading">
        <h4 class="panel-title">
          <a data-toggle="collapse" data-parent="#accordion" href="#collapse3">Collapsible Group 3</a>
        </h4>
      </div>
      <div id="collapse3" class="panel-collapse collapse">
        <div class="panel-body">Lorem ipsum dolor sit amet, consectetur adipisicing elit,
        sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
        quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</div>
      </div>
    </div>
  </div> 
</div>
    
</body>
</html>

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

Using JavaScript to retrieve the text value of a custom button

Recently, I encountered a peculiar button in my code: <button type="button" id="ext-gen26" class=" x-btn-text">button text here</button> Despite its unique appearance, I am struggling to locate it based on the tex ...

The page scroll disappears once the jQuery dialog is closed

Here is the code I use before closing the dialog... $('body').css('overflow','hidden'); $('.ui-widget-overlay').css('width','100%'); $('#content').removeClass('lightbox_bg' ...

Graphics distorting on android devices when implementing createjs and canvas technology

I have a question regarding the performance of my game that I'm developing to work on both desktop browsers and mobile devices. Using HTML5 and CreateJs, most of the game is being drawn on a canvas element. While everything runs smoothly on a standar ...

PHP loaded HTML does not allow JavaScript to execute

My system includes an announcements feature where all announcements are retrieved from a database and displayed on the screen using Ajax and PHP. Below is the code snippet used to load each announcement onto the page: echo '<div id="announcements ...

Manage the orientation of the text for entering dates

I need to customize a textfield to enter dates in the format of yyyy/MM/dd. The input direction is set to rtl. However, when I enter the day, then month, and then year, the final displayed values show up as dd/MM/yyyy, as shown in the image attached: My g ...

Can you explain the purpose of :not(style) ~ :not(style) in CSS?

Upon investigating, I found that Mui automatically included a :not(style) ~ :not(style) selector which required the use of !important in my sx to override. .css-1rb8ayf-MuiStack-root > :not(style) ~ :not(style) { margin-top: 40px; } .css-d13d9m-MuiSta ...

Employing Modernizr along with jQuery for Animation in Cases when CSS3 Transitions are Absent

Can Modernizr and jQuery be combined to create a transition effect similar to CSS3 if it's not supported? My current approach looks like this... <div class="hoverable"> <p>This div changes both width and height on hover</p> </di ...

Circular CSS menu

What I'm Trying to Achieve: I am interested in developing a unique radial menu that includes interactive elements, such as the central image and the surrounding sections. It is imperative that the solution is compatible across all browsers. The examp ...

What is the best way to center align a card using Bootstrap 4?

<div class="container"> <div class="row align-items-center h-100"> <div class="col-6 mx-auto"> <div class="card bg-white mx-auto" id="maincard"> <div class="card-body"> ...

Ways to toggle checkboxes and ensure they are clickable outside of the row for better user experience

There should only be one row with the class highlightRowSelected and its checkbox must be checked - this functionality is in place. How can I deselect other checkboxes that do not have the class highlightRowSelected, while allowing other rows to have thei ...

Flexbox layout to achieve equal height columns with content centered

In search of a solution to create two columns with equal height and center-aligned content within each column, while also maintaining different widths. Issue: The challenge lies in achieving both 'equal height' and 'middle aligned' at ...

Is it necessary to trigger a change event only for the specific ID that has been altered, even if there

I need a way to highlight the background of text inputs in green for 2 seconds when there is a successful AJAX request, but only for the specific input field that was changed. I'm struggling to figure this out using jQuery. For example, if I change t ...

Expand the background of columns to cover grid gutters

Imagine this... You're working on a layout with three columns, all within a fixed-sized container that is centered using margin:0 auto. The design requires the first column to have a background color that reaches the left edge of the browser window. ...

Configuration file for Mobile applications - HTML/PhoneGap

Currently working on developing a mobile app with PhoneGap and HTML5. I'm looking to organize customizable settings like server names in a separate file (akin to an App.config or Web.config in an ASP.NET app). I'm aware of HTML5's support f ...

"Enhancing User Experience with AngularJS by Dynamically Modifying and Refresh

I'm currently attempting to dynamically add HTML elements using JavaScript with a directive: document.getElementsByClassName("day-grid")[0].innerHTML = "<div ng-uc-day-event></div>"; or var ele = document.createElement("div"); ele.setAttr ...

Transforming a triangular shape into a square using Plane Geometry in THREE.js

I have an animation created with triangles in the link below. Is there a way to convert these triangular areas into squares? The animation currently has a line running through the middle when using "PlaneGeometry". Removing this line would turn the triangl ...

What is the best way to center three div elements horizontally, each with a different width – one on the left, one in the center, and

I have a parent div containing three child divs with dynamically changing widths. I am trying to align the first div to the left, center the middle one with equal padding on both sides, and align the right one to the right. It may sound simple, but it&apo ...

When the user is actively scrolling in the browser, the button will disappear. However, once the scrolling action is completed, the button will gradually fade

Is there a way to hide the button with the id #stats while the user is scrolling, and then have it fade back in once they finish scrolling? It seems like the code below is not working as expected. Any help would be greatly appreciated! <button id="st ...

Using a variable as a URL parameter in a jQuery ajax request: tips and tricks

$.ajax({ type:"POST", url:"hostname/projfolder/webservice.php?callback=statusReturn&content="+str_table, contentType: "application/json; charset=utf-8", crossDomain:true, dataType:'jsonp', succe ...

Generating dynamic tables using JQuery is not possible

This script is intended to generate a table and convert it into a canvas. Clicking on a cell should change its color to the selected color from the color picker input is retrieved from text boxes and the color picker, and executed upon submitting I need t ...