Share it

Bookmark and Share

Translate

Thursday, February 26, 2015

0 List Grid View using jQuery


List Grid View using jQuery


In almost every e-commerce website that you are designing, the feature to switch between list and grid view is very essential to facilitate better display of the huge product catalog. This is very simple to achieve using jQuery, as you can visualize the very minimal version in the jsfiddle showcased here.

To make this more complete, I experimented showcasing product catalog with fade-in and fade-out transitions while switching between grid and list views. In this post, let us see how to create fading list/grid view switcher with components to display product catalog using jQuery and css.



1. First let us prepare the content to be dynamically styled according to the view selected. I have used font-awesome icons for displaying list and gridview switch controls in the demo and it is absolutely optional, you can choose to use buttons or links. So first let us place the switching controls inside body tag.

1<div id="viewcontrols">
2       <a class="gridview">
3         <i class="fa fa-th fa-2x"></i>Grid
4       </a>
5       <a class="listview active">
6         <i class="fa fa-list fa-2x">List</i>
7       </a>
8</div>


Next let us add an unordered list containing product details. Each list-item represents one product and I have displayed only one list below, since it is the same for all products. In the demonstration, I have used eight products, that means I have used the list-item eight times.
01<ul class="list">
02    <li>
03        <img src="images/barbie_8.jpg" height="250px" width="250px"/>
04 <section class="list-left">
05 <span class="title">PRODUCT NAME</span>
06 <p>Product description goes here. Aliquam tincidunt diam varius
07                 ultricies auctor. Vivamus faucibus risus tempus,
08                 adipiscing justo
09        </p>
10 <div class="icon-group-btn">          
11  <a title="Add to Cart" href="javascript:void(0);" class="btn-cart">
12  <span class="icon-cart"></span>
13  <span class="icon-cart-text">
14   Add To Cart     
15  </span>
16  </a>
17  <a title="Add to wishlist" href="#" class="btn-wishlist">
18  <span class="icon-wishlist"></span>
19  <span class="icon-wishlist-text">
20   Add To Wishlist     
21  </span>
22  </a>
23  <a title="Add to Compare" href="#" class="btn-compare">
24  <span class="icon-compare"></span>
25  <span class="compare-text">
26   Add To Compare     
27  </span>
28  </a>
29 </div
30 </section>
31 <section class="list-right">
32  <span class="price">$50</span>
33  <span class="detail"><a class="button">Details</a></span>
34 </section>
35 </li>
36</ul>

2. Next we need two different css styles for displaying list and grid views. I have not displayed miscellaneous css styles here that are not mandatory to achieve list and grid layouts. However if you like to design a product catalog exactly like what is shown in the demonstration you can download this project and use it.

LIST LAYOUT

01ul.list{
02width: 800px;
03margin-right: 15%;
04margin-left: 15%;
05}
06ul.list li {
07background: #F7F7F7;
08border-bottom: 1px dotted #CCC;
09margin-bottom: 50px;
10padding-right: 20px;
11height: 250px;
12}
13ul.list li img{
14float: left;
15margin-right: 10px;
16padding-bottom: 0;
17border-bottom: 2px solid transparent;
18}
19ul.list li p{
20overflow:hidden;
21word-wrap:break-word;
22width:250px;
23}
24ul.list li .title{
25overflow:hidden;
26word-wrap:break-word;
27font-size:24px;
28}
29ul.list li .list-left{
30width:300px;
31position:absolute;
32padding-left:260px;
33}
34ul.list li .list-right{
35width:150px;
36float:right;
37}
38ul.list li .price{
39float:right;
40font-size:56px;
41display: block;
42margin-right: 10px;
43text-align: right;
44width:100%;
45color:#4FAFC2;
46}
47ul.list li .detail{
48float:right;
49display:block;
50}

GRID LAYOUT

01ul.grid li { float: left; width: 265px; height: 440px; border-right: 1px dotted #CCC; border-bottom: 1px dotted #CCC;margin:20px; background:#F7F7F7; }
02ul.grid li img{text-align:center;width:100%;}
03ul.grid li p{display:none;}
04ul.grid li .list-left{text-align:left;font-size:24px;margin-left: 10px;margin-top:10px;}
05ul.grid li .icon-group-btn{font-size:14px;}
06ul.grid li .list-right{display:block;width:100px;margin-left: 10px;}
07ul.grid li .list-right .price{font-size:24px;display:block;color:#4FAFC2;}
08ul.grid{
09width: 950px;
10margin-left: 10%;
11}


3. Finally let use jQuery to switch between grid and listviews with fade-in and fade-out effect.

01<script type="text/javascript">
02$(document).ready(function () {   
03var elem=$('ul');     
04$('#viewcontrols a').on('click',function(e) {
05 if ($(this).hasClass('gridview')) {
06  elem.fadeOut(1000, function () {
07  elem.removeClass('list').addClass('grid');
08  elem.fadeIn(1000);
09         });     
10 }
11 else if($(this).hasClass('listview')) {
12  elem.fadeOut(1000, function () {
13  elem.removeClass('grid').addClass('list');
14  elem.fadeIn(1000);
15        });        
16 }
17});
18});
19</script>

That is all. Do not forget to include jQuery library reference inside head tag of your html. Enjoy the demo and download the project to see how other components inside every list-item is styled.

0 comments:

Post a Comment

Thanks for your valuable Comment

 

TechnoTipworld- Tips,Tricks,Technology Copyright © 2011 - |- Template created by O Pregador - |- Powered by Blogger Templates