header.js 1.5 KB
/**
 * 公共头部
 * @author: yyq<yanqing.yang@yoho.cn>
 * @date: 2016/7/1
 */
var $ = require('yoho-jquery');

var $searchWrap = $('.search-wrapper'),
    $searchForm = $('#search-form'),
    $searchKey = $('#search-key'),
    $clearInput = $('.clear-input');

var delayer;

$('.yoho-group a').hover(function() {
    var data = $(this).data();

    $(this).text(data.cn);
}, function() {
    var data = $(this).data();

    $(this).text(data.en);
});

$('.contain-third').on({
    mouseenter: function() {
        var $thirdWrapper = $(this).children('.third-nav-wrapper');

        delayer = setTimeout(function() {
            $thirdWrapper.show();
        }, 200);
    },
    mouseleave: function() {
        var $thirdWrapper = $(this).children('.third-nav-wrapper');

        if (delayer) {
            clearTimeout(delayer);
        }
        $thirdWrapper.hide();
    }
});

$('.search-entry').click(function() {
    $searchWrap.show();
});

$searchForm.on('keyup', '#search-key', function(e) {
    var key = $searchKey.val();

    if (e.which === 13) {
        if (key) {
            $searchForm.submit();
        }
    } else {
        $searchKey.val(key.replace(new RegExp('\'', 'gm'), ''));
    }

    if (key) {
        $clearInput.show();
    } else {
        $clearInput.hide();
    }
});

$clearInput.click(function() {
    $searchKey.val('');
    $(this).hide();
});

$(document).click(function(e) {
    var $tar = $(e.target);

    if (!$tar.closest('.search-entry').length) {
        $searchWrap.hide();
    }
});