header.js 1.84 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();
});

$searchKey.keyup(function(e) {
    var key = $searchKey.val();

    if (e.which === 13) {
        if (key) {
            $searchForm.submit();
        }
    }

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

// ie8输入框提示特殊处理
if ($.browser && $.browser.msie && parseInt($.browser.version, 10) === 8) {
    $searchKey.focus(function() {
        var key = $searchKey.val();

        if (key === 'search') {
            $searchKey.val('').css('color', '#fff');
        }
    }).blur(function() {
        var key = $searchKey.val();

        if ($.trim(key) === '') {
            $searchKey.val('search').css('color', '#aaa');
        }
    });
}

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

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

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