img-brand.js 1.24 KB
var $ = require('yoho-jquery');

require('yoho-slider');

exports.init = function(selector) {
    var $imgBrand = $(selector);
    var slider = $imgBrand.unslider({
        autoplay: true,
        delay: 5000,
        arrows: false,
        nav: false
    });
    var imgBrandNum = $imgBrand.find('li').length;
    var cols = imgBrandNum / 3;
    var changeSliderTimeout;

    if (!($imgBrand.find('li').length % 3)) {
        cols -= 1;
    }

    $imgBrand.find('li:nth-child(3n)').css('margin-right', 0);

    slider.on('unslider.change', function(event, index) {
        clearTimeout(changeSliderTimeout);
        if (index >= cols) {
            changeSliderTimeout = setTimeout(function() {
                slider.unslider('animate:first');
            }, 4999);
        }
    });

    $('.img-brand-switch').on('click', '.prev', function() {
        if (slider.data('unslider').current > cols || slider.data('unslider').current <= 0) {
            slider.unslider('animate:' + cols);
        } else {
            slider.unslider('prev');
        }
    }).on('click', '.next', function() {
        if (slider.data('unslider').current >= cols) {
            slider.unslider('animate:first');
        } else {
            slider.unslider('next');
        }
    });
};