Skip to content

Instantly share code, notes, and snippets.

@IGGY-MODEL
Last active August 29, 2015 14:24
Show Gist options
  • Select an option

  • Save IGGY-MODEL/4126d53d0b38294c11b0 to your computer and use it in GitHub Desktop.

Select an option

Save IGGY-MODEL/4126d53d0b38294c11b0 to your computer and use it in GitHub Desktop.
Easy jQuery auto slider
<!-- HTML -->
<div class="container">
<div class="slider">
<div class="left"></div>
<ul>
<li><img src="img/1.jpg" height="300" width="500" alt=""></li>
<li><img src="img/2.jpg" height="300" width="500" alt=""></li>
<li><img src="img/3.jpg" height="300" width="500" alt=""></li>
<li><img src="img/4.jpg" height="300" width="500" alt=""></li>
<li><img src="img/5.jpg" height="300" width="500" alt=""></li>
<li><img src="img/6.jpg" height="300" width="500" alt=""></li>
<li><img src="img/7.jpg" height="300" width="500" alt=""></li>
<li><img src="img/8.jpg" height="300" width="500" alt=""></li>
<li><img src="img/9.jpg" height="300" width="500" alt=""></li>
</ul>
<div class="right"></div>
</div>
</div>
<!-- END HTML -->
/***Sass***/
$imgs: 9
.container
width: 500px
margin: 100px auto
.slider
width: 500px
height: 300px
outline: 5px solid #f35
position: relative
overflow: hidden
box-shadow: 10px 10px 15px #666
ul
padding: 0
margin: 0
position: absolute
width: 4500px
li
display: inline-block
list-style-type: none
float: left
img
width: 500px
height: 300px
.left,
.right
position: absolute
width: 25px
height: 25px
display: inline-block
background: #f65
z-index: 10
border: 3px solid #f35
.left
top: 45%
left: -3px
border-radius: 0 20px 20px 0
box-shadow: 2px 2px 10px #666
.right
top: 45%
right: -3px
border-radius: 20px 0 0 20px
box-shadow: -2px -2px 10px #666
/*END Sass*/
/*JS (jQuery)*/
$(document).ready(function() {
var cur = 1;
var total = 9;
var slW = 500;
$('.left').on('click', function () {
cur = cur - 1;
console.log(cur);
if (cur < 1) {
$('ul').animate({'margin-left':'-'+ slW*(total-1)+'px'},300);
cur = total;
} else {
$('ul').animate({'margin-left':'+=500px'},1000);
}
});
$('.right').on('click', function () {
cur = cur + 1;
console.log(cur);
if (cur > total) {
$('ul').animate({'margin-left':'0px'},300);
cur = 1;
} else {
$('ul').animate({'margin-left':'-=500px'}, 1000);
}
});
var autoSlide;
function startSlider () {
autoSlide = setInterval(function () {
cur = cur + 1;
console.log(cur);
if (cur > total) {
$('ul').animate({'margin-left':'0px'},300);
cur = 1;
} else {
$('ul').animate({'margin-left':'-=500px'}, 1000);
}
}, 3000);
};
startSlider();
$('ul').hover(function() {
clearInterval(autoSlide);
}, function() {
startSlider();
});
});
/*END JS*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment