Collapsible Widgets






Small Feature, Excellent Idea
Collapsible Widgets are a simple feature that can easily make a site or application exciting. It could just be the simple thing that adds that wow effect to your project. You can hardly find any of my work where this isn't implemented. All these projects; asset.bz, a1school.net, buysellairtime.com and many others proudly fly the banners of this wonderful feature. Collapsible widgets also provide an avenue for front-end flexibility and space management

Implementation
I won't bore you with any more rants, lets get down to how you can get this implemented. First you add two html elements, an header and a div  like we have below

 <div class="panel">  
   <header class="panel-heading">  
    Widget Header Text  
   </header>  
   <div class="panel-body">  
   </div>  
 </div>  

In order to make things a little more exciting, you may also want to include the following in your CSS:


 .panel-heading{  
      background-color: rgba(0, 0, 0, 0);  
      border-bottom-color: rgba(0, 0, 0, 0.2);  
      border-bottom-style: dotted;  
      border-bottom-width: 1px;  
      border-top-left-radius: 3px;  
      border-top-right-radius: 3px;  
      box-sizing: border-box;  
      color: rgb(66, 79, 99);  
      cursor: pointer;  
      display: block;  
      font-family: 'Open Sans', sans-serif;  
      font-size: 12px;  
      font-weight: bold;  
      height: 51px;  
      line-height: 20px;  
      padding-bottom: 15px;  
      padding-left: 15px;  
      padding-right: 15px;  
      padding-top: 15px;  
      text-transform: uppercase;  
 }  
 .panel-body{  
      box-sizing: border-box;  
      color: rgb(122, 118, 118);  
      display: block;  
      font-family: 'Open Sans', sans-serif;  
      font-size: 14px;  
      height: 316px;  
      line-height: 20px;  
      padding-bottom: 15px;  
      padding-left: 0px;  
      padding-right: 0px;  
      padding-top: 15px;  
 }  
 .panel{  
      background-color: rgb(255, 255, 255);  
      box-shadow: rgba(0, 0, 0, 0.2) 0px 2px 3px 0px;  
      box-sizing: border-box;  
      color: rgb(122, 118, 118);  
      display: block;  
      font-family: 'Open Sans', sans-serif;  
      font-size: 14px;  
      height: 367px;  
      line-height: 20px;  
      margin-bottom: 20px;  
 }  

So far so good. Now JQuery will be cake icing. Before you include the code below, be sure you have a reference to JQuery in your project. In your document ready block, add the following code:


 $('header.panel-heading').each(function () {  
     $(this).click(function () {  
       if ($(this).hasClass('up')) {  
         $(this).next('.panel-body').slideDown();  
         $(this).removeClass('up');  
       }  
       else {  
         $(this).next('.panel-body').slideUp();  
         $(this).addClass('up');  
       }  
     });  
 });  

And that will be all. Test your page now, and be sure everything works fine. If you encounter any challenges, please drop your comments below, I will try my best to help you fix it.

Cheers!

Comments

Popular posts from this blog

Resize or Crop Image before Upload Using HTML5 File API

Get Creative With Data Tables: Row Click Events

Exception from HRESULT: 0x80131040