css - jQuery infinite carousel leaves an empty space when scrolling to the left -
i'm using jquery responsive carousel display images , text. carousel works when clicking on forward (right) button (the hidden slide appears smoothly until gets place), has weird behavior when clicking on backward (left) button, because blank space (as wide slide) appears , after animation completed slide appears suddenly.
this html code:
<div id='carousel_container'> <div id='left_scroll'><img src='navleft.png' /></div> <div id='carousel_inner'> <ul id="carousel_ul"> <li> ....content...</li> <li> ....content...</li> ......... </ul> </div> <div id='right_scroll'><img src='navright.png' /></div> </div>
this css:
#carousel_container{ display:table; margin-left:auto; margin-right:auto; } #carousel_inner { float:left; width:660px; overflow: hidden; } #carousel_ul { position:relative; left:0px; list-style-type: none; padding: 0px; margin:0; width:9999px; /* important */ padding-bottom:10px; } #carousel_ul li{ float: left; width:210px; padding:0px; margin-top:10px; margin-bottom:10px; margin-left:5px; margin-right:5px; } #left_scroll, #right_scroll{ float:left; } #left_scroll img, #right_scroll img{ cursor: pointer; cursor: hand; }
this jquery code:
jquery(document).ready(function() { //when user clicks image sliding right jquery('#right_scroll img').click(function(){ var item_width = jquery('#carousel_ul li').outerwidth() + 10; var left_indent = parseint(jquery('#carousel_ul').css('left')) - item_width; jquery('#carousel_ul').animate({'left' : left_indent}, {duration:500, complete: function(){ jquery('#carousel_ul li:last').after(jquery('#carousel_ul li:first')); jquery('#carousel_ul').css({'left' : '0px'}); } }); }); //when user clicks image sliding left jquery('#left_scroll img').click(function(){ var item_width = jquery('#carousel_ul li').outerwidth() + 10; var left_indent = parseint(jquery('#carousel_ul').css('left')) + item_width; jquery('#carousel_ul').animate({'left' : left_indent}, {duration:500, complete: function(){ jquery('#carousel_ul li:first').before(jquery('#carousel_ul li:last')); jquery('#carousel_ul').css({'left' : '0px'}); } }); }); });
you're telling rotate items after animation completes. fine when going forward, because want animation complete, , offscreen item pushed end. when going need move last item place @ front of carousel (offscreen) , then animate. like:
//when user clicks image sliding left jquery('#left_scroll img').click(function(){ var item_width = jquery('#carousel_ul li').outerwidth() + 10; // rotate elements jquery('#carousel_ul li:first').before(jquery('#carousel_ul li:last')); // start offscreen jquery('#carousel_ul').css({'left' : (-item_width)+'px'}); // animate jquery('#carousel_ul').animate({'left' : 0},{duration:500}); });
Comments
Post a Comment