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

var plusstar = {},
    $footer = $('#yoho-footer');

require('yoho-jquery-lazyload');
require('../common');

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

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

        // 重置tab code位置
        if (window.localStorage) {
            $tabUlDom.find('li').each(function() {
                localStorage.setItem($(this).data('code'), 0);
            });
        }

        $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;

        this.common.codeDefault = code;// 记住最后一次的tab code

        loading.showLoadingMask();

        $.ajax({
            type: 'GET',
            url: '/guang/plusstar/resources-template',
            data: {
                code: code,
                app_version: window.queryString.app_version || window.queryString.appVersion
            },
            dataType: 'html',
            success: function(data) {
                $('.plusstar-resources').html(data);

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

                that.resInit();

                loading.hideLoadingMask();

                $('.plusstar-resources').find('img.lazy').lazyload();

                // 设置滚动条的位置
                if (window.localStorage) {
                    $(document).scrollTop(localStorage.getItem(code) || 0);
                }
            },
            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() {

    if (!(window.queryString.app_version || window.queryString.appVersion)) {
        $('.tab-nav').css({
            position: 'relative'
        });

        $footer.css({
            'max-width': '650px'
        }).before(
            '<div style="height: ' + parseInt($footer.css('height'), 0) + 'px"></div>'
        );
    }

    plusstar.init();

    if (window.localStorage) {
        $(document).scroll(function() {
            localStorage.setItem(plusstar.common.codeDefault, $(this).scrollTop());
        });
    }
});