ranking.js 2.62 KB
function newScoll(container, options){
		if (!container) return;
		options = options || {}; 
		var actions = new Object(); 
		var pre = options.pre, //后退按钮
			next = options.next,//前进按钮
			moveWidth    = options.moveWidth ? options.moveWidth : 0,//每次滚动的距离
			maxWidth     = options.maxWidth ? options.maxWidth : 0,//容器的最大宽度
			changeWidth  = options.changeWidth,//浏览器的临界宽度
			maxMoveWidth = options.maxMoveWidth,
    		minMoveWidth = options.minMoveWidth,
			actionCode   = 0,//防止连续点击
			end          = false;	

		//后退按钮
		pre.bind('click',function(){
			if(actionCode == 1){
				return false;
			}
			if(parseInt(container.css('left'))<0){//如果容器的左边有位移
				actionCode = 1;
				var left = parseInt(container.css('left'));
				actions.scoll(actions.getNewLeft(left,'+'));
			}
		});
		//前进按钮
		next.bind('click', function () {
			if(actionCode == 1){
				return false;
			}	
			if(actions.checkEnd()){//如果没有滚动到最后,则可以继续往下滚动
				actionCode = 1;	
				var left = parseInt(container.css('left'));
				actions.scoll(actions.getNewLeft(left,'-'));
			}
		});
		
		//判断是否到最后
		actions.checkEnd = function(){
			var left = parseInt(container.css('left'));
			if(-(left-moveWidth)>=maxWidth){
				end = true;
				return false;
			}else{
				end = false;
				return true;
			}
		};
		
		//图片滚动
		actions.scoll = function(newLeft){
			container.stop(true, false).animate({ "left": newLeft }, 600, function(){
			actions.setAstyle();
			actionCode = 0;
			});
		};
		
		//获得新的偏移量
		actions.getNewLeft = function(left,style){
			if(style=='-'){
				if(-(left-moveWidth)>(maxWidth-moveWidth)){
					var nowLeft = -(maxWidth-moveWidth);
				}else{
					var nowLeft = left - moveWidth;
				}
			}else{
				if(-left<moveWidth || -left==0){
					var nowLeft=0
				}else{
					var nowLeft = left + moveWidth;
				}
			}
			return nowLeft;
		};

		//改变箭头样式
		actions.setAstyle = function(){
			parseInt(container.css('left'))==0? pre.addClass('off') : pre.removeClass('off');
			actions.checkEnd() ? next.removeClass('off') : next.addClass('off');

		};

		//窗口改变初始化各种参数
		$(window).bind('resize',function(){
			var winwidth = parseInt($(window).width());
			var left = parseInt(container.css('left'));
			if (winwidth>changeWidth){
	   			moveWidth=maxMoveWidth;
	   		 }else{
	   			moveWidth=minMoveWidth;
	   		 }
			if(end==true){
				actions.scoll(-(maxWidth-moveWidth));
			}
			if(parseInt(container.css('left'))==0){
				actions.scoll(0);
			}
			actions.setAstyle();
		})
	}