/** * @author Sun Haihua * @since 2016/1/21 */ $(function () { Utility.so("Utility.Scroll", (function () { var scrollConfig = { direction:{ UP : 0, DOWN : 1, LEFT : 2, RIGHT : 3 }, def:{ warp:'scroll', cnt:'scroll-cnt', item:'scroll-item', direction:0, spceTimer:false }, timmer:50 }; var hideDiv = Utility.autoId('Agrij-scroll-hide-div-'); $(document.body).append('
'); hideDiv = $("#"+hideDiv); var timmer = null; var timmerTask = []; function startGlobalTimer(){ if(timmer)return; timmer = setInterval(function(){ for(var i in timmerTask){ try{ ///console.log(timmerTask); timmerTask[i].scrolling.call(timmerTask[i]); }catch(e){ //console.log("task error:",e); } } },scrollConfig.timmer); }; var scroll = function(config){ this.pause = false; this.id = Utility.autoId('Agrij-scroll-'); this.conf = this.parseConf(config); this.init(); this.scanHtmlConf(); }; scroll.prototype.init = function(config){ if(typeof this.conf.warp == 'string'){ this.warp = $($('.'+this.conf.warp).get(0)); }else{ this.warp = $(this.conf.warp); } this.debug = this.warp.attr('isdebug'); this.warp.attr('id',this.id); this.warp.html('
'+this.warp.html()+'
'); this.initItems(); this.prepareScroll(); this.addToTimer(); }; scroll.prototype.addToTimer = function(){ if(this.conf.spceTimer) this.timmer = setInterval(function(){self.scrolling.call(self);},scrollConfig.timmer); else timmerTask.push(this); var self = this; this.warp.hover(function(){ if(self.spceTimer)clearInterval(self.timmer); else self.pause = true; },function(){ if(self.spceTimer)self.timmer = setInterval(function(){self.scrolling.call(self);},scrollConfig.timmer); else self.pause = false; }); startGlobalTimer(); }; scroll.prototype.prepareScroll = function(){ [this.up,this.down,this.left,this.right][this.conf.direction].call(this); }; scroll.prototype.up = function(){ if(this.size.h>this.items.c*this.items.h) return this.scrolling = function(){}; this.cnt.obj.append(this.cnt.obj.html()); var cnt = this.cnt.obj.get(0); var max = this.cnt.h; var tmp = 0; this.scrolling = function(){ try{ //console.log("===>",this.warp,tmp,max,this.warp.get(0).scrollTop); if(this.pause)return; tmp++; if(tmp>(max*2-this.warp.height()))tmp = 0; this.warp.get(0).scrollTop = tmp; }catch(e){ } }; }; scroll.prototype.down = function(){ }; scroll.prototype.left = function(){ }; scroll.prototype.right = function(){ }; scroll.prototype.initItems = function(){ this.size = {w:this.warp.width(), h:this.warp.height()}; this.cnt = { obj : this.$("#"+this.id+"_cnt"), w : this.$("#"+this.id+"_cnt").width(), h : this.$("#"+this.id+"_cnt").height() }; this.items = { w : this.$('.'+this.conf.item).width(), h : this.$('.'+this.conf.item).height(), c : this.$('.'+this.conf.item).length }; }; scroll.prototype.$ = function(slt){ return this.warp.find(slt); }; scroll.prototype.parseConf = function(conf){ var ret = { warp:scrollConfig.def['warp'], cnt:scrollConfig.def['cnt'], item:scrollConfig.def['item'], direction:scrollConfig.def['direction'], spceTimer:scrollConfig.def['spceTimer'] }; for(var i in conf) ret[i] = conf[i]; return ret; }; scroll.prototype.scanHtmlConf = function(){ if(this.warp.attr('scroll')) this.conf.direction = Utility.defget( this.warp.attr('scroll').toUpperCase(), scrollConfig.direction, 0); if(this.warp.attr('spce-timer')) this.conf.spceTimer = Utility.defget( this.warp.attr('spce-timer').toUpperCase(), {'true':true,'false':false}, false); }; window.getScrollTask = function(){ return [timmerTask]; } return scroll; })()); $(".scroll-controller").each(function(){ new Utility.Scroll({warp:this}); }); });