Learn how to assign an image to a div element when it is collapsed, and then reveal the content when clicked on to expand

I am working on a three div with expand collapse functionality. When I expand one div, I want to set some image to the other divs. And when I go back to normal mode, I need the original content that was inside each div.

$("a.expansion-btn").click(function (){
  classes = this.className;
  var divNumber = classes.slice(-1);
   var toGetId = "#div-"+divNumber;
  if ($(toGetId).hasClass("expanded-div")){
   $(".normal-div").removeClass("compressed-div");
   $(".normal-div").removeClass("expanded-div");
  }
  else{
   $(".normal-div").removeClass("compressed-div");
   $(".normal-div").removeClass("expanded-div");
   $(".normal-div").addClass("compressed-div");
   $(toGetId).removeClass("compressed-div");
   $(toGetId).addClass("expanded-div");    
  }  
});
*{
  box-sizing:border-box;
}
.contenth1{
  height:20%;
}
.contenth2{
  height:70%;
}
.container{
  margin:0;
  padding:0;
  width:100%;
  height:400px;
}
.normal-div{
  width:33.33%;
  height:100%;
  position:relative;
  border:2px solid black;
  float:left;
}
.expanded-div{
    width:80%;
}
.compressed-div{
  width:10%;
}
#div-1{
   
}
#div-2{
   
}
#div-3{
  
}
a.expansion-btn{
  position:absolute;
  top:10px;
  right:10px;
  font-weight:bold;
  cursor:pointer;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
  <div class="normal-div" id="div-1">
    <div class="contenth1">
      <a class="expansion-btn exp-1">click</a>
    </div >
    <div class="contenth2">one</div>
    </div>
  <div class="normal-div" id="div-2">
  <div class="contenth1">
      <a class="expansion-btn exp-2">click</a>
    </div>
    <div class="contenth2">two</div>
    </div>
  <div class="normal-div" id="div-3">
    <div class="contenth1">
      <a class="expansion-btn exp-3">click</a>
    </div>
    <div class="contenth2">three</div>
    </div>
  </div>

Example:- In normal Mode Works Fine https://i.sstatic.net/os2ou.png

Example 1:- While Expand div other two div I need to hide the content and show some glyphicons something like this image https://i.sstatic.net/iICLH.png

Answer №1

To enhance the appearance, simply incorporate a background image into the .compressed-div class, and ensure that .compressed-div .contenth2 has a display:none property.

I have also optimized the selectors for selecting the current parent element, eliminating the need to depend on id and counter values.

$("a.expansion-btn").click(function (){
  var thisDiv = $(this).closest('.normal-div');
  if (thisDiv.hasClass("expanded-div")){
   $(".normal-div").removeClass("compressed-div");
   $(".normal-div").removeClass("expanded-div");
  }
  else{
   $(".normal-div").removeClass("compressed-div");
   $(".normal-div").removeClass("expanded-div");
   $(".normal-div").addClass("compressed-div");
   $(thisDiv).removeClass("compressed-div");
   $(thisDiv).addClass("expanded-div");    
  }  
});
*{
  box-sizing:border-box;
}
.contenth1{
  height:20%;
}
.contenth2{
  height:70%;
}
.container{
  margin:0;
  padding:0;
  width:100%;
  height:400px;
}
.normal-div{
  width:33.33%;
  height:100%;
  position:relative;
  border:2px solid black;
  float:left;
}
.expanded-div{
    width:80%;
}
.compressed-div{
  width:10%;
  background:url("https://unique-background-image-url.com");
}
.compressed-div .contenth2{
 display:none;
}
#div-1{
   
}
#div-2{
   
}
#div-3{
  
}
a.expansion-btn{
  position:absolute;
  top:10px;
  right:10px;
  font-weight:bold;
  cursor:pointer;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
  <div class="normal-div" id="div-1">
    <div class="contenth1">
      <a class="expansion-btn exp-1">click</a>
    </div >
    <div class="contenth2">one</div>
    </div>
  <div class="normal-div" id="div-2">
  <div class="contenth1">
      <a class="expansion-btn exp-2">click</a>
    </div>
    <div class="contenth2">two</div>
    </div>
  <div class="normal-div" id="div-3">
    <div class="contenth1">
      <a class="expansion-btn exp-3">click</a>
    </div>
    <div class="contenth2">three</div>
    </div>
  </div>

Answer №2

Check this out! I included a gif just for fun, but feel free to switch it out with a regular image.

If you're not a fan of repetitive backgrounds, simply add background-repeat:no-repeat; to the .coverUp class.

If you want the content to be visible within those compressed divs, try removing the z-index from the .coverUp class.

$("a.expansion-btn").click(function (){
  classes = this.className;
  var divNumber = classes.slice(-1);
   var toGetId = "#div-"+divNumber;
  if ($(toGetId).hasClass("expanded-div")){
   $(".normal-div").removeClass("compressed-div expanded-div");
  }
  else{
   $(".normal-div").removeClass("compressed-div expanded-div").addClass("compressed-div");
   $(toGetId).removeClass("compressed-div").addClass("expanded-div");    
  }  
});
*{
  box-sizing:border-box;
}
.contenth1{
  height:20%;
}
.contenth2{
  height:70%;
}
.container{
  margin:0;
  padding:0;
  width:100%;
  height:400px;
}
.normal-div{
  width:33.33%;
  height:100%;
  position:relative;
  border:2px solid black;
  float:left;
}
.expanded-div{
    width:80%;
}
.compressed-div{
  width:10%;
}
.coverUp{
  position:absolute;
  width:100%;
  height:100%;
  top:0;
  background-color:white;
  background-image:url('http://www.downgraf.com/wp-content/uploads/2014/09/01-progress.gif');
  background-position:center;
  background-size:contain;
  left:0;
  display:none;
  z-index:-1;
}
.compressed-div .coverUp{
  display:block;
}

#div-1{
   
}
#div-2{
   
}
#div-3{
  
}
a.expansion-btn{
  position:absolute;
  top:10px;
  right:10px;
  font-weight:bold;
  cursor:pointer;
  z-index:999;
}
.compressed-div .contenth2{
  display:none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
  <div class="normal-div" id="div-1">
    <div class="coverUp"></div>
    <div class="contenth1">
      <a class="expansion-btn exp-1">click</a>
    </div >
    <div class="contenth2">one</div>
  </div>
  <div class="normal-div" id="div-2">
    <div class="coverUp"></div>
  <div class="contenth1">
      <a class="expansion-btn exp-2">click</a>
    </div>
    see it?
    <div class="contenth2">two</div>
    </div>
  <div class="normal-div" id="div-3">
    <div class="coverUp"></div>
    <div class="contenth1">
      <a class="expansion-btn exp-3">click</a>
    </div>
    <div class="contenth2">three</div>
    </div>
  </div>

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

Error: The CommentsSection function did not return any values after rendering

I've been working on a project to create a simple web page that features multiple Material UI card components and integrates Redux to simulate a basic social media platform. The main issue I'm encountering is that when I try to expand a card, an ...

Utilizing Axios to filter response.data in an API request

Looking to retrieve the latest 8 products based on their date? Take a look at the code snippet below: const newestProducts= []; axios.get("http://localhost:3003/products").then(response => { let products = response.data.sort(fu ...

Ways to toggle the visibility of a div with a click event?

I am working on a project where I have a list view of items. I want to implement a feature where when a user clicks on an item, a hidden div below it will appear with options like price, quantity, and other details. I am looking for guidance on how to achi ...

Having trouble with custom padding for b-sidebar in VueJS?

I am struggling with adding padding to the sidebar using bootstrap-vue. When I attempt to add padding in the browser devtools, it works perfectly fine. However, when trying to implement the same code, it doesn't reflect on the actual webpage. I tried ...

jQuery/JSON Error: Unexpected character encountered during parsing

I am currently working on MVC4 and I am attempting to transfer data from a view to a controller using JQuery and JSON. The objective is to extract the values of checkboxes within a grid. Here is the code snippet: <script type="text/javascript"> func ...

Utilize jQuery to load external content into a div element while also optimizing for search engines

Seeking a solution to load remote content into a div element using jQuery, I found a script that works perfectly. The content displays correctly, but the issue arises when viewing the page source in the browser - it shows the Javascript code instead of th ...

What is the best way to terminate a file upload initiated by ajaxSubmit() in jQuery?

A snippet of code I currently have is: UploadWidget.prototype.setup_form_handling = function() { var _upload_widget = this; $('form#uploader') .unbind('trigger-submit-form') // Possibly a custom method by our company . ...

The initial value for the `useState` is not defined at the

Here's a simplified version of my requirement that demonstrates the issue. The ColorBox component receives a prop called "isVisible" from the ShowColorComponent component, which is used to initially set the state of the ColorBox.visible variable. impo ...

Filter out the truthy values in an array with JavaScript

I am currently working with a javascript array called 'foo' which looks like this: var foo = [false,false,true,false,true]; My goal is to eliminate all the 'true' values and only keep the 'false' ones, resulting in this arra ...

What is the best way to include a JavaScript variable in an HTML image src tag?

Even though I know the answer to this question is out there somewhere, I just can't seem to find it (or maybe I'm not recognizing it when I see it!). As a beginner in jquery, please be patient with me. Dilemma: I have a collection of 20 images, ...

Select an item by populating it with JQuery

Here is the select HTML code I am working with: <div class="form-group col-lg-6 row"> {{ Form::select( 'state',array( 0 => '' ),null, array( 'class' => 'form-control', 'data-placeholder' =&g ...

In JavaScript, use a regular expression to replace all instances of %2F with %

Looking for a JavaScript/regex solution to transform %2F into %21. This will allow me to successfully pass forward slashes through a GET parameter after using encodeURIComponent() on a URL. Once the data reaches the server, I'll convert back from ! ...

Regular expression patterns for authenticating and verifying passwords

Currently, I am working on a JavaScript program to validate passwords using regex. Here are the requirements: The password must consist of at least seven characters. It should include at least one of the following: an uppercase letter (A-Z) a lowercas ...

The Material UI export of 'useInsertionEffect' (imported as 'useInsertionEffect$1') was not located within the 'react' library while utilizing the Appbar component

https://i.sstatic.net/twJwh.png 1: https://i.sstatic.net/Y2Zdo.png**strong text**https://i.sstatic.net/twJwh.png While attempting to implement the Appbar component from Material UI, I encountered an unexpected error. ...

How can I create 3 conditions in an AJAX request to verify a user's ID?

I am trying to create 3 conditions in Ajax to check when creating a new user id. It works for 2 conditions (username can be used and username is already in use), but I encounter a problem when the user leaves the username field empty, and it still displays ...

Exploring the connections between nested AngularJS controllers: Techniques for enabling communication between different controllers in the same application

Viewing my html code: <div id="chatContainer"> <div id="users" ng-controller="usersController"> <div ng-repeat="user in users" class="userItem">{{user.username}}</div> </div> <div id="chatText" ng-cont ...

Is it possible to automatically redirect to a different URL if the server is running slow when clicking?

Is it possible to utilize Javascript, AJAX, or another client-side technology to automatically redirect the user to a different URL if the initial URL is slow to respond when clicked on? For example, if a link redirects to URL1 and there is no response fr ...

The error message displayed in app.js is: TypeError - it is not possible to call the 'query' method on null

Recently, I started working with node.js and encountered an issue while trying to load CSV file values into a database. The error message displayed is as follows: Error: at var stream = client.query(copyFrom('COPY menu_list FROM STDIN')); T ...

Tips for reconnecting tinymce to a textarea following an ajax refresh

Whenever I submit a form with a tinymce textarea, the containing div gets reloaded using the following code: $('body').on('submit', '.comment_form', function (e) { e.preventDefault(); tinyMCE.triggerSave(); ...

Using mobile devices for multipart forms: managing actions that appear upon selecting a file input

My website features a multipart form where users are required to upload images. <form method="POST" encType="multipart/form-data" action="/uploads" id="forminho"> <div className="btn btn-success" id="submitFileBtn"> Select Image ...