Skip to content

Commit b7dbb38

Browse files
committed
1.update: Touch类代码调整;
2.update: 渐变动画调整;
1 parent b0916a8 commit b7dbb38

File tree

6 files changed

+123
-129
lines changed

6 files changed

+123
-129
lines changed

src/common/utils.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,17 @@ const isPlainObject = value => {
1616
return Object.prototype.toString.call(value) === '[object Object]';
1717
};
1818

19+
/**
20+
* 比较两个值(取绝对值)
21+
* @param a
22+
* @param b
23+
*/
24+
const compareNumberABS = (a = 0, b = 0) => {
25+
return Math.abs(a - b) > 0.05;
26+
};
27+
1928
export {
2029
isNumber,
21-
isPlainObject
30+
isPlainObject,
31+
compareNumberABS
2232
};

src/core/imageViewer.js

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ import {
1313
CENTER_IMG,
1414
RIGHT_IMG
1515
} from '../common/profile';
16-
import {isNumber, isPlainObject} from '../common/utils';
16+
import {
17+
isNumber,
18+
isPlainObject,
19+
compareNumberABS
20+
} from '../common/utils';
1721
import lock from '../common/lock';
1822
import Touch from './touch';
1923
import Viewer from './viewer';
@@ -24,7 +28,7 @@ const defaultImgOption = {thumbnail: '', url: ''};
2428
class ImageViewer {
2529
constructor(images = [], opt = {}) {
2630
this.opt = opt;
27-
this.duration = this.opt.duration || 10;
31+
this.duration = this.opt.duration || 14;
2832
this.el = null;
2933
this.headerEl = null;
3034
this.bodyEl = null;
@@ -303,27 +307,27 @@ class ImageViewer {
303307
const animationFn = () => {
304308
if (nextAnimation) {
305309
nextAnimation = false;
306-
if (Math.abs(rect.top - data.top - offsetTop) >= 0.5) {
310+
if (compareNumberABS(rect.top, data.top + offsetTop)) {
307311
offsetTop += stepTop;
308312
nextAnimation = true;
309313
}
310-
if (Math.abs(rect.left - data.left - offsetLeft) >= 0.5) {
314+
if (compareNumberABS(rect.left, data.left + offsetLeft)) {
311315
offsetLeft += stepLeft;
312316
nextAnimation = true;
313317
}
314-
if (Math.abs(rect.width - data.width) >= 0.5) {
318+
if (compareNumberABS(rect.width, data.width)) {
315319
data.width += stepWidth;
316320
style.width = data.width + 'px';
317321
nextAnimation = true;
318322
}
319-
if (Math.abs(rect.height - data.height) >= 0.5) {
323+
if (compareNumberABS(rect.height, data.height)) {
320324
data.height += stepHeight;
321325
style.height = data.height + 'px';
322326
nextAnimation = true;
323327
}
324328
if (currentOpacity < 1) {
325329
currentOpacity += stepOpacity;
326-
this.el.style.opacity = currentOpacity;
330+
this.bodyEl.style.opacity = currentOpacity;
327331
nextAnimation = true;
328332
}
329333
setTranslateStyle(this.animationEl, offsetLeft.toFixed(4), offsetTop.toFixed(4));
@@ -344,11 +348,10 @@ class ImageViewer {
344348
}
345349

346350
_fadeOut(callback) {
347-
// @todo: 将透明度渐变和尺寸渐变分离开,先尺寸渐变完成后再渐变透明度
348351
if (this.opt.fadeOutFn) {
349352
const image = this._getCurrentImage();
350353
const duration = this.duration;
351-
const data = this._getPositionAndSize(this.opt.fadeOutFn(this.currentIndex));
354+
const data = this._getPositionAndSize(this.opt.fadeInFn(this.currentIndex));
352355
const style = this.animationEl.style;
353356
style.top = data.top + 'px';
354357
style.left = data.left + 'px';
@@ -357,12 +360,10 @@ class ImageViewer {
357360
const rect = this._getPositionAndSize(currentViewer.el);
358361
let differTop = rect.top - data.top;
359362
let differLeft = rect.left - data.left;
360-
let differWidth = rect.width - data.width;
361-
let differHeight = rect.height - data.height;
362363
let stepTop = differTop / duration;
363364
let stepLeft = differLeft / duration;
364-
let stepWidth = differWidth / duration;
365-
let stepHeight = differHeight / duration;
365+
let stepWidth = (rect.width - data.width) / duration;
366+
let stepHeight = (rect.height - data.height) / duration;
366367
let stepOpacity = 1 / duration;
367368
let currentOpacity = 1;
368369
let currentWidth = rect.width;
@@ -375,27 +376,29 @@ class ImageViewer {
375376
const animationFn = () => {
376377
if (nextAnimation) {
377378
nextAnimation = false;
378-
if (Math.abs(differTop) > 0.5) {
379+
if (compareNumberABS(differTop)) {
379380
differTop -= stepTop;
380381
nextAnimation = true;
381382
}
382-
if (Math.abs(differLeft) > 0.5) {
383+
if (compareNumberABS(differLeft)) {
383384
differLeft -= stepLeft;
384385
nextAnimation = true;
385386
}
386-
if (currentWidth >= data.width) {
387+
if (compareNumberABS(currentWidth, data.width)) {
387388
currentWidth -= stepWidth;
389+
currentWidth = compareNumberABS(currentWidth, data.width) ? currentWidth : data.width;
388390
style.width = currentWidth + 'px';
389391
nextAnimation = true;
390392
}
391-
if (currentHeight >= data.height) {
393+
if (compareNumberABS(currentHeight, data.height)) {
392394
currentHeight -= stepHeight;
395+
currentHeight = compareNumberABS(currentHeight, data.height) ? currentHeight : data.height;
393396
style.height = currentHeight + 'px';
394397
nextAnimation = true;
395398
}
396399
if (currentOpacity > 0) {
397400
currentOpacity -= stepOpacity;
398-
this.el.style.opacity = currentOpacity;
401+
this.bodyEl.style.opacity = currentOpacity >= 0 ? currentOpacity : 0;
399402
nextAnimation = true;
400403
}
401404
setTranslateStyle(this.animationEl, differLeft.toFixed(4), differTop.toFixed(4));
@@ -532,10 +535,11 @@ class ImageViewer {
532535

533536
close() {
534537
if (this.el) {
535-
this.bodyEl.style.visibility = 'hidden';
538+
this.viewerWrapperEl.style.visibility = 'hidden';
536539
this._fadeOut(() => {
537-
this.el.style.display = 'none';
540+
this.animationEl.children[0].src = '';
538541
this._getCurrentViewer().clearImg();
542+
this.el.style.display = 'none';
539543
});
540544
}
541545
}
@@ -550,15 +554,15 @@ class ImageViewer {
550554
}
551555

552556
if (this.opt.fadeInFn) {
553-
this.el.style.opacity = 0;
554-
this.bodyEl.style.visibility = 'hidden';
557+
this.bodyEl.style.opacity = 0;
558+
this.viewerWrapperEl.style.visibility = 'hidden';
555559
}
556560
this.el.style.display = 'block';
557561
this.swipeInByIndex(this.currentIndex, false, () => {
558562
window.requestAnimationFrame(() => {
559563
this._fadeIn(() => {
560-
this.el.style.opacity = 1;
561-
this.bodyEl.style.visibility = 'visible';
564+
this.bodyEl.style.opacity = 1;
565+
this.viewerWrapperEl.style.visibility = 'visible';
562566
// 下面这个再次调用是为了加载大图
563567
this.swipeInByIndex(this.currentIndex);
564568
});

0 commit comments

Comments
 (0)