home.js 7.24 KB
/**
 * 文件功能描述           :首页JS
 * 使用方法                 :直接use
 * author       :wei.wang@yoho.cn
 * date         :2015-4-23
 */

var $ = require("jquery"),
    mlellipsis = require("mlellipsis");
require("lazyload");
var Swiper = require("yoho-idangerous.swiper"),
    tips = require("yoho-tips"),
    box = require("yoho-box");
var isFixed = false,
    pop = null,
    popTop = null,
    popLeft = null,
    limitTop = null,
    stopTop = null,
    stopLeft = null;

var clientWidth = $(window).width();


//住显示区域行数计算
var totalLineNum = 7,
    maxTitleStrNum = 45,
    maxDetailStrNum = 22,
    citems = $(".item-text-area"),
    titleLineNum = 0;
    subTitleLineNum = 0;
    detailLineNum = 0;
    detailStr = "";
    self = null;

mlellipsis.init();
//pop=============================================================================================
if ($(".pop-container")[0] != null) {
    popTop = $(".pop-container").offset().top;
    popLeft = $(".content").offset().left + $(".content").width() - $(".pop-container").width();
    limitTop = $(".pager-content-default").offset().top + $(".pager-content-default").height() + 2 - $(".pop-container").height();
    stopTop = $(".pager-content-default").offset().top - $(".pop-container").height();
    stopLeft = $(".bottom-item-container").offset().left + $(".bottom-item-container").width() - $(".pop-container").width();
        pop = new Swiper('.swiper-container3', {
            loop: true,
            autoplay: 5000,
            autoStopPlay: false,
            paginationClickable: true,
            onSlideChangeEnd: function() {
                pop.startAutoplay();
                $(".swiper-container3 .lazy").lazyload();
            }
        });

    $('.prev-button').bind('click', function(e) {
        e.preventDefault();
        pop.swipePrev();
    });
    $('.next-button').bind('click', function(e) {
        e.preventDefault();
        pop.swipeNext();
    });

    if (limitTop - popTop > $(".pop-container").height()) {
        $(window).bind("scroll", function() {
            if ($(document).scrollTop() >= popTop &&
                isFixed === false &&
                $(document).scrollTop() < limitTop &&
                clientWidth > 768) {
                isFixed = true;
                $(".pop-container-bottom").attr("class", "pop-container");
                $(".pop-container").css({
                    position: "fixed",
                    top: 0,
                    left: popLeft
                });
            } else if ($(document).scrollTop() > limitTop && clientWidth > 768) {
                $(".pop-container").removeAttr("style");
                if ($(".swiper-container2")[0] != null) {
                    $(".pop-container").css({
                        "border-top": 0
                    });
                }
                $(".pop-container").attr("class", "pop-container-bottom");
                isFixed = false;
            } else if (isFixed === true && $(document).scrollTop() < popTop && clientWidth > 768) {
                isFixed = false;
                $(".pop-container").removeAttr("style");
                if ($(".swiper-container2")[0] != null) {
                    $(".pop-container").css({
                        "border-top": 0
                    });
                }

            }
        });
    }

    window.onresize = function() {
        clientWidth = $(window).width();
        popTop = $(".pop-container").offset().top;
        popLeft = $(".content").offset().left + $(".content").width() - $(".pop-container").width();
        limitTop = $(".pager-content-default").offset().top + $(".pager-content-default").height() + 2 - $(".pop-container").height();
        stopTop = $(".pager-content-default").offset().top - $(".pop-container").height();
        stopLeft = $(".bottom-item-container").offset().left + $(".bottom-item-container").width() - $(".pop-container").width();
        if (isFixed === true) {
            $(".pop-container").css({
                "left": popLeft
            });
        }
    };

    var maxLineNum = 5;
    $(".pop-link").find(".detail").mlellipsis(3, "pop-detail");
}

//================================================================================================

//首页碎片交互=================================================================================================
//mulLine.init();
function dealItems() {
    
    if (!citems.length) {
        return;
    }

    if (document.body.clientWidth < 1190 && document.body.clientWidth > 768) {
        totalLineNum = 6;
        // 6 行
        maxTitleStrNum = 39;
        // 13个字
        maxDetailStrNum = 20;
        // 18个字
    } else if (document.body.clientWidth <= 768) {
        totalLineNum = 5;
        // 6 行
        maxTitleStrNum = 12;
        // 13个字
        maxDetailStrNum = 15;
    }

    titleLineNum = 0;
    subTitleLineNum = 0;
    detailLineNum = 0;
    detailStr = "";
    self = null;

    citems.each(function() {
        self = $(this);

        var itemTitleObj = self.find(".item-title:first"),
            subTitle = itemTitleObj.next();
        // 主标题行数
        titleLineNum = 1;
        if (maxTitleStrNum < itemTitleObj.attr("_title").length) {
            titleLineNum = 2;
            itemTitleObj.text(itemTitleObj.attr('_title').substring(0, (maxTitleStrNum - 1) * 2) + '...');
        }

        // 副标题行数
        subTitleLineNum = 1;
        
        if (maxTitleStrNum < itemTitleObj.next().attr("_title").length) {
            subTitleLineNum = 2;
            subTitle.text(subTitle.attr('_title').substring(0, (maxTitleStrNum - 1) * 2) + '...');
        }

        // 摘要内容行数
        detailLineNum = totalLineNum - titleLineNum - subTitleLineNum;
        if ((maxDetailStrNum * detailLineNum) < self.find(".item-detail").attr("_title").length) {
            detailStr = self.find(".item-detail").attr("_title").substring(0, (maxDetailStrNum - 1) * (detailLineNum)) + " ...";
            self.find(".item-detail").text(detailStr.replace(/[a-zA-Z]+?\s{1}\.\.\.\$/, " ..."));
        } else {
            self.find(".content-item-detail").text(self.find(".content-item-detail").attr("_title"));
        }
    });
    citems = null;
}

dealItems();
//=============================================================

//邮件订阅=============================================================================================================
$(".followus-button").click(function() {
    if ($(".follow-us-input-content input").val() == '') {
        box.alert(tips.getTips("emailEmpty"));
        return;
    }
    if (!tools.IYOHO_isEmail($(".follow-us-input-content input").val())) {
        box.alert(tips.getTips("emailError"));
        return;
    }
    $(".search-suggest").hide();
    window.open('/passport/subscribe/index?email=' + $(".follow-us-input-content input").val());
});
$(".followus-input").keydown(function(event) {
    if (event.keyCode == 13) {
        $(".followus-button").trigger("click");
    }
});
//===================================================================================================================