/*
* hwslider���ݻ����л���� - v1.0
* by �¹��
*/
; (function($, window, document, undefined) {
var hwslider = function(ele, opt) {
var self = this;
self.$element = ele,
self.defaults = {
width: 600,
height: 320,
start: 1,
speed: 400,
interval: 3000,
autoplay: false,
dotshow: true,
arrshow: true,
touch: true,
afterslider: function() {}
},
self.clickable = true,
self.options = $.extend({},
self.defaults, opt)
};
hwslider.prototype = {
init: function() {
var self = this,
ele = self.$element;
var sliderinder = ele.children("ul");
var hwsliderli = sliderinder.children("li");
var hwslidersize = hwsliderli.length;
var index = self.options.start;
var touchstarty = 0,
touchstartx = 0;
if (self.options.arrshow) {
var arrelement = '';
ele.append(arrelement)
}
for (i = 1; i <= hwslidersize; i++) {
if (index == i) {
hwsliderli.eq(index - 1).addclass("active")
}
}
if (self.options.dotshow) {
var dot = "";
for (i = 1; i <= hwslidersize; i++) {
if (index == i) {
dot += ''
} else {
dot += ''
}
}
var dotelement = '
' + dot + "
";
ele.append(dotelement)
}
var resize = function() {
var swidth = ele.width();
var sheight = self.options.height / self.options.width * swidth;
ele.css("height", sheight);
if (self.options.arrshow) {
var arroffset = (sheight - 40) / 2;
ele.find(".arr").css("top", arroffset + "px")
}
if (self.options.dotshow) {
var dotwidth = hwslidersize * 20;
var dotoffset = (swidth - dotwidth) / 2;
ele.find(".dots").css("left", dotoffset + "px")
}
};
ele.css("height", self.options.height);
resize();
$(window).resize(function() {
resize()
});
if (self.options.arrshow) {
ele.find(".next").on("click",
function(event) {
event.preventdefault();
if (self.clickable) {
if (index >= hwslidersize) {
index = 1
} else {
index += 1
}
self.moveto(index, "next")
}
});
ele.find(".prev").on("click",
function(event) {
event.preventdefault();
if (self.clickable) {
if (index == 1) {
index = hwslidersize
} else {
index -= 1
}
self.moveto(index, "prev")
}
})
}
if (self.options.dotshow) {
ele.find(".dots span").on("click",
function(event) {
event.preventdefault();
if (self.clickable) {
var dotindex = $(this).data("index");
if (dotindex > index) {
dir = "next"
} else {
dir = "prev"
}
if (dotindex != index) {
index = dotindex;
self.moveto(index, dir)
}
}
})
}
if (self.options.autoplay) {
var timer;
var play = function() {
index++;
if (index > hwslidersize) {
index = 1
}
self.moveto(index, "next")
};
timer = setinterval(play, self.options.interval);
ele.hover(function() {
timer = clearinterval(timer)
},
function() {
timer = setinterval(play, self.options.interval)
})
}
if (self.options.touch) {
hwsliderli.on({
touchstart: function(e) {
touchstarty = e.originalevent.touches[0].clienty;
touchstartx = e.originalevent.touches[0].clientx
},
touchend: function(e) {
var touchendy = e.originalevent.changedtouches[0].clienty,
touchendx = e.originalevent.changedtouches[0].clientx,
ydiff = touchstarty - touchendy,
xdiff = touchstartx - touchendx;
if (math.abs(xdiff) > math.abs(ydiff)) {
if (xdiff > 5) {
if (index >= hwslidersize) {
index = 1
} else {
index += 1
}
self.moveto(index, "next")
} else {
if (index == 1) {
index = hwslidersize
} else {
index -= 1
}
self.moveto(index, "prev")
}
}
touchstarty = null;
touchstartx = null
},
touchmove: function(e) {
if (e.preventdefault) {
e.preventdefault()
}
}
})
}
},
moveto: function(index, dir) {
var self = this,
ele = self.$element;
var clickable = self.clickable;
var dots = ele.find(".dots span");
var sliderinder = ele.children("ul");
var hwsliderli = sliderinder.children("li");
if (clickable) {
self.clickable = false;
var offset = ele.width();
if (dir == "prev") {
offset = -1 * offset
}
sliderinder.children(".active").stop().animate({
left: -offset
},
self.options.speed,
function() {
$(this).removeclass("active")
});
hwsliderli.eq(index - 1).css("left", offset + "px").addclass("active").stop().animate({
left: 0
},
self.options.speed,
function() {
self.clickable = true
});
self.options.afterslider.call(self);
dots.removeclass("active");
dots.eq(index - 1).addclass("active")
} else {
return false
}
}
};
$.fn.hwslider = function(options) {
var hwslider = new hwslider(this, options);
return this.each(function() {
hwslider.init()
})
}
})(jquery, window, document);