logo-brand.js
2.63 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
70
71
72
73
74
75
76
77
78
79
80
81
82
/**
* 首页优选品牌js
* @author: liuyue(yue.liu@yoho.cn)
* @date: 2015/12/08
*/
var $ = require('yoho-jquery'),
lazyLoad = require('yoho-jquery-lazyload');
(function() {
var LogoBrand = function(element, options) {
this.$element = $(element);
this.options = $.extend({}, $.fn.logoBrand.defaults, options);
this.init();
};
LogoBrand.prototype = {
init: function() {
this.$element.addClass('logos-' + this.options.showNum);
lazyLoad(this.$element.find('img.lazy'));
this._bindEvent();
},
_brandShow: function(hidePage, showPage) {
var that = this;
lazyLoad($('li[data-page=' + showPage + ']').find('img.lazy').trigger('appear'));
that.$element.find('li[data-page=' + hidePage + ']').find('img').fadeOut('normal', function() {
that.$element.find('li').hide();
that.$element.find('li[data-page=' + showPage + ']').show().find('img').fadeIn();
});
},
_bindEvent: function() {
var that = this;
that.$element.on('click', '.next', function() {
var page = $(this).parent().data('page'),
nextPage = 0,
totalPage = Math.ceil(that.$element.find('li').size() / (that.options.showNum + 2)) - 1;
if (page === totalPage) {
nextPage = 0;
} else {
nextPage = page + 1;
}
that._brandShow(page, nextPage);
});
that.$element.on('click', '.prev', function() {
var page = $(this).parent().data('page'),
prevPage = 0,
totalPage = Math.ceil(that.$element.find('li').size() / (that.options.showNum + 2)) - 1;
if (page === 0) {
prevPage = totalPage;
} else {
prevPage = page - 1;
}
that._brandShow(page, prevPage);
});
}
};
$.fn.logoBrand = function(option) {
return this.each(function() {
var $this = $(this),
data = $this.data('LogoBrand'),
options = typeof option === 'object' && option;
if (!data) {
$this.data('LogoBrand', (data = new LogoBrand(this, options)));
}
if (typeof option === 'string') {
data[option]();
}
});
};
$.fn.logoBrand.Constructor = LogoBrand;
$.fn.logoBrand.defaults = {
showNum: 16,
url: '/boys/getBrand'
};
}());