Skip to content

Instantly share code, notes, and snippets.

@j100002ben
Created November 26, 2012 08:59
Show Gist options
  • Save j100002ben/4147281 to your computer and use it in GitHub Desktop.
Save j100002ben/4147281 to your computer and use it in GitHub Desktop.
jquery-slideshow.js ( Not test yet...T_T )
;(function(window, undefined) {
var document = window.document
, $ = window.jQuery
;
$.fn.slideshow = function(_settings){
var _default_settings = {
move_time: 50
, move_width: 'one' /* one || view */
, start_one: true
, start_wait: 0
, current_child: 0
}
, settings = $.extend(_default_settings, _settings)
, _handler = function(index, element){
var _old_action, _action = function(){
var sliding_go = 1
, slide_timeout = null
, $element = $(element)
, element_width = $(element).width()
, element_height = $(element).height()
, element_ul = $element.children('ul').get(0)
, i = 0
, element_child
, total_width
;
do{
element_child = element_ul.children[i++].cloneNode(true);
element_ul.appendChild(element_child);
total_width += element_child.scrollWidth;
}while( total_width < element_width );
var $child = $('li',element)
, child_width = $child.width()
, child_height = $child.height()
, child_num = $child.length
, current_child = settings.current_child
, move_width = settings.move_width == 'one' ? child_width : element_width
, slideShowFunction = function(back){
var step = ( !back ) ? 1 : -1;
if( slide_timeout )
clearTimeout(slide_timeout);
if( ! sliding_go )
return ;
if( step == -1 && current_child == 0 ){
$element.scrollLeft( ( current_child = child_num ) * move_width );
}
$element.animate({scrollLeft: ( move_width * (current_child + step) )}, 'normal',function(){
var post_index = current_child;
current_child += step;
if(step == 1 && current_child == child_num)
$element.scrollLeft((current_child=0));
$element.trigger('slideshown', {
'current_index': current_child
, 'post_index': post_index
});
slide_timeout = setTimeout(slideShowFunction, settings.move_time);
});
}
;
if( child_num <= 0 || child_num == 1 && !settings.start_one ){
return ;
}
$element.css({
'overflow': 'hidden'
, 'white-space':'nowrap'
}).bind('slideshow-jump', function(event){
clearTimeout(slide_timeout);
current_child = event.current_index;
$element.scrollLeft(move_width * current_child, 'normal', function(){
slide_timeout = setTimeout(slideShowFunction, settings.move_time);
});
}).bind('slideshow-pause', function(event){
sliding_go = 0;
}).bind('slideshow-start', function(event){
sliding_go = 1;
slideShowFunction();
}).bind('slideshow-prev', function(event){
slideShowFunction(true);
}).bind('slideshow-next', function(event){
slideShowFunction();
}).scrollLeft(move_width * current_child).contents().filter(function () {
return this.nodeType == 3;
}).replaceWith('');
$child.css({
'display': 'block'
, 'float': 'left'
, 'width': child_width + 'px'
, 'height': child_height + 'px'
});
slideShowFunction();
};
if( settings.start_wait > 0 ){
_old_action = _action;
_action = function(){
setTimeout(_old_action,settings.start_wait);
}
}
_action();
}
;
return this.each(_handler);
};
})(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment