plusstar-index.page.js 2.59 KB
var $ = require('yoho-jquery'),
    tip = require('../plugin/tip'),
    Swiper = require('yoho-swiper'),
    loading = require('../plugin/loading'),
    lazyLoad = require('yoho-jquery-lazyload');

var plusstar = {};

require('../common');
lazyLoad($('img.lazy'));

plusstar = {
    init: function() {
        var that = this,
            $liDom,
            $tabUlDom;

        $tabUlDom = $('.plusstar-page .tab-nav ul');

        $tabUlDom.find('li').css({
            width: 100 / $tabUlDom.find('li').length + '%'
        });

        // 事情委托机制
        $tabUlDom.bind('click', function(event) {
            $liDom = $(event.target).closest('li');

            if ($liDom.hasClass('focus')) {
                return true;
            }

            $(this).find('li').removeClass('focus');
            $liDom.addClass('focus');
            that.tabNav($liDom.data('code'));
        });

        // start -- 默认选中
        $liDom = $tabUlDom.find('li.focus');
        if ($liDom.length <= 0) {
            // 默认第一个
            $liDom = $('.plusstar-page .tab-nav ul li:eq(0)');
        }
        $tabUlDom.find('li').removeClass('focus');
        $liDom.addClass('focus');
        that.tabNav($liDom.data('code'));

        // ent -- 默认选中
    },
    tabNav: function(code) {
        var that = this;

        loading.showLoadingMask();
        $.ajax({
            type: 'GET',
            url: '/guang/plusstar/resources-template',
            data: {
                code: code
            },
            dataType: 'html',
            success: function(data) {
                $('.plusstar-resources').html(data);

                if (data === '') {
                    tip.show('没有更多内容了');
                    return true;
                }

                that.resInit();

                loading.hideLoadingMask();
                lazyLoad($('img.lazy'));
            },
            error: function() {
                tip.show('网络断开连接了~');
                loading.hideLoadingMask();
            }
        });
    },
    resInit: function() {
        // 头部banner轮播
        if ($('.banner-swiper').find('li').size() > 1) {
            new Swiper('.banner-swiper', {
                lazyLoading: true,
                lazyLoadingInPrevNext: true,
                loop: true,
                autoplay: 3000,
                autoplayDisableOnInteraction: false,
                paginationClickable: true,
                slideElement: 'li',
                pagination: '.banner-top .pagination-inner'
            });
        }
    }
};

$(function() {
    plusstar.init();
});