home.js 2.25 KB
/**
 * 逛首页
 * @author: xuqi<qi.xu@yoho.cn>
 * @date: 2015/10/10
 */

var $ = require('jquery'),
    Hammer = require('hammer'),
    Swiper = require('yoho.iswiper');

var info = require('./info'),
    loadMore = info.loadMore;

var $loadMoreInfo = $('#load-more-info');
var $loading = $(''),
    $noMore = $('');

var winH = $(window).height(),
    loadMoreH = $loadMoreInfo.height();

var $infoList = $('#info-list'),
    $infos = $infoList.children('.info-list'),
    $nav = $('#guang-nav'),
    $curNav = $nav.children('.focus'),
    curType = $curNav.data('type');

var state = {};

var mySwiper, navHammer;

if ($loadMoreInfo.length > 0) {
    $loading = $loadMoreInfo.children('.loading');
    $noMore = $loadMoreInfo.children('.no-more');
}

mySwiper = new Swiper('.swiper-container', {
    lazyLoading: true,
    pagination: '.swiper-pagination'
});

info.initInfosEvt($infoList);

//初始化各Nav下资讯加载的状态
(function() {
    var gender = $('#gender').val();

    $nav.children('.guang-nav-item').each(function() {
        var type = $(this).data('type'),
            focus = $(this).hasClass('focus');

        state[type] = {
            page: focus ? 1 : 0,
            gender: gender,
            type: type,
            end: false
        };
    });
}());

navHammer = new Hammer($nav[0]);
navHammer.on('tap', function(e) {
    var $this = $(e.target).closest('.guang-nav-item'),
        $content,
        index;

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


    index = $this.index();

    $this.addClass('focus');
    $curNav.removeClass('focus');

    $infos.not('.hide').addClass('hide');

    $content = $infos.eq(index);
    $content.removeClass('hide');

    $curNav = $this;
    curType = $this.data('type');

    //当未加载数据时去请求数据
    if (state[curType].page === 0) {
        loadMore($content, state[curType]);
    }

    //重置当前Tab的load-more
    if (state[curType].end) {
        $loading.addClass('hide');
        $noMore.removeClass('hide');
    } else {
        $loading.removeClass('hide');
        $noMore.addClass('hide');
    }
});

$(document).scroll(function() {
    if ($(window).scrollTop() + winH >= $(document).height() - loadMoreH) {
        loadMore($infos.not('hide'), state[curType]);
    }
});