outlets.page.js 3.42 KB
/**
 * 首页
 * @author: yyq<yanqing.yang@yoho.cn>
 * @date: 2016/05/31
 */

var $ = require('yoho-jquery'),
    lazyLoad = require('yoho-jquery-lazyload');

var $limitList = $('.limit-main .good-list'),
    $fixNav = $('.sort-layer .full-line'),
    $navPar,
    homePage = $('.outlets-page').data('page');

var limitTime = [];

require('../common');
require('../plugins/slider');
require('./outlets/special');

function convertTime(time) {
    var anHour = 3600000,
        aMinute = 60000,
        aSecond = 1000,
        showTime = '',
        d = 0,
        h = 0,
        m = 0,
        s = 0;

    if (time > 1000) {
        d = Math.floor(time / anHour / 24);
        h = Math.floor(time / anHour % 24);
        m = Math.floor(time / aMinute % 60);
        s = Math.floor(time / aSecond % 60);

        showTime += d > 0 ? d + '天' : '';
        showTime += h > 0 ? h + '时' : '';
        showTime += m > 0 ? m + '分' : '';
        showTime += s > 0 ? s + '秒' : '';
    }

    return showTime;
}

function setPageTime() {
    var i, text, showTime;

    for (i = 0; i < limitTime.length; i++) {
        if (limitTime[i].time < 0) {
            return;
        }

        limitTime[i].time -= 1000;
        showTime = convertTime(limitTime[i].time);
        if (showTime) {
            text = showTime;
            if (limitTime[i].pre) {
                text = limitTime[i].pre + ' ' + text;
            }
        } else {
            text = '活动已结束';
        }
        limitTime[i].dom.text(text);
    }
}

function limitPageSwitch(num) {

    $limitList.animate({
        'margin-left': (-num * 10) + '0%'
    }, 'fast');

    $limitList.data({
        page: num
    });
}

function formatExtraGood() {
    var $item = $limitList.find('.good-item'),
        html = '',
        list = [],
        i = 0;

    if (!$limitList.length) {
        return;
    }

    $item.each(function() {
        if ((i % 5) === 0) {
            list.push('');
        }
        list[list.length - 1] += $item[i].outerHTML;
        i++;
    });

    $limitList.data({max: list.length});

    for (i = 0; i < list.length; i++) {
        html += '<div class="list-page">' + list[i] + '</div>';
    }

    $limitList.html(html);
}

//
if ($fixNav.length) {
    $navPar = $fixNav.parent();

    $(window).scroll(function() {
        var navTop = $navPar.position().top;

        if ($(this).scrollTop() >= navTop) {
            $fixNav.addClass('fixed');
        } else {
            $fixNav.removeClass('fixed');
        }
    });
}

// 倒计时
$('.time').each(function() {
    var data = $(this).data();

    if (data.limit) {
        limitTime.push({
            dom: $(this),
            time: data.limit,
            pre: data.pre ? data.pre : ''
        });
    }
});

$('.good-page-btns > label').click(function() {
    var nowTime = new Date().getTime(),
        data = $limitList.data(),
        max = data.max ? data.max : 1,
        page = data.page ? data.page : 0;

    if (data.time && nowTime - data.time < 500) {
        return;
    }

    $limitList.data({
        time: nowTime
    });

    if ($(this).hasClass('pre-page-btn')) {
        page += max - 1;
    } else {
        page += 1;
    }

    page = page % max;

    limitPageSwitch(page);
});

setInterval(setPageTime, 1000);

if (homePage) {
    $('.slide-container').slider({
        orient: false
    });
} else {
    $('.slide-container').slider({
        orient: true
    });
}

formatExtraGood();

lazyLoad($('img.lazy'));