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

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

require('../common');

plusstar = {
    scrollObj: {},
    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;

        if (typeof this.scrollObj[code] === 'undefined') {
            // 如果不是第一次点击,回顶部
            $(document).scrollTop(0);
            this.scrollObj[code] = true;
        }

        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();
                lazyLoad($('img.lazy'));

                // 处理左右滑动,未加载的图片
                setTimeout(function() {
                    $('img.lazy').each(function() {
                        if ($(this).attr('src') !== $(this).data('original')) {
                            $(this).attr('src', $(this).data('original'));
                        }
                    });
                }, 40);
            },
            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 ($footer.length > 0) {
        $footer.before(
            '<div style="height: ' + parseInt($footer.css('height'), 0) + 'px"></div>'
        );
    }
    plusstar.init();
});