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

var $ = require('jquery');

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

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

var winH = $(window).height();

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

var state = {};

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

info.initSwiper(curType);

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 ? 2 : 1,
            gender: gender,
            type: type,
            end: false
        };
    });
}());
$nav.bind('contextmenu', function(e) {
    return false;
});
$nav.on('touchend touchcancel', 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');

    $content = $infos.eq(index);

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

    //当未加载数据时去请求数据
    if (state[curType].page === 1) {

        //无数据时隐藏正在加载和没有更多字样
        $loading.addClass('hide');
        $noMore.addClass('hide');

        state[curType].isTab = true;
        loadMore($content, state[curType]);
    } else {

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

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

    if (state[curType].page === 1) {
        window.rePosFooter();//进入空内容时重新定位footer位置
    }
});

function scrollHandler() {
    var $c = $infos.not('.hide');

    if ($(window).scrollTop() + winH >= $(document).height() - 0.25 * $c.height()) {
        loadMore($c, state[curType]);
    }
}

//srcoll to load more
$(document).scroll(function() {
    window.requestAnimationFrame(scrollHandler);
});

$nav.on('touchstart', function(e) {
    var target = e.target || e.srcElement;

    target.className = 'bytouch ' + target.className;
}).on('touchend touchcancel', function() {
    $nav.find('li').removeClass('bytouch');
});