Skip to content

Instantly share code, notes, and snippets.

@njif
Last active August 29, 2015 14:07
Show Gist options
  • Select an option

  • Save njif/447fec4c7b3b9ada8351 to your computer and use it in GitHub Desktop.

Select an option

Save njif/447fec4c7b3b9ada8351 to your computer and use it in GitHub Desktop.
responsive jquery slider
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
ul {
padding: 0;
margin: 0;
}
ul li {
padding: 0;
margin: 0;
list-style-type: none;
}
.container {
margin: 0 auto;
width: 90%;
background: #c4c4c4;
text-align: center;
position: relative;
}
.slider {
width: 40%;
overflow: hidden;
display: inline-block;
}
.slider__list {
position: relative;
width: 100%;
}
.slider__item {
position: absolute;
top: 0;
left: 0;
z-index: 0;
width: 100%;
}
.slider__item img {
width: 100%;
display: block;
}
.slider__item.active {
position: relative;
z-index: 1;
}
.slider__item.inslide {
z-index: 2;
}
.slider__controls-button {
position: absolute;
top: 50%;
display: block;
z-index: 3;
font-size: 30px;
height: 50px;
-ms-text-align-last: center;
text-align: center;
width: 50px;
margin-top: -25px;
background: #000000;
text-decoration: none;
border-radius: 50%;
line-height: 50px;
color: #ffffff;
opacity: .6;
}
.slider__controls-button_prev {
left: 10px;
}
.slider__controls-button_next {
right: 10px;
}
</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
</head>
<body>
<div class="container">
<div class="slider">
<ul class="slider__list">
<li class="slider__item active"><img src="http://lorempixel.com/600/300/people/1/" alt="" class="slider__pic" /></li>
<li class="slider__item"><img src="http://lorempixel.com/600/300/people/2/" alt="" class="slider__pic" /></li>
<li class="slider__item"><img src="http://lorempixel.com/600/300/people/3/" alt="" class="slider__pic" /></li>
<li class="slider__item"><img src="http://lorempixel.com/600/300/people/4/" alt="" class="slider__pic" /></li>
<li class="slider__item"><img src="http://lorempixel.com/600/300/people/5/" alt="" class="slider__pic" /></li>
<li class="slider__item"><img src="http://lorempixel.com/600/300/people/6/" alt="" class="slider__pic" /></li>
</ul>
<div class="slider__controls">
<a href="#" class="slider__controls-button slider__controls-button_prev"><</a>
<a href="#" class="slider__controls-button slider__controls-button_next">></a>
</div>
</div>
</div>
<script>
var slider = (function () {
function init() {
$('.slider__controls-button').on('click', function(e) {
e.preventDefault();
var $this = $(this),
slides = $this.closest('.slider').find('.slider__item'),
activeSlide = slides.filter('.active'),
nextSlide = activeSlide.next(),
prevSlide = activeSlide.prev(),
firstSlide = slides.first(),
lastSlide = slides.last();
if ($this.hasClass('slider__controls-button_next')) {
if (nextSlide.length) {
moveSlide(nextSlide, 'forward');
} else {
moveSlide(firstSlide, 'forward');
}
} else {
if (prevSlide.length) {
moveSlide(prevSlide, 'backward');
} else {
moveSlide(lastSlide, 'backward');
}
}
})
}
function moveSlide(slide, direction) {
var
container = slide.closest('.slider'),
slides = container.find('.slider__item'),
activeSlide = slides.filter('.active'),
slideWidth = slides.width(),
duration = 500,
reqCssPosition = 0,
reqSlideStrafe = 0;
if (direction === 'forward') {
reqCssPosition = slideWidth;
reqSlideStrafe = -slideWidth;
} else if (direction === 'backward') {
reqCssPosition = -slideWidth;
reqSlideStrafe = slideWidth;
}
slide.css('left', reqCssPosition).addClass('inslide');
var movableSlide = slides.filter('.inslide');
activeSlide.animate({ left: reqSlideStrafe }, duration);
movableSlide.animate({ left: 0 }, duration, function() {
var $this = $(this);
slides.css('left', '0').removeClass('active');
$this.toggleClass('inslide active');
});
}
return {
init: init,
moveSlide: moveSlide
}
}());
$(document).ready(function() {
if ($('.slider').length)
slider.init();
});
</script>
</body>
</html>
@njif
Copy link
Author

njif commented Oct 4, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment