Created
January 5, 2012 17:58
-
-
Save enapupe/1566377 to your computer and use it in GitHub Desktop.
simple left to right slider
This file contains hidden or 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
$.fn.enapupeSlider = function(changeDelay, transitionDelay, startDelay, halt_opt) { | |
var blockIt = function (a){ | |
if (!a.hasClass("blocked")) | |
return a.addClass("blocked"),!0 | |
} | |
this.each(function(inc, el){ | |
if (blockIt($(el))) { | |
//shows master slideshow div onstart | |
$(el).css("visibility", "visible"); | |
var timeOut = null; | |
var bannerPosition = 0; | |
var halt = 0; | |
var halt_tout; | |
var mousedown; | |
var force_release; | |
var slides = $(el).find("ul").addClass("slides"); | |
var slideWidth = slides.find("li").width(); | |
var slideCount = slides.find("li").length; | |
var navMenu = $("<div>").addClass("nav"); | |
var navWrapper = $("<div>").addClass("wrappNav"); | |
var i; | |
for (i = 1; i <= slideCount; i++) { | |
$("<a>").attr("href", "slide "+ i).attr("id", i).appendTo(navWrapper); | |
} | |
var navLi = navWrapper.find("a"); | |
navLi.eq(0).addClass("active"); | |
navLi.bind('click', function(e){ | |
e.preventDefault(); | |
clearTimeout(timeOut); | |
var value = Number($(this).attr('id')); | |
if (value == 1) { | |
bannerPosition = -slideWidth; | |
} else { | |
bannerPosition = (value-2) * slideWidth; | |
} | |
halt = 0; | |
mudaBanner(); | |
}); | |
navWrapper.appendTo(navMenu); | |
$(el).bind("mouseover", function(){ | |
halt = 1; | |
force_release = false; | |
}).bind("mouseout", function(){ | |
if (!mousedown) { | |
halt = 0; | |
} | |
force_release = true; | |
}).bind("mousedown", function(){ | |
mousedown = true; | |
}); | |
$("body").bind("mouseup", function(){ | |
mousedown = false; | |
if (force_release) { | |
halt = 0; | |
} | |
}); | |
var mudaBanner = function(){ | |
//check if should halt on mouse over | |
if (halt === 1&&halt_opt) { | |
clearTimeout(halt_tout); | |
halt_tout = setTimeout(function(){ | |
mudaBanner(); | |
}, 600); | |
} else { | |
clearTimeout(timeOut); | |
bannerPosition += slideWidth; | |
//reset if > maximum | |
bannerPosition>=slideWidth*slideCount&&(bannerPosition=0) | |
var eqNav = bannerPosition/slideWidth; | |
slides.stop().animate({'marginLeft':'-'+bannerPosition+'px'}, transitionDelay); | |
navLi.removeClass("active").eq(eqNav).addClass("active"); | |
//is autoplay? | |
startDelay!==!1&&(timeOut=setTimeout(mudaBanner,changeDelay)) | |
} | |
} | |
//append navigation after is ready to avoid unnecessary repaints | |
navMenu.appendTo(slides.parent()); | |
// is auto play? | |
startDelay!==!1&&setTimeout(mudaBanner,startDelay) | |
} | |
}); | |
return $(this); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment