sale.page.js 2.96 KB
/**
 * sale页面
 * @author: da<abel.wang@yoho.cn>
 * @date: 2016/05/31
 */
var $ = require('yoho-jquery'),
    lazyLoad = require('yoho-jquery-lazyload'),
    product = require('./index/product');

var limitTime = [];

var $saleBox = $('.sale-box'),
    $navItem = $('.sale-nav li');

require('../common');
require('./sale/banner');
require('../plugins/slider');
require('../plugins/filter');
require('../plugins/sort-pager');
require('./detail/latest-walk');

product.init(4);

lazyLoad($('img.lazy'));
$('.slide-container').slider();

// 请求带导航的列表数据
function getNaviData(items) {
    var location, url, goodItems, navTitle;

    navTitle = items.attr('urlLocation');

    location = '/product/sale/goods';

    goodItems = items.parent().parent('.sale-box').find('.goods');

    url = location + navTitle;
    $.ajax({
        type: 'GET',
        url: url,
        data: ''
    }).then(function(res) {

        goodItems.html('');
        goodItems.append(res);
        lazyLoad(items.closest('.sale-box').find('.lazy'));
    });
}

$navItem.on('click', function() {
    $(this).siblings('.active').removeClass('active');
    $(this).addClass('active');

    getNaviData($(this));

});


//  初始化页面数据
(function() {

    $saleBox.each(function() {
        var item = $(this).find($navItem).eq(0);

        getNaviData(item);
    });
}());


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) {
            $('.time').addClass('hide');
            return;
        }

        limitTime[i].time -= 1000;
        showTime = convertTime(limitTime[i].time);
        if (showTime) {
            text = showTime;
            if (limitTime[i].pre) {
                text = limitTime[i].pre + ' ' + text;
            }

            $('.time-calculate').removeClass('hide');
            $('.time-calculate').text(showTime);
        } else {
            text = '活动已结束';
            $('.time').addClass('hide');
        }
    }
    if (limitTime.length === 0) {
        $('.time').addClass('hide');
    }
}

if ($('.sale-discount-page').length > 0) {

    $('.time-calculate').each(function() {
        var data = $(this).attr('data');

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

    setInterval(setPageTime, 1000);
}