index.js 1.92 KB
/**
 * 分类
 * @author: xuqi<qi.xu@yoho.cn>
 * @date: 2015/10/14
 */

var $ = require('jquery'),
    Hammer = require('yoho.hammer');

var $nav = $('.category-nav'),
    $categoryContainer = $('.category-container'),
    $contents = $categoryContainer.children('.content'),
    $subLevelItem = $('.sub-level li');

var $curContent = $contents.not('.hide'),
    $curClickSubLevel;

var navHammer, ccHammer;

//初始化container高度
(function() {
    var $header = $('.yoho-header'),
        $search = $('#search-input');

    var h = $(window).height() - $header.outerHeight() - $search.outerHeight() - $nav.outerHeight();

    $categoryContainer.css('min-height', h);

    $contents.height(h);
}());

$('#search-input').focus(function() {
    $(this).blur();
});

navHammer = new Hammer($nav[0]);
navHammer.on('tap', function(e) {
    var $this = $(e.target).closest('li'),
        index = $this.index();

    if ($this.hasClass('focus')) {
        return;
    }

    $nav.find('li.focus').removeClass('focus');
    $this.addClass('focus');

    $curContent.addClass('hide');
    $curContent = $contents.eq(index).removeClass('hide');
});

ccHammer = new Hammer($categoryContainer[0]);
ccHammer.on('tap', function(e) {
    var $this = $(e.target),
        $subLevel,
        $cur, index;

    $cur = $this.closest('.p-level-item');
    if ($cur.length > 0) {
        index = $cur.index();
        $subLevel = $this.closest('.content').find('.sub-level');

        if ($this.hasClass('focus')) {
            return;
        }

        $this.closest('.primary-level').children('.focus').removeClass('focus');
        $this.addClass('focus');

        $subLevel.not('.hide').addClass('hide');
        $subLevel.eq(index).removeClass('hide');
    }
});

$subLevelItem.on('touchstart', function() {
    $subLevelItem.removeClass('highlight');
    $(this).addClass('highlight');
}).on('touchend touchcancel', function() {
    $(this).removeClass('highlight');
});