Skip to content

Instantly share code, notes, and snippets.

@virtualmayur
Created November 21, 2014 08:10
Show Gist options
  • Save virtualmayur/7f7919d814cf20cec0e7 to your computer and use it in GitHub Desktop.
Save virtualmayur/7f7919d814cf20cec0e7 to your computer and use it in GitHub Desktop.
very basic jquery slider slide logic
'use strict';
$(function(){
//Configuration
var width = 720;
var animationSpeed = 1000;
var pause = 3000;
var currentSlide = 1;
//Cache DOM
var $slider = $('#slider');
var $sliderContainer = $slider.find('.slides');
var $slides = $sliderContainer.find('.slide');
var interval;
// Slider logic
function startSlider(){
interval = setInterval(function(){
$sliderContainer.animate({'margin-left': '-='.width}, animationSpeed, function(){
currentSlide++;
if(currentSlide === $slides.length){
currentSlide = 1;
$sliderContainer.css({'margin-left':0});
}
});
}, pause);
}
function stopSlider(){
clearInterval(interval);
}
//Mouse enter and mouse leave event
$slider.on('mouseenter', stopSlider).on('mouseleave', startSlider);
startSlider();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment