Tools.js 17.2 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662
/**
 * jinrun
 * 2014-05-22
 * 工具类
 */ 
var Tools = (function(){
	var self = this;
	this.amHover = function(_cname, _action, _obj){
		var t = 1800;
		if (_obj && _obj.time){
			t = _obj.time;
		}
		function clear(n,a){
			window.setTimeout(function(){
				n.removeClass(a);
			}, t);
		};
		
		if (typeof _cname=='object'){
			if (_cname.hasClass(_action))return false;
			_cname.addClass(_action);
			clear(_cname,_action);
			return false;
		}
		var _tag = $('.'+_cname);
		
		_tag.bind('mouseover', function(){
			if ($(this).hasClass(_action))return false;
			$(this).addClass(_action);
			clear($(this),_action);
		});
		
	};
	
	this.amAdd = function(_cname, _action, _fun){
		var _tag = $('.'+_cname);
		_tag.addClass(_action);
		var timeer = setTimeout(function(){
			clearTimeout(timeer);
			_tag.removeClass(_action);
			if(_fun){_fun()};
		}, 1000);
	};

    
	
	/**
	 * 设置选中状态class
	 * _view : 最外面容器类名
	 * _aView : 需要绑定的标签
	 * _show : 需要显示的类名
	 */
	this.setSelected = function(_view, _aView, _show, _obj){			
		function addClass (){
			var _showView = $('.'+_view+' .'+_show);
			var _fView = '.'+_view+' '+_aView;
			var type = 'click';
			if(_obj && _obj.type){
				type = _obj.type;
			};
			$(_fView).live(type, function(){
				if(_showView){
					_showView.removeClass(_show);
				};
				if(_obj && _obj.type == 'change'){
					$(this).parent().parent().parent().addClass(_show);
					_showView = $(this).parent().parent().parent();
				}else{
					$(this).addClass(_show);
					_showView = $(this);
				}
				if(_obj && _obj.fun){
					_obj.fun($(this));
				};
				if($(this).tagName == "a"){
					return false;
				};
			});
			
			this.setIndex = function(index){
				if($(_fView).eq(index)){
					if(_showView){
						_showView.removeClass(_show);
					};
					$(_fView).eq(index).addClass(_show);
					_showView = $(_fView).eq(index);
				}
			};
			
			return this;
		};
		
		return new addClass();
	};
	
	/**
	 *  数量操作事件
	 * 
	 *  参数 obj  json对象
	 *      必填:
	 * 		     obj.add             function
	 * 		     obj.change          function
	 * 		     obj.reduce          function
	 *      选填:
	 * 		     obj.addView         string
	 * 		     obj.changeView      string
	 * 		     obj.reduceView      string
	 * 返回 :
	 * 		数量添加对象
	 * 方法:
	 * 		showNum()  显示数量操作提示信息
	 * 		参数 : 
	 * 				_txt : 显示提示文字
	 * 				_thisView : 显示对象的窗口this
	 */
	this.numChange = function(_view1, _view2, _view3){
		function DiliNum (){
			//数量减少
			$('.'+_view1).click(function(){
				var num = parseInt($(this).parent().next().find('input').val());
				if(num > 1){
					num -= 1;
					$(this).parent().next().find('input').val(num);
				};
			});
			//显示数值
			var inNum = 1;
			$('.'+_view2).focus(function(){
				inNum = parseInt($(this).val());
			}).blur(function(){
				if(parseInt($(this).val())){
					if(parseInt($(this).val()) >= 10000){
						$(this).val(inNum);
					}else{
						$(this).val(parseInt($(this).val()));
					};
				}else{
					$(this).val(inNum);
				};
			});
			//数量添加
			$('.'+_view3).click(function(){
				var num = parseInt($(this).parent().prev().find('input').val());
				if(num < 9999){
					num += 1;
					$(this).parent().prev().find('input').val(num);
				};
			});
		};
		
		function AjaxChange (){
			var _v1 = $('.increment');
			var _v2 = $('.quantity-text');
			var _v3 = $('.decrement');
			if(_view1.addView){
				_v1 = $('.'+_view1.addView);
			} 
			if(_view1.changeView){
				_v2 = $('.'+_view1.changeView);
			} 
			if(_view1.reduceView){
				_v3 = $('.'+_view1.reduceView);
			};
			_v1.live('click', _view1.add);
			_v2.live('change', _view1.change);
			_v3.live('click', _view1.reduce);
			
			this.showNum = function(_txt, _thisView){
				if(_txt == ""){
					$(_thisView).parent().nextAll('.u-emt').animate({'height':'0px'});
				}else{
					$(_thisView).parent().nextAll('.u-emt').html(_txt);
					$(_thisView).parent().nextAll('.u-emt').animate({'height':'auto'});
				}
			};
			
			return this;
		};
		
		var _Nc = null;
		if(_view2){
			_Nc = new DiliNum();
		}else{
			_Nc = new AjaxChange();
		};
		
		return _Nc;
	};
	
	/**
	 * 点击滚动图片
	 * 
	 * 参数 _obj
	 * 		_obj.leftView    左边按钮点击的class
	 * 		_obj.rightView   右边按钮点击的class
	 * 		roollView        滚动视图的class 
	 * 		width  			 单个图片的宽度
	 * 		page             当前滚动的页码
	 * 		lng              滚动个数
	 * 		vLng             滚动视图的总数
	 * 返回
	 * 		Img 滚动的视图对象
	 * 方法
	 * 		无
	 */
	this.rollImages = function(_obj){
		function Img (){
			var config = {
				leftView : 'p-forward',
				rightView: 'p-backward',
				roollView: 'ulmove',
				width: 175,
				page : 0,
				lng : 5,
				vLng : 0,
				islist : false
			};
			config = $.extend(config, _obj);
			$('.'+config.roollView).width(config.vLng * config.width);
			$('.'+config.leftView).click(function(){
				if(config.page < 0){
					config.page += 1;
					am(config.page);
				};
			});
			$('.'+config.rightView).click(function(){
				if((-config.page) < config.lng){
					config.page -= 1;
					am(config.page);
				};
			});
			
			function am(_index){
				$('.'+config.roollView).animate({'left':(_index * config.width)+'px'});
			};
		};
		return new Img();
	};
	
	/**
	 * 解析url方法
	 * 参数
	 * 		paras  必填    解析url参数的 键   key
	 * 		_url   选填    此参数有解析当前参数,无解析当前页面url
	 * 返回:
	 * 		paras 这个key对应的值
	 * 方法:
	 * 		无
	 * 
	 */
	this.request = function(paras, _url){
		var url = ""; 
		if(_url){
			url = _url; 
		}else{
			url = location.href; 
		};
        var paraString = url.substring(url.indexOf("?")+1,url.length).split("&"); 
        var paraObj = {} 
        for (i=0; j=paraString[i]; i++){ 
        	paraObj[j.substring(0,j.indexOf("=")).toLowerCase()] = j.substring(j.indexOf("=")+1,j.length); 
        } 
        	var returnValue = paraObj[paras.toLowerCase()]; 
        if(typeof(returnValue)=="undefined"){ 
        	return ""; 
        }else{ 
        	return returnValue; 
        } 
	};
	
	/**
	 * 输入框添加对象组件
	 * 参数 :
	 * 		_view    绑定输入框视图
	 * 返回 : 
	 * 		AddValue 返回输入框添加对象
	 * 方法:
	 * 		setData 设置绑定视图里面显示的操作数据块
	 * 		参数		
	 * 			data  array   数组对象
	 * 			data[0].txt   显示在数据块的文字
	 * 			data[0].val   显示在数据块的id或者其他重要的值
	 * 
	 */
	this.addValue = function(_view){
		function AddValue (){
			var _p_view = $(_view);
			this.setData = function(data){
				_p_view.html('');
				var _show_view = '';
				var _aData = [];
				for (var i=0; i < data.length; i++) {
				   _show_view += '<div class="a-v-view"><p>'+data[i].txt+'</p><span class="a-v-view-d" inx="'+i+'">×</span></div>';
				   _aData.push(data[i].val);
				};
				_show_view += '<input type="hidden" class="a-v-hidden" value="'+_aData+'" name="'+_p_view.attr('data-name')+'" />';
				_p_view.html(_show_view);
				
				$('.a-v-view-d').click(function(){
					var _index = $(this).attr('inx');
					_index = parseInt(_index);
					_aData.splice(_index, 1);
					$(this).parent().remove();
					_p_view.find('.a-v-hidden').remove();
					var _h_view = '<input type="hidden" class="a-v-hidden" value="'+_aData+'" name="'+_p_view.attr('data-name')+'" />';
					_p_view.append(_h_view);
				});
				
			};
			
			return this;
		};
		
		return new AddValue();
	};
	
	
	/**
	 * 购物车提示信息   
	 * 
	 * 参数:
	 * 	  _v   绑定显示框class
	 * 返回     
	 * 	  obj   对象
	 * 方法 :
	 * 	  obj.load    显示加载loading
	 * 			_this   显示文字框对象
	 * 	  obj.show 	     显示提示文字
	 * 			text  string  为字符时显示文字
	 * 			text  obj     为对象时候
	 * 				text.msg  显示提示文字
	 * 			
	 */
	this.shoppingMsg = function(_v){
		var _view = $('.q-message');
		var _top = 0;
		var _time = null;
		var is = true;
		if(_v){
			_view = $('.'+_v);
		};
		$('.q-close').click(function(){
			_view.hide();
			if(_time){
				clearTimeout(_time);
				_time = null;
			};			
		});
		
		return {
			load : function(_this){
				if(_time){
					clearTimeout(_time);
					_time = null;
				};	
				_view.find('.load').show();
				_view.find('.text').hide();
				var top = $(_this).parent().offset().top;
				var left = $(_this).parent().offset().left;
				_top = top - 55;
				_view.css({"top":_top, "left":left - 50}).css({"height":'50px'});
				_view.show();
				self.amAdd('q-message', 'fadeInDown');	
			},
			show : function(text){
				_time = setTimeout(function(){
					_view.css("display", "none");
					if(_time){
						clearTimeout(_time);
						_time = null;
					};	
				}, 3000);
				_view.stop().css({"height":'75px', 'top':_top-30});
				_view.find('.load').hide();
				_view.find('.text').show();
				_view.css("overflow", "visible");
				_view.find('.text').find('p').css('line-height', '30px');
				if(typeof(text) == "string"){
					_view.find('.text').find('p').eq(0).show();
					_view.find('.text').find('p').eq(1).html('小计金额:<b class="q-total">'+text+'</b>');
				}else{
					_view.find('.text').find('p').eq(0).hide();
					_view.find('.text').find('p').eq(1).html(text.msg).css('line-height', '80px');
				};
			}
		}
	};
	
	
	/**
	 *  随机数 
	 */
	this.randomize = function(b,e){   
	    if(!b && b!=0 || !e){return "?";}   
	    return Math.floor( ( Math.random() * e ) + b );   
	};
	
	/**
	 *  打分组件
	 *  参数
	 * 	   _view : 绑定视图的class
	 * 方法:无
	 * 返回:无
	 */
	this.DiliRaty = function(_view, _xv){
		var _xing = 8;
		if(_xv){_xing = _xv};
		$('.'+_view).find('span').width(_xing * 10);
		$('.'+_view).next().html(_xing/2);
		$('.'+_view).mousemove(function(e){
			var xx = e.originalEvent.layerX || e.originalEvent.x || 0; 
			if(xx){xx = e.clientX - $(this).offset().left;};
			if(xx < 1){return false;};
			xx += '';
			var pageX = 0;
			pageX = parseInt(xx.substring(1,0));
			if(pageX % 2 != 0){
				pageX += 1;
			};
			$(this).find('span').width(pageX * 10);
			$(this).next().html(+pageX/2);
		});
	};
	
	/**
	 *  格子移动动画 
	 */
	this.getPageCoord = function(_view){
		function getOffset(e) {
		    var target = e.target;
		    if (target.offsetLeft == undefined) {
		        target = target.parentNode;
		    }
		    var pageCoord = getPageCoord(target);
		    var eventCoord = {
		        x: window.pageXOffset + e.clientX,
		        y: window.pageYOffset + e.clientY
		    };
		    var offset = {
		        offsetX: eventCoord.x - pageCoord.x,
		        offsetY: eventCoord.y - pageCoord.y
		    };
		    return offset;
		};
		
		function getPageCoord(element) {
		    var coord = {
		        x: 0,
		        y: 0
		    };
		    while (element) {
		        coord.x += element.offsetLeft;
		        coord.y += element.offsetTop;
		        element = element.offsetParent;
		    }
		    return coord;
		};
		
		$(_view).hover(function(e){
			var _this  = $(this), //闭包
			_desc  = _this.find(".end").stop(true, true),
			width  = 103, //取得元素宽
			height = 103, //取得元素高
			left   = (e.offsetX == undefined) ? getOffset(e).offsetX : e.offsetX, //从鼠标位置,得到左边界,利用修正ff兼容的方法
			top    = (e.offsetY == undefined) ? getOffset(e).offsetY : e.offsetY, //得到上边界
			right  = width - left, //计算出右边界
			bottom = height - top, //计算出下边界
			rect   = {}, //坐标对象,用于执行对应方法。
			_min   = Math.min(left, top, right, bottom), //得到最小值
			_out   = e.type == "mouseleave", //是否是离开事件
			spos   = {}; //起始位置
			rect[left] = function (epos){ //鼠从标左侧进入和离开事件
				spos = {"left": -width, "top": 0};
				if(_out){
					_desc.stop().animate(spos, "fast"); //从左侧离开
				}else{
					_desc.css(spos).stop().animate(epos, "fast"); //从左侧进入
				}
			};
		
			rect[top] = function (epos) { //鼠从标上边界进入和离开事件
				spos = {"top": -height, "left": 0};
				if(_out){
					_desc.stop().animate(spos, "fast"); //从上面离开
				}else{
					_desc.css(spos).stop().animate(epos, "fast"); //从上面进入
				}
			};
		
			rect[right] = function (epos){ //鼠从标右侧进入和离开事件
				spos = {"left": left,"top": 0};
				if(_out){
					_desc.stop().animate(spos, "fast"); //从右侧成离开
				}else{
					_desc.css(spos).stop().animate(epos, "fast"); //从右侧进入
				}
			};
		
			rect[bottom] = function (epos){ //鼠从标下边界进入和离开事件
				spos = {"top": height, "left": 0};
				if(_out){
					_desc.stop().animate(spos, "fast"); //从底部离开
				}else{
					_desc.css(spos).stop().animate(epos, "fast"); //从底部进入
				}
			};
			rect[_min]({"left":0, "top":0}); // 执行对应边界 进入/离开 的方法
		});
	};
	
	/**
	 * tab 滑动选中组件
	 * 参数 : 
	 * 		 _obj  对象
	 * 		_obj.rollview     滚动位置显示下标视图对象
	 * 		_obj.view         tab监听对象
	 * 		_obj.offset       视图偏移量
	 * 		_obj.istxtwidth   是否要使用文字宽度
	 * 		_obj.bias         文字宽度计算偏差
	 * 返回:无
	 * 方法:无
	 */
	this.tabRolling = function(_obj){
		var config = {
			rollview : 'u-buoy',
			view : 'u-list li',
			offset : 20,
			istxtwidth : false,
			bias : 2
		};
		config = $.extend(config, _obj);
		var width = 0;
		var left = 0;
		$('.'+config.view).hover(function(e){
			left = $(this).position().left;
			width = $(this).width()+config.offset;
			if(config.istxtwidth){
				width = $(this).find('a').width();
				left = left + (($(this).width() - $(this).find('a').width()) / 2) + (config.offset / 2) + config.bias;
			};
			$('.'+config.rollview).stop().animate({"left":left, "width":width}, 300);
		});
	};
	
	return this;
})();	

