I have been doing some Code School classes on jQuery, and I needed a image slider for my site. Right now, there are only two responsive states, but that will most likely change (and/or become fluid later).
I was just wondering if there are any best practices or general clean up I can do to the code. I'm still a newbie with jQuery, so any advice would be helpful.
Plugins used: Fastclick and hammer.js
$(function() {
FastClick.attach(document.body);
var slide = $(".ImgSection li");
var ImgSec = $(".ImgSection").hammer();
var CrntPos = 0;
var Width;
var Time;
var PlusPos;
$(window).on("load resize", function(e) {
Width = slide.outerWidth(true);
});
ImgSec.on("dragleft dragright", "li", function(ev) {
ev.gesture.preventDefault();
});
function changeImg (e){
CrntPos = $(this).index();
var ClkWth = Width * .1;
var NewPos = (CrntPos * Width) - ClkWth;
slide.css("transform", "translate3d(-"+ NewPos +"px,0,0)");
if (CrntPos === 1 ){
$("li:eq(0)").on("click", function() {
slide.css("transform", "translate3d(0,0,0)");
});
}
}
slide.click(changeImg);
ImgSec.on("swipe", "li", function(ev) {
if(ev.gesture.direction == 'left'){
slide.eq(CrntPos + 1).trigger("click");
}
if(ev.gesture.direction == 'right'){
slide.eq(CrntPos - 1).trigger("click");
}
if($(this).is(':last-child') && ev.gesture.direction == 'left'){
slide.eq(0).trigger("click");
}
});
$(window).resize(function() {
clearTimeout(Time);
Time = setTimeout(Resize, 100);
});
function Resize(){
if ($('img', slide).css('max-width') == '245px' ){
slide.eq(CrntPos).trigger('click');
}
else {
slide.eq(CrntPos).trigger('click');
}
}
});