`
JavaSam
  • 浏览: 935786 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

javascript按需加载模板

 
阅读更多
/**
 * @author wsf 滚动按需加载
 */
;
(function (w,$){
	var jutil = null;//工具类
	/**
	 * 滚动按需加载对象
	 */
    function demandLoader (){
    	this.startLoadFlag = {};//是否开始加载
    	this.allLoaded = false;//是否全部加载完成
    	//分批加载函数
    	this.batch = {
    			"fun1":this.loadCustTree,
    			"fun2":this.prevLoadActAndOpt,
    			"fun3":this.prevLoadActAndOpt
    			//等等
    	};
    }

    /**
     * 原型对象
     */
    demandLoader.prototype = {
    	constructor:demandLoader,//构造函数
    	//加载后台数据
    	getJson:function (url,param,callback,othargs){
    		var that = this;
    		$.getJSON(url,param,function (data){
    			callback.call(that,data,othargs);
    		});
    	},
    	//开始加载
    	startLoad:function(){
    		var scrolltop = arguments[0];//页面滚动高度
    		jutil = arguments[1];//jsUtil
    		if(typeof idxpos != "object"){
    			return false;
    		}
    		var catalog1 = idxpos.h2IdxPos;//一级目录位置
    		var catalog2 = idxpos.h3IdxPos;//二级目录位置
    		this.loadCataCommon(catalog1, scrolltop);//一级目录延迟加载
    		this.loadCataCommon(catalog2, scrolltop);//二级目录延迟加载
    	},
		// 目录公共加载方法
		loadCataCommon : function(catalog, scrolltop) {
			for ( var i in catalog) {
				var cl = catalog[i];// 目录
				var text = cl.text;// 加载哪一部分
				var catalogtop = cl.top;// 目录位置(父目录)
				var difTop = scrolltop - catalogtop;// 高度差
				if (difTop>0&&this.startLoadFlag[text] == undefined) {
					var loadFn = this.batch[text];// 加载处理函数
					if (jutil.isFunction(loadFn)) {
						this.startLoadFlag[text] = "startload";// 开始加载
						loadFn.call(this, text);
					} 
				}
			}
		},
		//加载客户关系树
		loadCustTree:function (){
			this.startLoadFlag[text] = "endload";//加载完成
    		this.endLoad();//加载结束
		},
    	//加载收入信息
    	loadIncome:function (){
    		this.startLoadFlag[text] = "endload";//加载完成
    		this.endLoad();//加载结束
    	},
    	//预先加载活动,机会信息
    	prevLoadActAndOpt:function (text){
    		this.startLoadFlag[text] = "endload";//加载完成
    		this.endLoad();//加载结束
    	},
    	//加载地址信息
    	loadDz:function (text){
    		this.startLoadFlag[text] = "endload";//加载完成
    		this.endLoad();//加载结束
    	},
        //结束加载
        endLoad:function (){
        	$._initCatelogPos();//重新计算目录位置
                this.allLoaded = true;
        	for(var catalotnm in this.batch){
        		this.allLoaded = this.allLoaded && (this.startLoadFlag[catalotnm] == "endload");
        	}
        }
    }
    
    w.demandLoader = demandLoader;//外部调用入口
})(window,jQuery);


调用方式
 /**
  * 窗口滚动事件
  */
 function winScroll(){
  scrollTop = $(win).scrollTop();
  if(!_demandLoader.allLoaded){
   _demandLoader.custid = $("#custid").val();//当前客户id
   //还没有加载过
   _jsUtils.throttle(function (){
    _demandLoader.startLoad.call(_demandLoader,scrollTop,_jsUtils);//开始按需加载
   },110);
  }
 }
 $(win).scroll(winScroll);//窗口滚动监听

 

 

 

 

 

 

 

0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics