common.js 3.9 KB
/*
 * @description: common js
 * @author: lore-w
 * @date: 2015-4-23
 */

var $ = require('jquery'),
    swiper = require('swiper-mobile'),
    tools = require('yoho-tools'),
    mulLine = require('./plugin/mlellipsis'),
    box = require('./plugin/box'),
    tips = require("./plugin/tips");

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

// 初始化页面
exports.init = function () {

    var oNav = $('.girl-nav');

    var isTrue = true,
        top = 66;

    $(window).scroll(function () {
        setTimeout(function () {

            if (isTrue) {
                top = 66;
            }else{
                top = 0;
            }

            var scrollTop = $(this).scrollTop();
            if (scrollTop > 44) {
                oNav.css({
                    position: 'fixed',
                    top: top
                });
            } else {
                oNav.css({
                    position: 'static',
                    top: 'initial'
                });
            }
        },100)
    });

    // nav
    var navSwiper = new swiper('.girl-nav', {
        grabCursor: true,
        slidesPerView: 'auto',
        slideElement: 'li',
        hashnav: true
    });

    // channel page banner
    var channelSwiper = new swiper('.banner .swiper-container', {
        loop: true,
        pagination: '.pagination',
        grabCursor: true,
        paginationClickable: true,
        autoplay: 5000,
        slideElement: 'li',
        lazyLoading: true,
        lazyLoadingInPrevNext: true
    });

    // download app
    var downBtn = $('.home-download').find('.down-btn');

    if (downBtn.length > 0) {
        if (getDeviceType().isIphone) {
            downBtn.each(function (index) {
                $(this).attr('href', $(this).attr('data-ios'));
            });
        }

        if (getDeviceType().isAndroid) {
            downBtn.each(function (index) {
                $(this).attr('href', $(this).attr('data-android'));
            });
        }
    }

    // back to top
    $('.come-back').bind('tapone', function () {
        $('body').animate({scrollTop: 0});
    });

    var oLogin = $(".login");
    oLogin.login();
    oLogin.click(function () {
        $(this).login("loginBoxAnimate");
    });


    //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;
        }
    })();

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


};

/*
 * @description: 获取设备类型
 * @param: none
 * @return: JSON
 */
function getDeviceType() {

    var sUserAgent = navigator.userAgent.toLowerCase(),

        bIsIphoneOs = sUserAgent.match(/iphone os/i) == "iphone os",
        bIsAndroid = sUserAgent.match(/android/i) == "android",
        bIsWeixin = sUserAgent.match(/MicroMessenger/i) == 'micromessenger';

    return {
        isIphone: bIsIphoneOs,
        isAndroid: bIsAndroid,
        isWeixin: bIsWeixin
    }
}

exports.getDeviceType = getDeviceType;