@@ -39,20 +39,16 @@ class ImageViewer {
3939 this . translateX = 0 ;
4040 }
4141
42- _generateViewerDom ( ) {
43- return Array . prototype . slice . call ( this . images , 0 , 3 ) . map ( function ( ) {
44- return `<div class="viewer"><div class="panel"><img></div></div>`
45- } ) . join ( ) ;
46- }
47-
4842 _create ( ) {
4943 this . el = query ( '.image-viewer' ) [ 0 ] ;
5044 this . destroy ( ) ;
5145 let imageViewerTemplate =
5246 `<div class="image-viewer">
5347 <div class="image-header"></div>
5448 <div class="image-body">
55- ${ this . _generateViewerDom ( ) }
49+ <div class="viewer"><div class="panel"><img></div></div>
50+ <div class="viewer"><div class="panel"><img></div></div>
51+ <div class="viewer"><div class="panel"><img></div></div>
5652 </div>
5753 <div class="image-footer"></div>
5854 </div>` ;
@@ -82,7 +78,7 @@ class ImageViewer {
8278 this . viewers = [ ] ;
8379 for ( let i = 0 , length = this . itemList . length , item ; i < length ; i ++ ) {
8480 item = this . itemList [ i ] ;
85- this . viewers . push ( new Viewer ( this , item , this . width , this . height ) ) ;
81+ this . viewers . push ( new Viewer ( this , item , this . width , this . height , i ) ) ;
8682 }
8783 this . translateX = 0 ;
8884 this . swipeInByIndex ( this . currentIndex ) ;
@@ -202,6 +198,9 @@ class ImageViewer {
202198 return this . images [ index ] || '' ;
203199 }
204200
201+ /**
202+ * 重置当前图片的缩放
203+ */
205204 reset ( ) {
206205 this . viewers [ 1 ] . init ( this . displayIndex , true , null , false ) ;
207206 setTimeout ( ( ) => {
@@ -272,11 +271,18 @@ class ImageViewer {
272271 }
273272 }
274273
274+ /**
275+ * 根据给定的下标移动到指定图片处
276+ * @param index 数组下标,从0开始
277+ */
275278 swipeInByIndex ( index ) {
276279 if ( ! isNaN ( index ) && - 1 < index && index < this . imagesLength ) {
277280 this . currentIndex = index ;
278- setTranslateStyle ( this . bodyEl , this . translateX , 0 ) ;
281+ setTranslateStyle ( this . bodyEl , 0 , 0 ) ;
279282
283+ this . viewers = this . viewers . sort ( function ( a , b ) {
284+ return a . index < b . index ;
285+ } ) ;
280286 this . viewers [ 0 ] . init ( - 1 , true , null , true , this . _getPrevImage ( ) ) ;
281287 this . viewers [ 1 ] . init ( 0 , true , null , true , this . _getCurrentImage ( ) ) ;
282288 this . viewers [ 2 ] . init ( 1 , true , null , true , this . _getNextImage ( ) ) ;
@@ -294,10 +300,6 @@ class ImageViewer {
294300 this . images = images ;
295301 this . imagesLength = images . length ;
296302 this . currentIndex = startIndex ;
297-
298- let listEl = query ( '.image-body' , this . el ) [ 0 ] ;
299- listEl . innerHTML = this . _generateViewerDom ( ) ;
300- this . itemList = listEl . children ;
301303 this . _init ( ) ;
302304 }
303305
0 commit comments