img-brand.js
1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
var $ = require('yoho-jquery'),
lazyLoad = require('yoho-jquery-lazyload');
require('yoho-slider');
exports.init = function(selector, isAuto) {
var $imgBrand = $(selector);
var slider = $imgBrand.unslider({
delay: 5000,
arrows: false,
nav: false
});
var imgBrandNum = $imgBrand.find('li').length;
var cols = imgBrandNum / 3;
var timer;
if (!($imgBrand.find('li').length % 3)) {
cols -= 1;
}
$imgBrand.find('li:nth-child(3n)').css('margin-right', 0);
slider.on('unslider.change', function() {
setTimeout(function() {
var $lazyImg = $('.img-item img.lazy');
$lazyImg.each(function(idx, item) {
var $item = $(item);
if ($item.attr('src').match('data:image') || $item.attr('src').length === 0) {
lazyLoad($item);
}
});
}, 1000);
});
$('.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');
}
});
function autoSlider() {
timer = setInterval(function() {
if (slider.data('unslider').current >= cols) {
slider.unslider('animate:first');
} else {
slider.unslider('next');
}
}, 2000);
}
if (isAuto) {
autoSlider();
$imgBrand.hover(function() {
clearInterval(timer);
}, function() {
autoSlider();
});
}
};