Blame view

public/js/guang/collocation-block.js 3.1 KB
徐炜 authored
1 2 3 4 5 6 7 8 9 10 11
/**
 * 搭配区块
 *
 * @constructor
 */
function CollocationBlock() {
    this.elem = $('.collocation-block');
    this.container = this.elem.children('.thumb-container');
    this.elem.find('.prod');
    this.prods = this.elem.find('.prod');
ccbikai(👎🏻🍜) authored
12
    // this.fixedContainer = $('#wrapper')
徐炜 authored
13 14 15
    //    .after(this.container.clone().addClass('fixed-thumb-container fixed-bottom'))
    //    .next('.thumb-container');
ccbikai(👎🏻🍜) authored
16
    // lazyLoad(this.fixedContainer.find('.lazy'));
徐炜 authored
17 18 19 20 21 22 23 24 25 26 27 28

    this.container.delegate('.thumb', 'touchend', this._touchHandler.bind(this));

    // this.fixedContainer.delegate('.thumb', 'touchend', this._touchHandler.bind(this));

    this.thumbs = this.container.find('li');

    // this.fixedThumbs = this.fixedContainer.find('li');

    // 初始化箭头位置
    this._updateCollocationArrowPos(this.thumbs.filter('.focus'));
陈峰 authored
29 30 31 32
    // let scrollFn = () => {
    //     let pos = this.container.offset(),
    //         posFixed = this.fixedContainer.offset();
    //     let visible = false;
徐炜 authored
33 34

陈峰 authored
35 36 37 38 39
    //     if (posFixed.top >= pos.top) {
    //         visible = false;
    //     } else {
    //         visible = true;
    //     }
徐炜 authored
40
陈峰 authored
41 42 43 44 45 46 47 48
    //     if (posFixed.top + this.fixedContainer.height() > $('ul.brand-list').offset().top) {
    //         visible = false;
    //     } else if (pos.top < $(window).scrollTop()) {
    //         visible = true;
    //         this.fixedContainer.removeClass('fixed-bottom').addClass('fixed-top');
    //     } else {
    //         this.fixedContainer.removeClass('fixed-top').addClass('fixed-bottom');
    //     }
徐炜 authored
49
陈峰 authored
50 51 52 53
    //     this.fixedContainer.css({
    //         visibility: visible ? 'visible' : 'hidden'
    //     });
    // };
徐炜 authored
54
ccbikai(👎🏻🍜) authored
55 56
    // $(document).on('scroll', scrollFn);
    // $(document).on('touchmove', scrollFn);
徐炜 authored
57 58 59 60 61 62 63 64 65 66 67 68 69

}

CollocationBlock.exists = function() {
    return $('.collocation-block').size() > 0;
};

$.extend(CollocationBlock.prototype, {
    /**
     * 计算搭配的箭头的位置
     * @param current 当前focus的搭配项
     */
    _updateCollocationArrowPos: function(current) {
陈峰 authored
70
        let left = current.offset().left,
徐炜 authored
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
            bgPos = -$(window).width() + left + (this.thumbs.width() / 2) + 'px';

        this.container.css({
            backgroundPosition: bgPos + ' bottom'
        });

        /*
         this.fixedContainer.css({
         backgroundPosition: bgPos + ' bottom'
         });
         */
    },

    /**
     * 搭配thumb的touch事件句柄
     *
     * @param e
     * @private
     */
    _touchHandler: function(e) {
陈峰 authored
91
        let current = $(e.currentTarget),
徐炜 authored
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
            index = current.index();

        if (current.hasClass('focus')) {
            return;
        }

        this.thumbs.removeClass('focus');

        // this.fixedThumbs.removeClass('focus');

        current.addClass('focus');

        // 更新箭头位置
        this._updateCollocationArrowPos(current);

        this.prods.not('.hide').addClass('hide');
        this.curProds = this.prods.eq(index);
        this.curProds.removeClass('hide');

        $('body').animate({
            scrollTop: this.elem.offset().top
        }, 400);
    }
});


module.exports = CollocationBlock;