search.js 5.91 KB
function filterAction(options){
		var params = new Object();
		options = options || {};
		params['brand']=options.brand||null;
		params['color']=options.color||null;
		params['msort']=options.msort||null;
		params['misort']=options.misort||null;
		params['day']=options.day||null;
		params['discount']=options.discount||null;
		params['price']=options.price||null;
		params['size']=options.size||null;
		params['gender']=options.gender||null;
		params['query'] = options.query||null;
		params['specialsale_id'] = options.specialsale_id||null;
		var url = options.url||null;
		var okbtn = options.okbtn || $('.filterBox .ok');
		var style = $('.box')[0].style;
		$('p.nav').bind("click",function(){
				showNavList($(this));
		})
		
		$('.selectCon p').bind('click', function(){
			  selectAction($(this));
			
		});
		
		//点击清空按钮事件
		$('.filterBox .clear').bind("click",function(){
			$('.filterBox p').removeClass('selected');
			$('.filterBox p.all').addClass('selected');
			$('.brand p.all').removeClass('selected');	
			clearBrand();
			$('p.nav .selected').text('全部');
			deleteALl();
			goUrl();
		})
		
		function deleteALl(){
			delete params['brand'];
			delete params['color'];
			delete params['msort'];
			delete params['misort'];
			delete params['day'];
			delete params['discount'];
			delete params['price'];
			delete params['size'];
			delete params['query'];
		}
		
		//点击p.nav事件
		function showNavList(ele){
			ele.parent().children('.selectCon').toggleClass('hide');
			ele.toggleClass('show').children().last().toggleClass('show');
		}
		
		//点击小分类选选中状态;
		function selectAction(ele){
			var cl = ele.parent().parent().attr('class');
			var cl2 =ele.attr('name')
			var text = '全部';
			if(cl!="brand"){
				ele.parent().children().removeClass('selected');
				ele.addClass('selected');
				if(!ele.hasClass('all')){
					ele.css('background-position','235px');
					text = ele.text();	
				}
				showNavList(ele.parent().parent().children().first());
				params[cl2] = ele.attr('value');
			}else{
				var text = selectBrand(ele);

			}
			changeAction(cl,text);
			
			
		}
		
		//修改选中条件
		function changeAction(cl,text){
			$('li.'+cl+' span.selected').text(text)
		}

		//选择品牌条件		
		function selectBrand(ele){
			var brands='';
			var span = ele.children();
			var first = span.first();
			var second = span.eq(1)
			var cl = first.attr('class');
			if(!ele.hasClass('all')){
				if(cl == "check"){
					first.removeClass("check").addClass('uncheck');
					second.css('color','#767676');
				}else{
					first.removeClass("uncheck").addClass('check');
					second.css('color','#f00');
					
				}
			}
			var check = $('.brand span.check');
			var length = check.length;
			if(length>0){
				 if(length > 1){
                     	 var text = check.eq(0).parent().children().last().text()+'等'+length+'个';
	             }else{
	                     var text = check.eq(0).parent().children().last().text();
	             }
				
				
			}else{
				
				var text = '全部';
			}
			$('.brand p.all').children().first().text("已选择:"+length+"个(可多选)")
			return text;
		}
		
		//清楚品牌筛选条件
		$('.brand p.all .delete').click(function(){
			clearBrand();
			showNavList($('ul li.brand p.nav'));
			
		})
		//清楚品类筛选条件
		$('.category p.all').click(function(){
			delete params['msort'];
			delete params['misort'];
		})
		
		//点击大分类清除小分类
		$('.category p.msort ').click(function(){
			delete params['misort'];
		})
			
		//清除品牌条件方法
		function clearBrand(){
			$('.brand .selectCon span.check').removeClass('check').addClass('uncheck');	
			$('.brand .selectCon span').css('color','#767676');
			$('.brand p.all').children().first().text("已选择:0个(可多选)");
			delete params['brand'];
		}
		
		function getSelectBrand(){
			var p = $('ul li.brand .selectCon p');
			params['brand'] = ''
			$.each(p,function(){
				if($(this).children().first().hasClass('check')){
					params['brand']+=$(this).attr('value');
					params['brand']+=',';
				}
			})
			return params['brand'];
		}
		//刷选面板移动事件
		function scoll(width){
		 style.webkitTransitionDuration =
	            style.MozTransitionDuration =
	                style.msTransitionDuration =
	                    style.OTransitionDuration =
	                        style.transitionDuration =  '400ms';
		 style.webkitTransform = 'translate(' + width + 'px,0)' + 'translateZ(0)';
	        style.msTransform =
	            style.MozTransform =
	                style.OTransform = 'translateX(' + width + 'px)'; 
		}
		
		//显示筛选面板事件
		$("ul li.filter").on("click",function(){
			scoll(-275);
			createCover();
		});
		
		//关闭筛选面板事件
		$(".close").on("click",function(){
			delCover();
			scoll(0);
		})
		
		//当滚动条滚动时
		$(window).bind("scroll", function(){ 
			var h = $(document).scrollTop(); 
			if(h>45){
				$('.filterHeader').css('top', h-45+'px');
			}else{
				
				$('.filterHeader').css('top', '0px');
			}
		})
		
		//点击确定按钮事件
		okbtn.on('touch click',function(){
				
				goUrl();
		});
		//生成一蒙版
		function createCover(){		
			var boarddiv = "<div class='cover' style='background:#000;width:100%;height:100%;z-index:999;position:absolute;top:0;opacity: 0.5;filter:alpha(opacity=50);-moz-opacity:0.5;-khtml-opacity: 0.5; '></div>";
			$('.box').append(boarddiv);
			var cover = $('.box .cover');
			cover.on("click",function(){
				delCover();
				scoll(0);
			});
		}
		//删除蒙版
		function delCover(){	
			var cover = $('.box .cover');
			cover.remove();
		}
		
		function goUrl(){
			
			var query = '';
			getSelectBrand();
			for (var p in params) {
				if (params[p]) {
					query += '&' + p + '=' + params[p];
				}
			}
			if(url == null){
					location.href = 'http://'+window.location.host+window.location.pathname+'?'+query;
			}else{
					location.href = 'http://'+url+'?'+query;
			}
		}
		
	}