Eric Goncalves
Eric Goncalves

Reputation: 5353

Simple jQuery content Slider

I have an unordered list that holds 'x' amount of list items. These list items will be shown but only 'n' amount will be visible at a time. I then want to add a next and previous thus sliding the previous content out and the new 'n' amount of list items in.

However what I have does the job as a paginator but well as a content slider.

jsFiddle

HTML

<ul>
  <li><span class="box"></span></li>
  <li><span class="box"></span></li>
  <li><span class="box"></span></li>
  <li><span class="box"></span></li>
  <li><span class="box"></span></li>
  <li><span class="box"></span></li>
  <li><span class="box"></span></li>
  <li><span class="box"></span></li>
  <li><span class="box"></span></li>
</ul>
<a href="" id="prev">Prev</a>
<a href="" id="next">Next</a>

CSS

body {
  margin: 5px;
}
ul {
  overflow: hidden;
}
li {
  float: left;
}
.box {
  width: 100px;
  height: 100px;
  background: #33cccc;
  margin: 5px;
  display: block;
}

JS

var from = 0, step = 3;

// show show next function
function showNext(list) {
  list
    .find('li').hide().end()
    .find('li:lt(' + (from + step) + '):not(li:lt(' + from + '))')
      .show();
  from += step;
}
// show previous function
function showPrevious(list) {
  from -= step;
  list
    .find('li').hide().end()
    .find('li:lt(' + from + '):not(li:lt(' + (from - step) + '))')
      .show();
}

// show initial set
showNext($('ul'));

// clicking on the 'more' link:
$('#more').click(function(e) {
  e.preventDefault();
  showNext($('ul'));
});

// clicking on the 'prev' link:
$('#prev').click(function(e) {
  e.preventDefault();
  showPrevious($('ul'));
});

Upvotes: 2

Views: 2247

Answers (1)

antejan
antejan

Reputation: 2624

If I understand right, you want to slide next/prev items from the sides.

You need to make a long block with all the items and a fixed sizes wrapper block around it with overflow:hidden. And then animate position of inner block (position:relative;left:-...px;) or wrapper scroll https://jsfiddle.net/uXn2p/1/

Upvotes: 4

Related Questions