Showing
1 changed file
with
173 additions
and
0 deletions
public/js/plugins/slider.js
0 → 100644
1 | +/** | ||
2 | + * 首页banner轮播 | ||
3 | + * @author: liuyue(yue.liu@yoho.cn) | ||
4 | + * @date: 2015/12/04 | ||
5 | + */ | ||
6 | + | ||
7 | +var $ = require('yoho-jquery'), | ||
8 | + lazyLoad = require('yoho-jquery-lazyload'); | ||
9 | + | ||
10 | +(function() { | ||
11 | + var Slider = function(element, options) { | ||
12 | + this.$element = $(element); | ||
13 | + this.options = $.extend({}, $.fn.slider.defaults, options); | ||
14 | + this.bigItem = this.$element.find('.slide-wrapper').find('li'); | ||
15 | + this.smallItem = null; | ||
16 | + this.len = this.bigItem.size(); | ||
17 | + this.index = 0; | ||
18 | + this.timer = null; | ||
19 | + this.init(); | ||
20 | + }; | ||
21 | + | ||
22 | + Slider.prototype = { | ||
23 | + init: function() { | ||
24 | + if (!this.$element) { | ||
25 | + return; | ||
26 | + } | ||
27 | + | ||
28 | + if (this.len <= 1) { | ||
29 | + lazyLoad(this.$element.find('img.lazy')); | ||
30 | + return; | ||
31 | + } | ||
32 | + if (this.options.pagination) { | ||
33 | + this.smallItem = $(this.options.pagination).find('li'); | ||
34 | + } else { | ||
35 | + this._createPage(); | ||
36 | + } | ||
37 | + | ||
38 | + if (this.options.orient) { | ||
39 | + this._createOrient(); | ||
40 | + } | ||
41 | + this._slideShow(); | ||
42 | + this._bindEvent(); | ||
43 | + this._autoplay(); | ||
44 | + }, | ||
45 | + _createOrient: function() { | ||
46 | + | ||
47 | + var orientHtml = '<div class="slide-switch">' + | ||
48 | + '<a class="prev" href="javascript:;"><span class="iconfont"></span></a>' + | ||
49 | + '<a class="next" href="javascript:;"><span class="iconfont"></span></a>' + | ||
50 | + '</div>'; | ||
51 | + | ||
52 | + if (this.$element.find('.slide-switch').length > 0) { | ||
53 | + return; | ||
54 | + } | ||
55 | + | ||
56 | + this.$element.append(orientHtml); | ||
57 | + }, | ||
58 | + _createPage: function() { | ||
59 | + var pageHtml = '<div class="slide-pagination"><div class="slide-pagination-inner">' + | ||
60 | + '<div class="slide-shade"></div><div class="slide-pagination-last">', | ||
61 | + i = 0; | ||
62 | + | ||
63 | + if (this.len <= 1) { | ||
64 | + return; | ||
65 | + } | ||
66 | + for (i = 0; i < this.len; i++) { | ||
67 | + pageHtml += '<span></span>'; | ||
68 | + } | ||
69 | + pageHtml += '</div></div></div>'; | ||
70 | + this.$element.append($(pageHtml)); | ||
71 | + this.smallItem = this.$element.find('.slide-pagination-inner span'); | ||
72 | + }, | ||
73 | + _bindEvent: function() { | ||
74 | + var that = this; | ||
75 | + | ||
76 | + this.$element.find('.slide-switch').on('click', '.next', function() { | ||
77 | + that._nextSlide(); | ||
78 | + clearInterval(that.timer); | ||
79 | + that.timer = setInterval(function() { | ||
80 | + that._nextSlide(); | ||
81 | + }, that.options.time); | ||
82 | + }).on('click', '.prev', function() { | ||
83 | + that._prevSlide(); | ||
84 | + clearInterval(that.timer); | ||
85 | + that.timer = setInterval(function() { | ||
86 | + that._nextSlide(); | ||
87 | + }, that.options.time); | ||
88 | + }); | ||
89 | + | ||
90 | + this.smallItem.on('mouseenter', function() { | ||
91 | + that.index = $(this).index(); | ||
92 | + clearInterval(that.timer); | ||
93 | + that._slideShow(); | ||
94 | + }).on('mouseleave', function() { | ||
95 | + that._autoplay(); | ||
96 | + }); | ||
97 | + | ||
98 | + this.$element.on('mouseenter', function() { | ||
99 | + $(this).find('.slide-switch').addClass('show'); | ||
100 | + }).on('mouseleave', function() { | ||
101 | + $(this).find('.slide-switch').removeClass('show'); | ||
102 | + }); | ||
103 | + }, | ||
104 | + _nextSlide: function() { | ||
105 | + if (this.index === this.len - 1) { | ||
106 | + this.index = 0; | ||
107 | + } else { | ||
108 | + this.index++; | ||
109 | + } | ||
110 | + this._slideShow(); | ||
111 | + }, | ||
112 | + _prevSlide: function() { | ||
113 | + if (this.index === 0) { | ||
114 | + this.index = this.len - 1; | ||
115 | + } else { | ||
116 | + this.index--; | ||
117 | + } | ||
118 | + this._slideShow(); | ||
119 | + }, | ||
120 | + _slideShow: function() { | ||
121 | + var $img = this.bigItem.eq(this.index).find('img.lazy'); | ||
122 | + | ||
123 | + // 未加载图片的及时显示 | ||
124 | + if ($img.attr('src') !== $img.data('original')) { | ||
125 | + lazyLoad($img, { | ||
126 | + event: 'sporty' | ||
127 | + }); | ||
128 | + $img.trigger('sporty'); | ||
129 | + } | ||
130 | + | ||
131 | + this.smallItem.eq(this.index).addClass('focus').siblings().removeClass('focus'); | ||
132 | + this.bigItem.eq(this.index).show().stop().animate({ | ||
133 | + opacity: 1 | ||
134 | + }, function() { | ||
135 | + | ||
136 | + // 修正IE下切换时文字会重叠的问题 | ||
137 | + $(this).find('.slide-tips > p').removeClass('hide'); | ||
138 | + }).siblings().stop().animate({ | ||
139 | + opacity: 0 | ||
140 | + }, function() { | ||
141 | + $(this).hide().find('.slide-tips > p').addClass('hide'); | ||
142 | + }); | ||
143 | + }, | ||
144 | + _autoplay: function() { | ||
145 | + var that = this; | ||
146 | + | ||
147 | + clearInterval(this.timer); | ||
148 | + this.timer = setInterval(function() { | ||
149 | + that._nextSlide(); | ||
150 | + }, this.options.time); | ||
151 | + } | ||
152 | + }; | ||
153 | + $.fn.slider = function(option) { | ||
154 | + return this.each(function() { | ||
155 | + var $this = $(this), | ||
156 | + data = $this.data('Slider'), | ||
157 | + options = typeof option === 'object' && option; | ||
158 | + | ||
159 | + if (!data) { | ||
160 | + $this.data('Slider', (data = new Slider(this, options))); | ||
161 | + } | ||
162 | + if (typeof option === 'string') { | ||
163 | + data[option](); | ||
164 | + } | ||
165 | + }); | ||
166 | + }; | ||
167 | + $.fn.slider.Constructor = Slider; | ||
168 | + $.fn.slider.defaults = { | ||
169 | + time: 5000, | ||
170 | + orient: true, // 左右切换箭头的显示 | ||
171 | + pagination: null | ||
172 | + }; | ||
173 | +}($)); |
-
Please register or login to post a comment