function statemanger (config) { var currstate = 0, statelist = config.statelist, statelen = statelist.length, running = false; var changestate = function (frame) { if( running ){ return; } if( !statelist[frame] ){ return; } if( currstate != frame ){ statelist[currstate].out(); } if( !statelist[frame].load ){ statelist[frame].init(); statelist[frame].load = true; } running = true; statelist[frame].reset && statelist[frame].reset(); if(statelist[frame].delay){ settimeout(function(){ statelist[frame].view(); },statelist[frame].delay) }else{ statelist[frame].view(); } settimeout(function(){ running = false; },statelist[frame].time*1000); currstate = frame; } var scroll = function (delta) { console.log(delta) changestate(currstate+delta); } var bindwheel = function(){ var mwdata = false; var t; $(document).bind("mousewheel", function (e, delta) { if (mwdata) { return; } mwdata = true; scroll(delta > 0 ? -1 : 1); settimeout(function () { mwdata = false; }, 1000); }); } var bindkey = function(keyprev,keynext){ var mwdata = false; var t; $(document).bind("keydown", function (e) { var e = e || window.event var keycode = e.which || e.keycode; if (!mwdata) { if (keycode == keynext) { scroll(1); } else if (keycode == keyprev) { scroll(-1); } mwdata = true; cleartimeout(t); t = settimeout(function () { mwdata = false; }, 60); } }); } var init = function(){ config.key && bindkey(config.key.prev,config.key.next); config.wheel && bindwheel(); config.init && config.init(); } return { init : init, changestate : changestate, getstate : function(){ return parseint(currstate); } } }