Ways to transition between divs using clickable buttons

I have a coding challenge where I need to implement two panels. The lower panel consists of three buttons and upon clicking each button, different data should load in the upper panel.

For example, when button 1 is clicked, divs 1.1, 1.2, and 1.3 should slide in. Similarly, when button two is pressed, divs 2.1, 2.2, 2.3, and 2.4 should be displayed.

If you want to see the setup code, check out this DEMO.

.panel {
  width: 300px;
  height: 100px;
  border: 1px solid black;
}
.panel > .panelTop {
  width: 100%;
  height: 49px;
  border-bottom: 1px dashed black;
}
.panel > .panelBottom {
  width: 100%;
  height: 50px;
}
.panel > .panelTop > div {
  width: 24px;
  height: 24px;
  float: right;
  margin: 13px 9px 0 0;
  border: 1px dashed black;
}
.panel > .panelBottom > div {
  width: 32px;
  height: 32px;
  float: right;
  margin: 9px 9px 0 0;
  border: 1px solid black;
  text-align: center;
  font-size: 22px;
}
<div class="panel">
  <div class="panelTop">
    Dependant Buttons:
    <!-- Side1 -->
    <div>1.1</div>
    <div>1.2</div>
    <div>1.3</div>

    <!-- Side2
    <div>2.1</div>
    <div>2.2</div>
    <div>2.3</div> -->

    <!-- Side3 
    <div>3.1</div>
    <div>3.2</div>
    <div>3.3</div> -->
  </div>
  <div class="panelBottom">
    Main Buttons:
    <div class="btn1">1</div>
    <div class="btn2">2</div>
    <div class="btn3">3</div>
  </div>
</div>

Each div will have its unique classes, and I'm considering using arrays in jQuery or setting visibility to hidden in CSS to achieve this functionality. Do you have any other ideas?

Answer №1

Do you require something similar to this?

UPDATE: Fiddle

*All that is needed is CSS "manipulation" (no JavaScript required).

This is what I have written:

html

<div class="panel">
  <div class="panelTop">
    Dependent Buttons:
    <div>
      <input id="box-1" name="trigger" type="radio">
      <div>1.1</div>
      <div>1.2</div>
      <div>1.3</div>
    </div>
    <div>
      <input id="box-2" name="trigger" type="radio">
      <div>2.1</div>
      <div>2.2</div>
      <div>2.3</div>
    </div>
    <div>
      <input id="box-3" name="trigger" type="radio">
      <div>3.1</div>
      <div>3.2</div>
      <div>3.3</div>
    </div>
  </div>
  <div class="panelBottom">
    Main Buttons:
    <label for="box-1" class="btn1">1</label>
    <label for="box-2" class="btn2">2</label>
    <label for="box-3" class="btn3">3</label>
  </div>
</div>

css

input[type="radio"]{
  position: absolute;
  opacity: 0;
}

.panelTop > div {
  position: absolute;
  top: 15px;
  right: 0;
}

.panelTop > div div {
  opacity: 0;
  transition: ease .3s;
  transform: translate(-150%, 0)
}

.panelTop > div input[type="radio"]:checked ~ div {
   opacity: 1;
   transform: translate(0%, 0)
}

.panel {
  width: 400px;
  height: 100px;
  border: 1px solid black;
}

.panel > .panelTop {
  position: relative;
  width: 100%;
  height: 49px;
  border-bottom: 1px dashed black;
}

.panel > .panelBottom {
  width: 100%;
  height: 50px;
}

.panel > .panelTop > div div {
  width: 24px;
  height: 24px;
  float: right;
  margin: 0px 9px 0 0;
  border: 1px dashed black;
}

.panel > .panelBottom > div {
  width: 32px;
  height: 32px;
  float: right;
  margin: 9px 9px 0 0;
  border: 1px solid black;
  text-align: center;
  font-size: 22px;
}

Notice that each label tag has a for attribute with the id of your radio input type.

The label tags "trigger" your radio inputs and with CSS selectors we can change the divs after them.

.panelTop > div input[type="radio"]:checked ~ div

This CSS selector represents what happens after the click, so you can customize it as needed.

input[type="radio"]:checked ~ div
- selects every div after a checked radio input type.

I trust this example aids in your understanding.

Answer №2

I made some updates to the code originally written by @zeev katz.

Check out the updated version on JSFiddle

$(document).ready(function() {
  $("input").click(function() {
  $(this).parent().siblings().find("input").prop("checked", false);l
});

Make sure to review it and let me know what you think. Hopefully, you'll like the changes :)

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

Tips for configuring page-specific variables in Adobe DTM

Although I have integrated Adobe Analytics for tracking on my website, I am facing difficulty in properly defining the variables. This is how I attempted to define the variable: var z = new Object(); z.abc = true; z.def.ghi = true Despite following the ...

Show a dynamic highchart graph displaying linear data retrieved from the database

I am attempting to present data retrieved from a database in a linear highchart format. Here is the JSON response from my database: [{"protocol":"tcp","date":"01/02/20","time":"00:10:20","total":281}, {"protocol":"udp","date":"01/02/20","time":"00:10:30", ...

Having trouble implementing a transition on a dropdown menu in React

Can anyone help me troubleshoot an issue with adding a transition to a select box that appears when clicking on an input field arrow? I have tried implementing a CSS transition property, but it doesn't seem to be working. Any suggestions on what might ...

Incorporate a platform-specific button in a user interface with React Native

I need to display a button that is only shown on iOS, not on android. I have tried using the following style, but it doesn't seem to be working. ...Platform.select({ android: { display:'none', }, The code for my button is: <Bu ...

Floating action buttons in CSS

I'm trying to create a button that reveals additional options when hovered over. I've been looking for a method similar to what's shown in the image below, but haven't been successful. This needs to be implemented on a web page using HT ...

Issue encountered while attempting to set up react-native project

Whenever I attempt to create a new react-native project using the command: npx react-native init myproject I encounter the following errors: ERESOLVE is overriding peer dependency npm gives me a warning while trying to resolve: [email protected] ...

Is it possible for a Vue data property to have a value that is determined by another Vue data property object?

Within my HTML form, I have implemented the flatPickr (calendar picker) component which generates an input field. I am currently exploring how to dynamically change the class of the input field if the error class function returns true. Below is the flatPi ...

Tracking progress across various iterations of an ongoing ajax process

I need to make multiple ajax requests to call an API multiple times. How can I display a progress indicator for these ajax calls? The progress indicator needs to complete once all the requests are finished and the page is ready. I have tried using ajaxStar ...

Issue encountered with AJAX multiple image uploader

I'm attempting to create an PHP and JavaScript (jQuery using $.ajax) image uploader. HTML: <form method="post" action="php/inc.upload.php" id="upload-form" enctype="multipart/form-data"> <input type="file" id="file-input" name="file[]" a ...

Activate a function when the v-data-table in Vuetify is expanded and then collapsed

While trying to create a v-data-table, I came across some odd behavior when monitoring the expanded.sync value. The first layer's expanded.sync value seems normal, but the second layer's expanded.sync has three consecutive identical records. I w ...

How to message someone privately in a public Discord channel using discord.js

Can someone help me figure out how to create a message in discord.js version 12.5.3 that only I can see? I know how to send messages to channels using message.channel.send, but I'm not sure how to make a message visible only to myself. Thank you! ...

Exploring Django's AJAX suggestions

I successfully created a functional website using Django, but I am facing a challenge with two separate forms that are currently on different pages. Since these forms are central to the web application's activities, I would like to combine them onto t ...

display the HTML form when the page is clicked

Trying to implement an onclick method for selecting form types. I have multiple form types available - example here Clicking on one submenu should open up the desired form type directly below the menu How can I dynamically load a form based on the select ...

Utilizing PHP & AJAX to dynamically update JSON files

In the process of creating a game that requires registration for use, I am attempting to add the username and password inputted into a form to my JSON file. The current structure of the JSON file is as follows: { "LogIns":[ { "Username":"mike ...

What is the best way to store data retrieved from an asynchronous database callback in Nodejs with express?

After querying my sqlite3 database to verify user details, I encounter an issue when trying to store the retrieved data in a session variable or global instance. Despite being able to access the data within the callback function, it becomes undefined once ...

Elements overlapped with varying opacities and responsive to mouse hovering

In this Q/A session, we will explore a JS solution for managing the opacity of overlapping elements consistently during hover. Objective Our goal is to create two transparent and overlapping elements, similar to the red boxes showcased below. These eleme ...

What is the process for adjusting the color of the browser tab?

My chat application is running smoothly, but a client has requested the ability to highlight or make the browser tab blink when they receive a visitor call. This will help them respond promptly to incoming inquiries. Does anyone know how to implement thi ...

Utilize the assigned value of a variable from a separate file

Is it possible to set a variable in an external file called "Variable.js", assign it a value in a file named "Page1.html", and then use that variable with its assigned value in another file named "Page2.html"? For example, let's consider the contents ...

Retrieve data from a MySQL database to a mobile application using jQuery by utilizing PHP with jQuery/AJAX

I've come across countless tutorials, but I find them to be quite confusing. I struggle to grasp how to utilize the information from these tutorials to achieve the specific outcome I desire. For instance, I have an icon on my homepage. My goal is for ...

Utilizing the dynamic pairing of Three.js and CSS3 to bring life to animated 3D objects

I am currently working on a project that involves creating 3D Lego elements and assembling them into a figure using animation. I have successfully modeled the elements in Blender and exported them as .obj/mlt files without any issues. However, when it come ...