public320.js 4.18 KB
/*
 * @description: h5 js
 * @author: chenglong.wang@yoho.cn
 * @date: 2015/1/13
 */

var $       = require('jquery');
var swiper  = require('yoho.swiper');
var Hammer  = require('hammerjs');
var tools   = require('yoho-tools');

var mulLine = require('./plugin/mlellipsis');
var box     = require('./plugin/box');
var tips    = require("./plugin/tips");

              require('lazyload');
              require("./plugin/login");

//public banner
var publicSwiper = new swiper('.public-banner',{
    mode:'horizontal',
    loop: true,
    pagination: '.pagination',
    grabCursor: true,
    paginationClickable: true,
    autoplay: 5000,
    slideElement: 'li',
    onSlideChangeEnd: function () {
        publicSwiper.startAutoplay();
        $(".relazy").lazyload({threshold: 100});
    }
});

//侧边栏展开、折叠
var isExpand = true;
var oNav = $('.nav-bar');
var wrap = $('#mobile');
var side = $('#side-bar');
var wW = $(window).width();

var nav = new Hammer(document.getElementById('mobile-wrap'),{time:50});
nav.on('tap',function (event) {
    if ($(event.target).is(oNav)) {
        side.css('visibility','visible');
        wrap.addClass('menuOpen').css({
            'height':'100%',
            'min-height':'580px',
            'overflow-y':'hidden',
            'overflow-x':'hidden'
        });
        $('.header-download').css('left',0);
        isExpand = false;
        event.preventDefault();

    }else {
        if (!isExpand) {
            wrap.removeClass('menuOpen').css({
                'height':'auto',
                'overflow-y':'auto',
                'overflow-x':'hidden'
            });
            setTimeout(function () {
                side.css('visibility','hidden');
                $('.header-download').css({
                    'left':(wW-320)/2
                });
            },500);
        }
    }
});
var oLogin = $(".side-bar-header");
oLogin.login();
oLogin.click(function () {
    $(this).login("loginBoxAnimate");
});


//关闭顶部浮层
$('.tip-c').click(function () {
    $('.header-download-wrap').remove();
});

//滚动到顶部
var top = new Hammer(document.getElementById('back-top'));
top.on('tap',function () {
    $('#mobile').animate({scrollTop: 0});
});

//懒加载
$(".lazy").lazyload({threshold: 100});

//rss
var oRss = $(".rss input");
if (oRss.length>0) {
    $(".rss-btn").click(function(){
        if(oRss.val()==='') {
            box.alert(tips.getTips("emailEmpty"));
            return;
        }
        if(!tools.IYOHO_isEmail(oRss.val())) {
            box.alert(tips.getTips("emailError"));
            return;
        }
        window.open( '/passport/subscribe/index?email='+oRss.val());
    });
    oRss.keydown(function(event){
        if(event.keyCode===13) {
            $(".rss-btn").trigger("click");
        }
    });
}

//截字
(function () {
    var items=$(".home-content li");
    if(items.length > 0) {
        var detailStr = "";
        items.each(function(index,obj){
            detailStr = $(obj).find(".a-detail").text().substring(0, 58) + " ...";
            $(obj).find(".a-detail").text( detailStr.replace(/[a-zA-Z]+?\s{1}\.\.\.\B/, " ...") );
        });
        items = null;
    }
})();
//获取设备类型
(function (){
    var sUserAgent = navigator.userAgent.toLowerCase();
    var bIsIphoneOs = sUserAgent.match(/iphone os/i) == "iphone os";
    var bIsAndroid = sUserAgent.match(/android/i) == "android";
    var bIsweixin = sUserAgent.match(/MicroMessenger/i) == 'micromessenger';
    var app = $('.yohoboy-download-app');
    var downBtn = $('.home-download').find('.down-btn');
    if(bIsIphoneOs){
        app.attr('href',app.attr('ios-href'));
        app.attr('onclick','openApp("'+app.attr('data-ios')+'")');
        downBtn.each(function (index) {
            $(this).attr('href',$(this).attr('data-ios'));
        });
    }
    if (bIsAndroid) {
        downBtn.each(function (index) {
            $(this).attr('href',$(this).attr('data-android'));
        });
    }
    if (bIsweixin) {
        $('.header-download-wrap').remove();
    }
})();

function openApp(link) {
    var clickedAt = +new Date();
    setTimeout(function(){
        if (+new Date() - clickedAt < 2000) {
            window.location.href = link;
        }
    }, 500);
}