detail.js 2.6 KB
/**
 *  商品详情
 * @author: liangzhifeng<zhifeng.liang@yoho.cn>
 * @date: 2015/10/20
 */
var $ = require('jquery'),
    Swiper = require('yoho.iswiper'),
    Hammer = require('yoho.hammer'),
    lazyLoad = require('yoho.lazyload');

var goodsSwiper,
    $discountFolder = $('.goodsDiscount .discount-folder'),
    $discountArrow = $('.goodsDiscount .first-item span');

var goodsDiscountEl = document.getElementById('goodsDiscount'),
    goodsDiscountHammer = goodsDiscountEl && new Hammer(goodsDiscountEl);

var $cart = $('.cart-bar'),
    $goodsSubtitle = $('.goodsSubtitle'),
    divH,
    $goodsSubtitleSpan;

require('./desc');
require('./comments-consults');
require('../recommend-for-you-product-desc');

//add extra marign-bottom for footer to show the yoho copyright
function showFooter() {
    var $cartBar = $('.cart-bar');

    var timer = setInterval(function() {
        if ($cartBar) {
            window.reMarginFooter('.cart-bar');
            clearInterval(timer);
        } else {
            $cartBar = $('.cart-bar');
        }
    }, 200);
}

showFooter();

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

//顶部swiper
goodsSwiper = new Swiper('.banner-swiper', {
    lazyLoading: true,
    lazyLoadingInPrevNext: true,
    paginationClickable: true,
    pagination: '.banner-top .pagination-inner',
    nextButton: '.my-swiper-button-next',
    prevButton: '.my-swiper-button-prev',
    spaceBetween: 3
});


//初始化goods-discount
if (0 === $('.goodsDiscount .discount-folder').children().length) {
    $discountFolder.css('display', 'none');
    $discountArrow.html('');
}

//goods-discount下拉按钮点击事件
if (goodsDiscountHammer) {
    goodsDiscountHammer.on('tap', function(e) {
        if ($discountFolder.is(':hidden')) {
            $discountArrow.removeClass('icon-down').addClass('icon-up').html('&#xe608;');
            $discountFolder.slideDown();
        } else {
            $discountArrow.removeClass('icon-up').addClass('icon-down').html('&#xe609;');
            $discountFolder.slideUp();
        }
        return false;
    });
}

//购物车商品数量
$.ajax({
    type: 'GET',
    url: '/cart/index/count',
    success: function(data) {
        if (data.code === 200) {
            $cart.find('.num-tag').html(data.data.cart_goods_count).removeClass('hide');
        }
    }
});

//限制goodsSubtitle为两行
if ($goodsSubtitle[0]) {
    divH = $goodsSubtitle.height();
    $goodsSubtitleSpan = $goodsSubtitle.find('span');
    while ($goodsSubtitleSpan.outerHeight() > divH) {
        $goodsSubtitleSpan.text($goodsSubtitleSpan.text().replace(/(\s)*([a-zA-Z0-9]+|\W)(\.\.\.)?$/, '...'));
    }
}
require('./like');