Skip to content

Instantly share code, notes, and snippets.

@rbk
Created February 6, 2015 15:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rbk/489aa16fa527cc42b918 to your computer and use it in GitHub Desktop.
Save rbk/489aa16fa527cc42b918 to your computer and use it in GitHub Desktop.
My first slider made with jquery
function init_slider(id, num){
// Cache the ID
var id = '#' + id;
// Define slide height
var slider_height = '143px';
if( $(window).width() < 767 ){
slider_height = 'auto';
}
// Get the count of list items
var slides = $(id + ' ul li');
var count = slides.length;
var ul_height = $(id + ' ul li').css('height');
$(id +' ul').css({'overflow':'hidden', 'height': slider_height, 'padding': '5px'});
// test for num, this determines how many slides should be visible at once
count = num;
// Get width of the slider container
var ulw = $(id + ' ul').width();
// Set the width to even
var slide_width = ulw / count;
slides.css({'width': slide_width,'float':'left', 'height' : slider_height });
$(id + ' img').css({'width': '50%', 'height' :'auto'});
// set margin-top of each image to half its height
// Cache the ul
var ul = $(id + ' ul');
// Bind window size with function to make responsize
// don't rotate unless there are more slides that suppose tto be showing at once
// slides.length > num &&
if( $(window).width() > 767 ) {
// Loop
var t = setInterval(function(e){
ul.css({
'width': ulw + slide_width * 2,
'overflow' : 'hidden'
});
slides = $(id + ' ul li');
var lead = slides.eq(0);
lead.clone().appendTo( ul );
var list_item_width = $(id + ' ul li').css('width');
$(id + ' ul li:first-child').animate({'margin-left': '-=' + list_item_width }, 2000, 'swing', function() {
// Animation complete.
lead.detach();
});
},(6000));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment