Skip to content

Instantly share code, notes, and snippets.

@NatalieMac
Created April 5, 2014 16:52
Show Gist options
  • Save NatalieMac/9994504 to your computer and use it in GitHub Desktop.
Save NatalieMac/9994504 to your computer and use it in GitHub Desktop.
jQuery Slideshow Plugin
<!doctype html>
<html lang="en" class="jsOff">
<head>
<meta charset="UTF-8">
<title>Slideshow</title>
<link rel="stylesheet" href="styles/style.css">
</head>
<body>
<div class="slider">
<ul>
<li><img src="images/slide1.jpg" height="300" width="600" alt="Image 1"></li>
<li><img src="images/slide2.jpg" height="300" width="600" alt="Image 2"></li>
<li><img src="images/slide3.jpg" height="300" width="600" alt="Image 3"></li>
<li><img src="images/slide4.jpg" height="300" width="600" alt="Image 4"></li>
<li><img src="images/slide5.jpg" height="300" width="600" alt="Image 5"></li>
</ul>
</div>
<div class="slider-nav">
<button data-dir="prev">Previous</button>
<button data-dir="next">Next</button>
</div>
<div class="slider">
<ul>
<li><img src="images/slide6.jpg" height="300" width="300" alt="Image 6"></li>
<li><img src="images/slide7.jpg" height="300" width="300" alt="Image 7"></li>
<li><img src="images/slide8.jpg" height="300" width="300" alt="Image 8"></li>
<li><img src="images/slide9.jpg" height="300" width="300" alt="Image 9"></li>
</ul>
</div>
<div class="slider-nav">
<button data-dir="prev">Previous</button>
<button data-dir="next">Next</button>
</div>
<script src="scripts/jquery-2.1.0.min.js"></script>
<script src="scripts/jquery.techjobs-slider.js"></script>
<script>
$('.slider').tjlaslider();
</script>
</body>
</html>
(function($, window, document, undefined){
$.fn.tjlaslider = function(options){
var opts = $.extend( {}, $.fn.tjlaslider.defaults, options);
return this.each(function(){
var $this = $(this),
sliderUL = $this.css('overflow', 'hidden').children('ul'),
imgs = sliderUL.find('img'),
nav = $this.next('.slider-nav');
$this.data('imgWidth', imgs[0].width);
$this.data('imgsLen', imgs.length);
$this.data('current', 1);
$this.data('totalImgsWidth', $this.data('imgsLen')*$this.data('imgWidth'));
$this.width($this.data('imgWidth'));
nav.show().find('button').on('click', function() {
var direction = $(this).data('dir'),
loc = $this.data('imgWidth'),
current = $this.data('current');
console.log(current);
( direction === 'next' ) ? $this.data('current', ++current) : $this.data('current', --current);
if ($this.data('current') === 0 ) {
$this.data('current', $this.data('imgsLen'));
loc = $this.data('totalImgsWidth') - $this.data('imgWidth');
direction = 'next';
} else if ( $this.data('current') - 1 === $this.data('imgsLen')) {
$this.data('current', 1);
loc = 0;
}
console.log($this.data('current'));
transition(sliderUL, loc, direction);
});
function transition( container, loc, direction ) {
var unit;
if (direction && loc !== 0 ) {
unit = (direction === 'next' ) ? '-=' : '+=';
}
container.animate({
'margin-left': unit ? (unit + loc) : loc
});
}
});
};
$.fn.tjlaslider.defaults = {
}
}(jQuery, window, document));
body {
background: #7cc0cb;
font-family: Geneva, "Lucida Sans", "Lucida Grande", "Lucida Sans Unicode", Verdana, sans-serif;
margin: 1em auto;
max-width: 45em;
}
h1, h2, h3, h4, h5, h6 {
font-family: Geneva, Verdana, "Lucida Sans", "Lucida Grande", "Lucida Sans Unicode", sans-serif;
}
/* Slideshow */
.slider-nav {
display: none;
margin-top: 1em;
}
.slider-nav button {
padding: 1em;
margin-right: 1em;
border-radius: 10px;
cursor: pointer;
}
.slider {
height: 300px;
overflow: scroll;
}
.slider ul {
margin: 0;
padding: 0;
width: 10000px;
list-style: none;
}
.slider li {
float: left;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment