brand.js 3.13 KB
/**
 *  品牌一览
 * @author: liangzhifeng<zhifeng.liang@yoho.cn>
 * @date: 2015/10/23
 */

var $ = require('jquery'),
    Hammer = require('yoho.hammer'),
    Swiper = require('yoho.iswiper'),
    lazyLoad = require('yoho.lazyload');

var swiper,
    $brandList = $('.brand-list');

var searchH = $('.newbrand-search').outerHeight(),
    headerH = $('.yoho-header').outerHeight(),
    brandSwipe = parseInt(searchH) + parseInt(headerH) - 1;

var myHammer, searchBtnHammer, clearTextHammer;

var $rightBarCon = $('#right-bar .con').find('b').unbind();

swiper = new Swiper('.swiper-container', {
    lazyLoading: true,
    loop: true,
    autoplay: 3000,
    pagination: '.swiper-pagination .pagination-inner'
});

lazyLoad($('img.lazy'));
$('.yoho-header').css({
    'z-index': 2,
    position: 'fixed'
});
$('.banner-top').css('padding-top', '90px');

if ($brandList.length > 0) {
    $(window).scroll(function() {
        var scrTop = $(window).scrollTop();

        $('.brand-list').each(function() {
            var offTop = $(this).offset().top - brandSwipe;

            if (scrTop >= offTop) {
                $brandList.find('.title-bar').css('position', 'static');
                $(this).find('.title-bar').css({
                    position: 'fixed',
                    top: brandSwipe
                });
            } else {
                $(this).find('.title-bar').css('position', 'static');
            }
        });
    });
}

if ($rightBarCon[0]) {
    myHammer = new Hammer($rightBarCon[0]);
    myHammer.on('tap', function(e) {
        var index = $rightBarCon.index();

        if ($('.bar-' + index).size() > 0) {
            document.body.scrollTop = parseInt($('.bar-' + index)[0].offsetTop) - parseInt(brandSwipe - 1);
        }
        e.srcEvent.stopPropagation();
    });
}

function searchResult(type) {
    var keyword = $('#keyword').val(),
        gender = $('#gender').val();

    $.get('/brands/search', {
        keyword: keyword,
        gender: gender,
        type: type
    }, function(html) {
        $('.search-result').html(html);

    });
}

$('#keyword').on('keyup', function() {
    searchResult(0);
}).on('keydown', function() {
    $(this).closest('.search-box').css('width', '11.25rem');
    $('.search-action').show();
}).on('blur', function() {
    $(this).closest('.search-box').css('width', '12.5rem');
    $('.search-action').hide();
});

// $('#search-btn').on('tap', function() {
//     searchResult(1);
// });

searchBtnHammer = new Hammer($('#search-btn')[0]);
searchBtnHammer.on('tap', function(e) {
    searchResult(1);
    e.srcEvent.stopPropagation();
});

// $('.clear-text').on('tap', function() {
//     $('#keyword').val('').trigger('input');
// });

clearTextHammer = new Hammer($('.clear-text')[0]);
clearTextHammer.on('tap', function(e) {
    $('#keyword').val('').trigger('input');
    e.srcEvent.stopPropagation();
});

$('form.search-box').on('submit', function() {
    return false;
});

$('.clear-history').on('touchstart', function() {

    // setcookie('h_brands', '', {
    //     expire: -1,
    //     path: '/',
    //     domain: '.m.yohobuy.com'
    // });
    $('#history-keyword').remove();
    $(this).hide();
});