/**
 * zzf 
 * 2014-6-19
 * 参数 : 
 * 		unitlen : 滚动距离
 * 		page    : 滚动数量
 * 		direction : 滚动方向
 * 		time    :  滚动间隔时间
 * 		point   :  滚动的视图任意一个小点class
 * 		pointPage : 操作下面小点的视图的大小
 * 		pointView : 滚动的视图任意一个小点的父窗口
 * 方法:
 * 		actionLeft   向前显示
 * 		actionRight  想后显示
 */
(function($){	
	$.fn.dilislider = function(options){
		var settings ={
			affect: 'scrollx', //效果  有scrollx|scrolly|fade|none
			speed: 500, //动画速度
			space: 6000, //时间间隔
			auto: true, //自动滚动
			trigger: 'mouseover', //触发事件 注意用mouseover代替hover
			conbox: 'ul', //内容容器id或class
			ctag: 'li', //内容标签 默认为<a>
			switcher: '.tab-a', //切换触发器id或class
			stag: 'a', //切换器标签 默认为a
			current:'on', //当前切换器样式名称
			rand:false //是否随机指定默认幻灯图片
		};
		//前版本兼容
		if(options.point){
			options.switcher = options.point.split(" ")[0];
			options.stag = options.point.split(" ")[1]
		};
		settings = $.extend({}, settings, options);
		var index = 1;
		var last_index = 0;
		var $conbox = $(this).find(settings.conbox),$contents = $conbox.find(settings.ctag);
		var $switcher = $(settings.switcher),$stag = $switcher.find(settings.stag);
		if(settings.rand) {index = Math.floor(Math.random()*$contents.length);slide();}
		if(settings.affect == 'fade'){
			$.each($contents,function(k, v){(k === 0) ? $(this).css({'position':'absolute','z-index':9}):$(this).css({'position':'absolute','z-index':1,'opacity':0});});
		};
		function slide(){if (index >= $contents.length) index = 0;
			$stag.removeClass(settings.current).eq(index).addClass(settings.current);
			switch(settings.affect){case 'scrollx':
					$conbox.width($contents.length*$contents.width());
					$conbox.stop().animate({left:-$contents.width()*index},settings.speed);
					break;
				case 'scrolly':
					$contents.css({display:'block'});
					$conbox.stop().animate({top:-$contents.height()*index+'px'},settings.speed);
					break;
				case 'fade':
					$contents.eq(last_index).stop().animate({'opacity': 0}, settings.speed/2).css('z-index',1)
							 .end()
							 .eq(index).css('z-index',9).stop().animate({'opacity': 1}, settings.speed/2)
					break;
				case 'none':
					$contents.hide().eq(index).show();
					break;
			}
			last_index = index;
			index++;
		};
		if(settings.auto) var Timer = setInterval(slide, settings.space);
		$stag.bind(settings.trigger,function(){_pause()
			index = $(this).index();
			slide();
			_continue()
		});
		$conbox.hover(_pause,_continue);
		function _pause(){
			clearInterval(Timer);
		}
		function _continue(){
			if(settings.auto)Timer = setInterval(slide, settings.space);
		}	
	},//聚焦延时事件
	$.fn.dhover = function(_fun, _sfun){
		var trigger = null;
		var self = this;
		$($(this).selector).hover(function(e){
			var thishover = this;
			this.ufun = _fun;
			trigger = setTimeout(function(){
				thishover.ufun(e);
			}, 200);
		}, function(e){
			this.sfun = _sfun;
			clearTimeout(trigger);
			this.sfun(e);
		});
	}
})(jQuery);

/**
 * jquery 超过 “e”高度出Y轴滚动条 事件
 */
function overflow_y(obj, e, i){
	var height_obj;
	if(!i){
		height_obj = obj.height();
	}else{
		height_obj = i;
	}
	
	if(height_obj < e ){
		obj.height(height_obj);
		obj.css("overflow-y","auto");
	}else{
		obj.removeAttr("style");
	}
};