views:

29

answers:

1

How can I transform jQuery carousel to show 3x3 square of images instead of 1x3?

From:

<- |image| |image| |image| ->

To:

   |image| |image| |image|
<- |image| |image| |image| ->
   |image| |image| |image|

My source code is like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>

  <meta http-equiv="content-type" content="text/html; charset=utf-8" />

  <link rel="stylesheet" href="css/style.css" type="text/css" />

  <!--[if IE 6]>
  <link rel="stylesheet" type="text/css" href="css/ie6.css" />
  <![endif]-->

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"&gt;&lt;/script&gt;

<script type="text/javascript">

$.fn.infiniteCarousel = function () {

    function repeat(str, num) {
        return new Array( num + 1 ).join( str );
    }

    return this.each(function () {
        var $wrapper = $('> div', this).css('overflow', 'hidden'),
            $slider = $wrapper.find('> ul'),
            $items = $slider.find('> li'),
            $single = $items.filter(':first'),

            singleWidth = $single.outerWidth(), 
            visible = Math.ceil($wrapper.innerWidth() / singleWidth), // note: doesn't include padding or border
            currentPage = 1,
            pages = Math.ceil($items.length / visible);            


        // 1. Pad so that 'visible' number will always be seen, otherwise create empty items
        if (($items.length % visible) != 0) {
            $slider.append(repeat('<li class="empty" />', visible - ($items.length % visible)));
            $items = $slider.find('> li');
        }

        // 2. Top and tail the list with 'visible' number of items, top has the last section, and tail has the first
        $items.filter(':first').before($items.slice(- visible).clone().addClass('cloned'));
        $items.filter(':last').after($items.slice(0, visible).clone().addClass('cloned'));
        $items = $slider.find('> li'); // reselect

        // 3. Set the left position to the first 'real' item
        $wrapper.scrollLeft(singleWidth * visible);

        // 4. paging function
        function gotoPage(page) {
            var dir = page < currentPage ? -1 : 1,
                n = Math.abs(currentPage - page),
                left = singleWidth * dir * visible * n;

            $wrapper.filter(':not(:animated)').animate({
                scrollLeft : '+=' + left
            }, 500, function () {
                if (page == 0) {
                    $wrapper.scrollLeft(singleWidth * visible * pages);
                    page = pages;
                } else if (page > pages) {
                    $wrapper.scrollLeft(singleWidth * visible);
                    // reset back to start position
                    page = 1;
                } 

                currentPage = page;
            });                

            return false;
        }

        $wrapper.after('<a class="arrow back">&lt;</a><a class="arrow forward">&gt;</a>');

        // 5. Bind to the forward and back buttons
        $('a.back', this).click(function () {
            return gotoPage(currentPage - 1);                
        });

        $('a.forward', this).click(function () {
            return gotoPage(currentPage + 1);
        });

        // create a public interface to move to a specific page
        $(this).bind('goto', function (event, page) {
            gotoPage(page);
        });
    });  
};

$(document).ready(function () {
  $('.infiniteCarousel').infiniteCarousel();
});
</script>
</head>
<body>


        <div class="infiniteCarousel">
            <div class="wrapper">
                <ul>
                    <li><a href="images/pic1.png"><img src="images/pic1.png" /></a></li>
                    <li><a href="images/pic2.png"><img src="images/pic2.png" /></a></li>
                    <li><a href="images/pic3.png"><img src="images/pic3.png" /></a></li>
                    <li><a href="images/pic4.png"><img src="images/pic4.png" /></a></li>
                    <li><a href="images/pic5.png"><img src="images/pic5.png" /></a></li>
                    <li><a href="images/pic6.png"><img src="images/pic6.png" /></a></li>
                    <li><a href="images/pic7.png"><img src="images/pic7.png" /></a></li>
                    <li><a href="images/pic8.png"><img src="images/pic8.png" /></a></li>
                </ul>    
            </div>
        </div>



</body>
</html>

And this:

.infiniteCarousel{
position: relative;
float: right;
width: 501px;
margin: 193px 0 0 -2px;
z-index: 10000;
}
.infiniteCarousel .wrapper{
position: absolute;
top: 0;
min-height: 10em;
width: 395px;
margin: 0 40px;
overflow: auto;
}
.infiniteCarousel ul a img{border: none;}
.infiniteCarousel .wrapper ul{
position: absolute;
padding:0;
top: 0;
width: 9999px;
margin:0;
list-style-image:none; ist-style-position:outside; list-style-type:none;
}
.infiniteCarousel ul li{
display:block;
float:left;
padding: 10px 5px;
height: 124px;
width: 124px;
}
.infiniteCarousel ul li a img{display:block;}
.infiniteCarousel .arrow{
display: block;
height: 50px;
width: 50px;
background: url(../images/arrow.png) no-repeat 0 0;
text-indent: -999px;
position: absolute;
top: 33px;
cursor: pointer;
}
.infiniteCarousel .forward{background: url(../images/arrowright.png) no-repeat 0 0; right: 3px;}
.infiniteCarousel .back{background: url(../images/arrowleft.png) no-repeat 0 0; left: -12px;}
.infiniteCarousel .forward:hover{background: url(../images/arrowrighthover.png) no-repeat 0 0;}
.infiniteCarousel .back:hover{background: url(../images/arrowlefthover.png) no-repeat 0 0;}
A: 

How about clearing after each third li:

    <div class="infiniteCarousel">
        <div class="wrapper">
            <ul>
                <li><a href="images/pic1.png"><img src="images/pic1.png" /></a></li>
                <li><a href="images/pic2.png"><img src="images/pic2.png" /></a></li>
                <li><a href="images/pic3.png"><img src="images/pic3.png" /></a></li>
                <br class="clear" />
                <li><a href="images/pic4.png"><img src="images/pic4.png" /></a></li>
                <li><a href="images/pic5.png"><img src="images/pic5.png" /></a></li>
                <li><a href="images/pic6.png"><img src="images/pic6.png" /></a></li>
                <br class="clear" />
                <li><a href="images/pic7.png"><img src="images/pic7.png" /></a></li>
                <li><a href="images/pic8.png"><img src="images/pic8.png" /></a></li>
            </ul>    
        </div>
    </div>

Notice the addition of <br class="clear" /> above. The CSS for clear class is:

.clear{
  clear:both;
}
Sarfraz