city-swiper.js 1.97 KB
import Swiper from 'yoho-swiper';
import $ from 'yoho-jquery';
import Page from 'yoho-page';

class CitySwiper extends Page {
    constructor(data) {
        super();
        this.selector = {
            $swiperC: $('.city-swiper'),
            $swiper: $('.city-swiper .swiper-list').find('.swiper-container'),
            $slide: $('.city-swiper .swiper-list').find('.swiper-slide'),
            $swiperLine: $('.city-swiper .swiper-line'),
            $swiperList: $('.city-swiper .swiper-list')
        };
        this.init(data);
    }

    init(data) {
        let swiperParams = this.swiperParams(data);

        this.setStyle(data);
        new Swiper('.city-swiper .swiper-container', swiperParams);
    }

    setStyle(data) {
        let wWidth = $(window).width();
        let swiperWidth = wWidth / data.swiperNum || 1;
        let lineHeight = this.selector.$swiperList.height() / data.lineNum;
        let lineTop = lineHeight / 2;

        if (data.hidePartingLine) {
            this.selector.$swiperLine.hide();
        }
        this.selector.$swiper.css('width', `${swiperWidth}px`);
        this.selector.$swiperLine.css('height', `${lineHeight}px`);
        this.selector.$swiperLine.css('margin-top', `-${lineTop}px`);
    }

    swiperParams(data) {
        let $thisSwiper = '';
        let newParams = {
            direction: 'vertical',
            slidesPerView: data.lineNum || 1,
            centeredSlides: data.centeredSlides,
            loop: data.true,
            onInit: (swiper) => {
                $thisSwiper = swiper.activeIndex;
                $thisSwiper = $(swiper.slides[`${$thisSwiper}`]);
                this.emit('citySwiperCb', $thisSwiper);
            },
            onSlideChangeEnd: (swiper) => {
                $thisSwiper = swiper.activeIndex;
                $thisSwiper = $(swiper.slides[`${$thisSwiper}`]);
                this.emit('citySwiperCb', $thisSwiper);
            }
        };

        return newParams;
    }
}

module.exports = CitySwiper;