Authored by xuqi

yoho filter

@@ -89,6 +89,32 @@ @@ -89,6 +89,32 @@
89 } 89 }
90 } 90 }
91 91
  92 +### 筛选
  93 +
  94 + {
  95 + filter: {
  96 + classify: [
  97 + {
  98 + title: '性别',
  99 + default: true/false,
  100 + name: '全部性别',
  101 + dataType: 'gender', //查询时字段名
  102 + subs: [
  103 + {
  104 + dataId: 0,
  105 + name: '全部性别',
  106 + chosed: true //是否当前选中
  107 + },
  108 + ...
  109 + ]
  110 + },
  111 + {
  112 + ...
  113 + }
  114 + ]
  115 + }
  116 + }
  117 +
92 ## 首页 118 ## 首页
93 119
94 { 120 {
  1 +/**
  2 + * 筛选JS
  3 + * 暴露三个接口:注册回调、显示filter、隐藏filter
  4 + * @author: xuqi<qi.xu@yoho.cn>
  5 + * @date: 2015/10/19
  6 + */
  7 +
  8 +var $ = require('yoho.zepto');
  9 +
  10 +var $filter = $('#yoho-filter');
  11 +
  12 +var $classify = $filter.find('.classify'),
  13 + $subClassify = $filter.find('.sub-classify');
  14 +
  15 +var cbFn;
  16 +
  17 +//隐藏筛选界面
  18 +function hideFilter() {
  19 + $filter.addClass('hide');
  20 +}
  21 +
  22 +//显示筛选界面
  23 +function showFilter() {
  24 + $filter.removeClass('hide');
  25 +}
  26 +
  27 +//注册sub-classify点击后的回调
  28 +function registerCbFn(cb) {
  29 + cbFn = cb;
  30 +}
  31 +
  32 +//初始化sub高度与classify高度一致
  33 +$subClassify.height($classify.height());
  34 +
  35 +//设置完高度后显示sub并设置选中
  36 +$classify.children(':first-child').addClass('active'); //T:不在HTML中使用{{#if @first}}active{{/if}}来初始化active为避免sub设置高度时的闪烁
  37 +
  38 +//classify switch
  39 +$classify.delegate('.classify-item', 'touchstart', function() {
  40 + var $this = $(this);
  41 +
  42 + if ($this.hasClass('active')) {
  43 + return;
  44 + }
  45 +
  46 + $this.siblings('.active').removeClass('active');
  47 +
  48 + $this.addClass('active');
  49 +});
  50 +
  51 +//点击Mask隐藏筛选界面
  52 +$filter.children('.filter-mask').click(function() {
  53 + hideFilter();
  54 +});
  55 +
  56 +$subClassify.delegate('li', 'click', function(e) {
  57 + var $this = $(this),
  58 + id = $this.data('id');
  59 +
  60 + var $sub = $this.closest('.sub-classify');
  61 +
  62 + var $shower = $sub.siblings('.shower');
  63 +
  64 + var html, shower;
  65 +
  66 + e.stopPropagation();
  67 +
  68 + if ($this.hasClass('chosed')) {
  69 + return;
  70 + }
  71 +
  72 + $sub.children('.chosed').removeClass('chosed');
  73 + $this.addClass('chosed');
  74 +
  75 + html = $.trim($this.html());
  76 +
  77 + shower = $.trim($shower.html());
  78 +
  79 + $shower.html(
  80 + shower.substring(0, shower.indexOf('</span>') + 7) + //拆分出shower的title
  81 + html.substring(0, html.indexOf('<i')) //拆分选中筛选值
  82 + );
  83 +
  84 + if ($this.index() === 0) {
  85 + $shower.addClass('default');
  86 + } else {
  87 + $shower.removeClass('default');
  88 + }
  89 +
  90 + if (cbFn) {
  91 + cbFn({
  92 + type: $sub.data('type'),
  93 + id: id
  94 + });
  95 + }
  96 +
  97 + hideFilter();
  98 +});
  99 +
  100 +
  101 +exports.showFilter = showFilter;
  102 +
  103 +exports.hideFilter = hideFilter;
  104 +
  105 +exports.registerCbFn = registerCbFn;
  1 +.filter-mask, .filter-body {
  2 + position: absolute;
  3 + left: 0;
  4 + right: 0;
  5 + top: 0;
  6 +}
  7 +
  8 +.filter-mask {
  9 + height: 100%;
  10 + background: rgba(0,0,0,0.1);
  11 +}
  12 +
  13 +.filter-body {
  14 + position: relative;
  15 + background: #fff;
  16 + color: #000;
  17 + cursor: pointer;
  18 + font-size: 14px;
  19 +
  20 + .classify {
  21 + width: 50%;
  22 +
  23 + > li {
  24 + background: #f8f8f8;
  25 + height: 60px;
  26 + line-height: 60px;
  27 +
  28 + > * {
  29 + box-sizing: border-box;
  30 + }
  31 +
  32 + &.active {
  33 + background: #fff;
  34 + }
  35 +
  36 + .shower {
  37 + padding-left: 20px;
  38 + width: 100%;
  39 + overflow: hidden;
  40 + white-space: nowrap;
  41 + text-overflow: ellipsis;
  42 + }
  43 +
  44 + .default {
  45 + color: #999;
  46 + }
  47 +
  48 + .title {
  49 + float: left;
  50 + color: #000;
  51 + }
  52 + }
  53 + }
  54 +
  55 + .sub-classify {
  56 + position: absolute;
  57 + display: none;
  58 + width: 50%;
  59 + padding-left: 15px;
  60 + left: 50%;
  61 + top: 0;
  62 + overflow: auto;
  63 +
  64 + > li {
  65 + height: 60px;
  66 + line-height: 60px;
  67 + border-bottom: 1px solid #e6e6e6;
  68 + overflow: hidden;
  69 + white-space: nowrap;
  70 + text-overflow: ellipsis;
  71 +
  72 + &:last-child {
  73 + border-bottom: none;
  74 + }
  75 + }
  76 +
  77 + .chosed-icon {
  78 + display: none;
  79 + }
  80 +
  81 + .chosed .chosed-icon {
  82 + display: inline;
  83 + }
  84 + }
  85 +
  86 + .active > .sub-classify {
  87 + display: block;
  88 + }
  89 +}
@@ -111,4 +111,4 @@ a { @@ -111,4 +111,4 @@ a {
111 @include border-radius(10px); 111 @include border-radius(10px);
112 } 112 }
113 113
114 -@import "layout/header", "layout/footer", "good", "passport/index", "guang/index", "home/index", "category/index", "product/index", "index/index";  
  114 +@import "layout/header", "layout/footer", "good", "filter", "passport/index", "guang/index", "home/index", "category/index", "product/index", "index/index";
  1 +{{# filter}}
  2 + <div id="yoho-filter">
  3 + <div class="filter-mask"></div>
  4 + <div class="filter-body">
  5 + <ul class="classify">
  6 + {{#each classify}}
  7 + <li class="classify-item">
  8 + <p class="shower{{#if default}} default{{/if}}">
  9 + <span class="title">{{title}}:</span>
  10 + {{name}}
  11 + </p>
  12 + <ul class="sub-classify" data-type={{dataType}}>
  13 + {{# subs}}
  14 + <li {{#if chosed}}class=chosed{{/if}} data-id={{dataId}}>
  15 + {{name}}
  16 + <i class="iconfont chosed-icon">&#xe617;</i>
  17 + </li>
  18 + {{/ subs}}
  19 + </ul>
  20 + </li>
  21 + {{/each}}
  22 + </ul>
  23 + </div>
  24 + </div>
  25 +{{/ filter}}