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

var $ = require('jquery');

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

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

(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();
});
$nav.bind('contextmenu', function(e) {
    return false;
});
$('.category-container').bind('contextmenu', function(e) {
    return false;
});

$nav.on('touchend touchcancel', 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');
});

$categoryContainer.on('touchend', function(e) {
    var $this = $(e.target),
        $subLevel,
        $cur, index;

    $cur = $this.closest('.p-level-item');

    //第一次单击右则空白处,出现冒泡
    if ($(e.target).closest('.sub-level').length <=0) {
        e.preventDefault();
    }

    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');
    }
});

$categoryContainer.find('.primary-level').on('touchstart touchend touchcancel', 'li', function() {
    $primaryItem.removeClass('highlight');
    $(this).addClass('highlight');
}).on('touchend touchcancel', 'li', function() {
    $(this).removeClass('highlight');
});

$categoryContainer.find('.sub-level').on('touchstart', 'li', function() {
    $subLevelItem.removeClass('highlight');
    $(this).addClass('highlight');
}).on('touchend touchcancel', 'li', function() {
    $(this).removeClass('highlight');
});

$nav.on('touchstart', 'li', function() {
    $nav.find('li').removeClass('bytouch');
    $(this).addClass('bytouch');
}).on('touchend touchcancel', 'li', function() {
    $nav.find('li').removeClass('bytouch');
});