Doing a roll-in/roll-out slideshow in jQuery

Posted by erizias on Stack Overflow See other posts from Stack Overflow or by erizias
Published on 2012-06-01T22:36:47Z Indexed on 2012/06/01 22:41 UTC
Read the original article Hit count: 247

Filed under:
|
|
|
|

I am trying to create a roll-in / roll-out slideshow in jQuery/JavaScript. My problem is, that it needs to be repeated. And right now when it's starting over, the pictures doesnt come from the right side anymore :(

The reason for which I have created the slideLeft function, is that afterwards I need to create 2 functions, where the user can interrupt the slideshow manually pressing a left or right button.

This is what I've got:

<script class="jsbin" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>

<div style='background: #c4c4c4; border: 1px solid #7b7b7b; height: 220px; overflow: hidden; padding: 5px; position: absolute; width: 590px;'>
  <div id='slider-image-1' style='left: 5px; background: red; height: 216px; padding: 2px; position: absolute; top: 5px; width: 586px;'></div>
  <div id='slider-image-2' style='left: 600px; background: yellow; height: 216px; padding: 2px; position: absolute; top: 5px; width: 586px;'></div>
  <div id='slider-image-3' style='left: 600px; background: green; height: 216px; padding: 2px; position: absolute; top: 5px; width: 586px;'></div>
  <div id='slider-image-4' style='left: 600px; background: blue; height: 216px; padding: 2px; position: absolute; top: 5px; width: 586px;'></div>
</div>

<script type='text/javascript'>
  $(document).ready(function() {
    var amount = 0;
    var nextamount = 1;
    setInterval(function() {
      amount++;
      nextamount++;
      if (nextamount === 5) nextamount = 1;
      if (amount === 5) amount = 1;
        slideLeft(amount, nextamount);
      }, 2000);
    });

  function slideLeft(i, j) {
    var $theItem = $('#slider-image-' + i);
    $theItem.animate({
      left: parseInt($theItem.css('left'), 10) == 5 ? -$theItem.outerWidth() : 5
    }, 500);

    var $theItem = $('#slider-image-' + j);
    $theItem.animate({
      left: parseInt($theItem.css('left'), 10) == 5 ? $theItem.outerWidth() + 10 : 5
    }, 500);

  };
</script>

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery