div.controls
is vital for autoplay functionality
Last active
August 29, 2015 14:13
-
-
Save tillsanders/4c878e216221f79e1454 to your computer and use it in GitHub Desktop.
Very simple slideshow plugin for jquery
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<section class="slideshow"> | |
<div class="viewbox"> | |
<ul class="slides"> | |
<li><img src="1.jpg" alt="" /></li> | |
<li><img src="1.jpg" alt="" /></li> | |
<li><img src="1.jpg" alt="" /></li> | |
</ul> | |
</div> | |
<div class="controls"> | |
<ul> | |
<li><a class="active" data-slide="1"></a></li> | |
<li><a data-slide="2"></a></li> | |
<li><a data-slide="3"></a></li> | |
</ul> | |
</div> | |
</section> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function ($) { | |
var settings; | |
$.fn.slideshow = function(options) { | |
settings = $.extend({}, $.fn.slideshow.defaults, options); | |
return this.each(function() { | |
var container = $(this); | |
adjust(container); | |
$(document).ready(function() { | |
adjust(container); | |
}); | |
$(window).on('resize load', function() { | |
adjust(container); | |
}); | |
container.find('a[data-slide]').click(function() { | |
settings.onSlide.call(container); | |
var link = $(this); | |
container.find('a[data-slide]').removeClass('active'); | |
link.addClass('active'); | |
slide(container, link.parent('li').index(), settings.speed); | |
}).first().trigger('click'); | |
if(settings.autoplay) { | |
autoplay(container); | |
} | |
}); | |
}; | |
function adjust(container) { | |
var slides_container = container.find('ul.slides'); | |
var slides = slides_container.find('li'); | |
var width = container.width(); | |
slides_container.css('width', width * slides.length); | |
slides.css('width', width); | |
slide(container, container.find('a.active').parent('li').index(), 0); | |
} | |
function slide(container, index, duration) { | |
if(duration > 0) { | |
container.find('ul.slides').animate({ | |
left: -index * container.width() | |
}, duration); | |
} else { | |
container.find('ul.slides').css('left', -index * container.width()); | |
} | |
if(settings.autoplay) { | |
autoplay(container); | |
} | |
} | |
function autoplay(container) { | |
clearInterval(container.data('interval')); | |
container.data('interval', setInterval(function() { | |
container.find('a[data-slide]').eq((container.find('a.active[data-slide]').parent('li').index() + 1) % container.find('.controls li').length).trigger('click'); | |
}, settings.interval)); | |
} | |
$.fn.slideshow.defaults = { | |
speed: 400, | |
autoplay: false, | |
interval: 7000, | |
onSlide: function() {} | |
}; | |
}(jQuery)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
section.slideshow { | |
position: relative; | |
.viewbox { | |
overflow: hidden; | |
} | |
ul { | |
position: relative; | |
left: 0; | |
overflow: hidden; | |
} | |
li { | |
float: left; | |
display: block; | |
} | |
img { | |
width: 100%; | |
} | |
} |
Introduced autoplay functionality :) ... still needs work, though 😄
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Has flaws atm and needs more work, I know ;)