Skip to content

Commit ae54c37

Browse files
committed
重构初步完成
1 parent faf9411 commit ae54c37

File tree

8 files changed

+77
-85
lines changed

8 files changed

+77
-85
lines changed

demo/index.html

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,11 @@ <h1>Hello, this is an image viewer.</h1>
3131
</div>
3232
<script src="build/imageViewer.js"></script>
3333
<script type="text/javascript">
34+
var images1 = ['images/2.jpg'];
35+
var images2 = ['images/2.jpg', 'images/3.jpg'];
36+
var images3 = ['images/2.jpg', 'images/3.jpg', 'images/4.jpg'];
3437
var images = ['images/2.jpg', 'images/3.jpg', 'images/4.jpg', 'images/5.jpg', 'images/6.jpg', 'images/7.jpg', 'images/8.jpg'];
35-
var imageViewer = new ImageViewer(images, {
38+
var imageViewer = new ImageViewer(images3, {
3639
container: 'body',
3740
enableScale: true,
3841
startIndex: 0,
@@ -57,15 +60,15 @@ <h1>Hello, this is an image viewer.</h1>
5760
swipeLastLeft: function (imageViewer, distance) {
5861
console.log('swipeLastLeft', distance);
5962
if (distance > 50) {
60-
//imageViewer.setImageOption(['images/4.jpg']);
61-
//return true;
63+
imageViewer.setImageOption(['images/4.jpg']);
64+
return true;
6265
}
6366
},
6467
swipeFirstRight: function (imageViewer, distance) {
6568
console.log('swipeFirstRight', distance);
6669
if (distance > 30) {
67-
//imageViewer.setImageOption(['images/5.jpg', 'images/6.jpg', 'images/7.jpg']);
68-
//return true;
70+
imageViewer.setImageOption(['images/5.jpg', 'images/6.jpg', 'images/7.jpg']);
71+
return true;
6972
}
7073
}
7174
});

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "image-viewer-gallery",
33
"version": "0.5.4",
44
"scripts": {
5-
"dev": "webpack-dev-server --inline --hot",
5+
"dev": "webpack-dev-server --config webpack.config.dev.js --inline --hot",
66
"dist": "webpack --config webpack.config.dist.js"
77
},
88
"description": "A lib for image display.",
@@ -21,12 +21,14 @@
2121
},
2222
"homepage": "https://github.com/KyLeoHC/ImageViewer#readme",
2323
"devDependencies": {
24+
"autoprefixer": "^6.7.0",
2425
"babel-core": "^6.17.0",
2526
"babel-loader": "^6.2.5",
2627
"babel-plugin-transform-runtime": "^6.15.0",
2728
"babel-preset-es2015": "^6.16.0",
2829
"babel-runtime": "^6.11.6",
2930
"css-loader": "^0.25.0",
31+
"postcss-loader": "^1.2.2",
3032
"style-loader": "^0.13.1",
3133
"webpack": "^1.13.2",
3234
"webpack-dev-server": "^1.16.2"

src/core/imageViewer.js

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -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

src/core/viewer.js

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@ import Event from '../common/event';
1313
import Hammer from '../lib/hammer';
1414

1515
class Viewer {
16-
constructor(imageViewer, el, width, height) {
16+
constructor(imageViewer, el, width, height, index) {
1717
this.event = new Event(false);
1818
this.imageViewer = imageViewer;
1919
this.el = el; //.viewer类
2020
this.panelEl = el.firstElementChild;//.panel类
2121
this.imageEl = query('img', this.el)[0];
2222
this.src = '';
23+
this.index = index; //viewer排序用,记录原始的数组位置
2324
this.displayIndex = 0;
2425
this.width = width;
2526
this.height = height;
@@ -80,7 +81,7 @@ class Viewer {
8081
_initImage();
8182
}
8283
return this;
83-
};
84+
}
8485

8586
_bindEvent() {
8687
let mc = new Hammer.Manager(this.panelEl);
@@ -115,7 +116,7 @@ class Viewer {
115116
this.imageEl.addEventListener('load', () => {
116117
this.event.emit(this.EVENT_NAME);
117118
}, false);
118-
};
119+
}
119120

120121
_pinch(scale) {
121122
let currentScale = scale + this.scale;
@@ -124,7 +125,7 @@ class Viewer {
124125
setScaleAndTranslateStyle(this.panelEl, this.currentScale, this.translatePanelX, this.translatePanelY);
125126
}
126127
return this;
127-
};
128+
}
128129

129130
_pinchEnd(scale) {
130131
this.scale = isNaN(scale) ? this.currentScale : scale;
@@ -141,7 +142,7 @@ class Viewer {
141142
lock.releaseLock(LOCK_NAME);
142143
}
143144
return this;
144-
};
145+
}
145146

146147
calculate(a, b) {
147148
return a > 0 ? (a - b) : (a + b);
@@ -168,7 +169,7 @@ class Viewer {
168169
}
169170

170171
return this;
171-
};
172+
}
172173

173174
_translatePanelEnd() {
174175
if (this.realWidth <= this.width && this.realHeight <= this.height)return this;
@@ -199,23 +200,21 @@ class Viewer {
199200
}
200201
this.needResetX = this.needResetY = false;
201202
return this;
202-
};
203+
}
203204

204205
isScale() {
205206
return Math.abs(this.scale - 1) > 0.01;
206-
};
207+
}
207208

208-
addAnimation(needAnimation) {
209-
if (needAnimation || needAnimation === undefined) {
210-
this.el.classList.add(ITEM_ANIMATION_CLASS);
211-
}
212-
return this;
213-
};
209+
addAnimation() {
210+
this.panelEl.classList.add(ITEM_ANIMATION_CLASS);
211+
this.el.classList.add(ITEM_ANIMATION_CLASS);
212+
}
214213

215214
removeAnimation() {
215+
this.panelEl.classList.remove(ITEM_ANIMATION_CLASS);
216216
this.el.classList.remove(ITEM_ANIMATION_CLASS);
217-
return this;
218-
};
217+
}
219218
}
220219

221220
export default Viewer;

src/css/index.css

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
.viewer-animation {
2-
-webkit-transition: transform .4s; /* Safari 和 Chrome */
3-
-moz-transition: transform .4s; /* Firefox 4 */
4-
-o-transition: transform .4s; /* Opera */
52
transition: transform .4s;
63
}
74

@@ -14,9 +11,8 @@
1411
background-color: black;
1512
z-index: 10000;
1613
font-size: 0;
17-
-webkit-transform: translate3d(0, 0, 0);
18-
-moz-transform: translate3d(0, 0, 0);
1914
transform: translate3d(0, 0, 0);
15+
backface-visibility: hidden;
2016
overflow: hidden;
2117
}
2218

@@ -26,9 +22,8 @@
2622
top: 50%;
2723
width: 100%;
2824
text-align: center;
29-
-webkit-transform: translate3d(0, -50%, 0);
30-
-moz-transform: translate3d(0, -50%, 0);
3125
transform: translate3d(0, -50%, 0);
26+
backface-visibility: hidden;
3227
}
3328

3429
.image-viewer .image-header {
Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
1-
var webpack = require('webpack');
2-
var path = require('path');
31
var config = {
42
entry: {
53
imageViewer: './src/index'
64
},
7-
output: {
8-
path: path.resolve(__dirname, 'build'),
9-
publicPath: '/build/',
10-
filename: '[name].js'
11-
},
125
module: {
136
noParse: /es6-promise\.js$/,
147
loaders: [
@@ -17,18 +10,18 @@ var config = {
1710
exclude: /node_modules/,
1811
loader: 'babel'
1912
},
20-
{test: /\.css$/, loader: 'style-loader!css-loader'}
13+
{test: /\.css$/, loader: 'style-loader!css-loader!postcss-loader'}
2114
]
2215
},
2316
babel: {
2417
presets: ['es2015'],
2518
plugins: ['transform-runtime']
2619
},
2720
plugins: [],
28-
devServer: {
29-
contentBase: './demo',
30-
host: '0.0.0.0',
31-
port: '8084'
21+
postcss: () => {
22+
return [
23+
require('autoprefixer')
24+
];
3225
}
3326
};
3427

webpack.config.dev.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
var webpack = require('webpack');
2+
var path = require('path');
3+
var baseConfig = require('./webpack.config.base');
4+
5+
baseConfig.output = {
6+
path: path.resolve(__dirname, 'build'),
7+
publicPath: '/build/',
8+
filename: '[name].js'
9+
};
10+
baseConfig.devServer = {
11+
contentBase: './demo',
12+
host: '0.0.0.0',
13+
port: '8084'
14+
};
15+
16+
module.exports = baseConfig;

webpack.config.dist.js

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,17 @@
11
var webpack = require('webpack');
22
var path = require('path');
3-
var config = {
4-
entry: {
5-
imageViewer: './src/index'
6-
},
7-
output: {
8-
path: path.resolve(__dirname, 'dist'),
9-
filename: '[name].min.js'
10-
},
11-
module: {
12-
noParse: /es6-promise\.js$/,
13-
loaders: [
14-
{
15-
test: /\.js$/,
16-
exclude: /node_modules/,
17-
loader: 'babel'
18-
},
19-
{test: /\.css$/, loader: 'style-loader!css-loader'}
20-
]
21-
},
22-
babel: {
23-
presets: ['es2015'],
24-
plugins: ['transform-runtime']
25-
},
26-
plugins: [
27-
new webpack.optimize.UglifyJsPlugin({
28-
compress: {
29-
warnings: false
30-
}
31-
})
32-
]
3+
var baseConfig = require('./webpack.config.base');
4+
5+
baseConfig.output = {
6+
path: path.resolve(__dirname, 'dist'),
7+
filename: '[name].min.js'
338
};
9+
baseConfig.plugins.push(
10+
new webpack.optimize.UglifyJsPlugin({
11+
compress: {
12+
warnings: false
13+
}
14+
})
15+
);
3416

35-
module.exports = config;
17+
module.exports = baseConfig;

0 commit comments

Comments
 (0)