Simple Slider
Simple Slider
HTML
<div class="container">
<div class="prev ban"></div>
<div class="slide-wrap">
<div class="slide-content">
<div class="slide">1</div>
<div class="slide">2</div>
<div class="slide">3</div>
<div class="slide">4</div>
</div>
</div>
<div class="next"></div>
</div>
CSS
.container {
position: relative;
margin: 0 auto;
width: 500px;
height: 300px;
border: 1px solid black;
}
.prev {
position: absolute;
left: -60px;
top: 140px;
border: 20px solid transparent;
border-right: 20px solid black;
cursor: pointer;
z-index: 1;
}
.next {
position: absolute;
right: -60px;
top: 140px;
border: 20px solid transparent;
border-left: 20px solid black;
cursor: pointer;
z-index: 1;
}
.ban {
visibility: hidden;
}
.slide-wrap {
position: relative;
width: 100%;
height: 100%;
overflow: hidden;
}
.slide-content {
position: absolute;
left: 0;
top: 0;
width: 2000px;
height: 100%;
}
.slide {
float: left;
width: 500px;
height: 100%;
font-size: 40px;
line-height: 300px;
text-align: center;
}
JavaScript
$(function(){
function Slider(slider_class, animation_type) {
var _ = this;
_.slider_tag = $('.' + slider_class);
_.slider_content = $(_.slider_tag).find('.slide-content');
_.prev = $(_.slider_tag).find('.prev');
_.next = $(_.slider_tag).find('.next');
_.cursor = 0;
$(_.prev).click(function () {
if(_.cursor > 0) {
_.cursor--;
$(_.slider_content).animate({
left: _.cursor*-500
}, 500, animation_type, function(){
if(_.cursor === 0) $(_.prev).addClass('ban');
// when you click the prev arrow, the next arrow should be visiable
$(_.next).removeClass('ban');
});
}
});
$(_.next).click(function () {
if(_.cursor < 3) {
_.cursor++;
$(_.slider_content).animate({
left: _.cursor*-500
}, 500, animation_type, function(){
if(_.cursor === 3) $(_.next).addClass('ban');
// when you click the next arrow, the prev arrow should be visiable
$(_.prev).removeClass('ban');
});
}
});
}
new Slider('container', 'linear');
});