image-viewer.vue 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. <template>
  2. <transition name="viewer-fade">
  3. <div tabindex="-1" ref="el-image-viewer__wrapper" class="el-image-viewer__wrapper" :style="{ 'z-index': viewerZIndex }">
  4. <div class="el-image-viewer__mask" @click.self="handleMaskClick"></div>
  5. <!-- CLOSE -->
  6. <span class="el-image-viewer__btn el-image-viewer__close" @click="hide">
  7. <i class="el-icon-close"></i>
  8. </span>
  9. <!-- ARROW -->
  10. <template v-if="!isSingle">
  11. <span
  12. class="el-image-viewer__btn el-image-viewer__prev"
  13. :class="{ 'is-disabled': !infinite && isFirst }"
  14. @click="prev">
  15. <i class="el-icon-arrow-left"/>
  16. </span>
  17. <span
  18. class="el-image-viewer__btn el-image-viewer__next"
  19. :class="{ 'is-disabled': !infinite && isLast }"
  20. @click="next">
  21. <i class="el-icon-arrow-right"/>
  22. </span>
  23. </template>
  24. <!-- ACTIONS -->
  25. <div class="el-image-viewer__btn el-image-viewer__actions">
  26. <div class="el-image-viewer__actions__inner">
  27. <i class="el-icon-zoom-out" @click="handleActions('zoomOut')"></i>
  28. <i class="el-icon-zoom-in" @click="handleActions('zoomIn')"></i>
  29. <i class="el-image-viewer__actions__divider"></i>
  30. <i :class="mode.icon" @click="toggleMode"></i>
  31. <i class="el-image-viewer__actions__divider"></i>
  32. <i class="el-icon-refresh-left" @click="handleActions('anticlocelise')"></i>
  33. <i class="el-icon-refresh-right" @click="handleActions('clocelise')"></i>
  34. </div>
  35. </div>
  36. <!-- CANVAS -->
  37. <div class="el-image-viewer__canvas">
  38. <img
  39. v-for="(url, i) in urlList"
  40. v-if="i === index"
  41. ref="img"
  42. class="el-image-viewer__img"
  43. :key="url"
  44. :src="currentImg"
  45. :style="imgStyle"
  46. @load="handleImgLoad"
  47. @error="handleImgError"
  48. @mousedown="handleMouseDown">
  49. </div>
  50. </div>
  51. </transition>
  52. </template>
  53. <script>
  54. import { on, off } from 'element-ui/src/utils/dom';
  55. import { rafThrottle, isFirefox } from 'element-ui/src/utils/util';
  56. import { PopupManager } from 'element-ui/src/utils/popup';
  57. const Mode = {
  58. CONTAIN: {
  59. name: 'contain',
  60. icon: 'el-icon-full-screen'
  61. },
  62. ORIGINAL: {
  63. name: 'original',
  64. icon: 'el-icon-c-scale-to-original'
  65. }
  66. };
  67. const mousewheelEventName = isFirefox() ? 'DOMMouseScroll' : 'mousewheel';
  68. export default {
  69. name: 'elImageViewer',
  70. props: {
  71. urlList: {
  72. type: Array,
  73. default: () => []
  74. },
  75. zIndex: {
  76. type: Number,
  77. default: 2000
  78. },
  79. onSwitch: {
  80. type: Function,
  81. default: () => {}
  82. },
  83. onClose: {
  84. type: Function,
  85. default: () => {}
  86. },
  87. initialIndex: {
  88. type: Number,
  89. default: 0
  90. },
  91. appendToBody: {
  92. type: Boolean,
  93. default: true
  94. },
  95. maskClosable: {
  96. type: Boolean,
  97. default: true
  98. }
  99. },
  100. data() {
  101. return {
  102. index: this.initialIndex,
  103. isShow: false,
  104. infinite: true,
  105. loading: false,
  106. mode: Mode.CONTAIN,
  107. transform: {
  108. scale: 1,
  109. deg: 0,
  110. offsetX: 0,
  111. offsetY: 0,
  112. enableTransition: false
  113. }
  114. };
  115. },
  116. computed: {
  117. isSingle() {
  118. return this.urlList.length <= 1;
  119. },
  120. isFirst() {
  121. return this.index === 0;
  122. },
  123. isLast() {
  124. return this.index === this.urlList.length - 1;
  125. },
  126. currentImg() {
  127. return this.urlList[this.index];
  128. },
  129. imgStyle() {
  130. const { scale, deg, offsetX, offsetY, enableTransition } = this.transform;
  131. const style = {
  132. transform: `scale(${scale}) rotate(${deg}deg)`,
  133. transition: enableTransition ? 'transform .3s' : '',
  134. 'margin-left': `${offsetX}px`,
  135. 'margin-top': `${offsetY}px`
  136. };
  137. if (this.mode === Mode.CONTAIN) {
  138. style.maxWidth = style.maxHeight = '100%';
  139. }
  140. return style;
  141. },
  142. viewerZIndex() {
  143. const nextZIndex = PopupManager.nextZIndex();
  144. return this.zIndex > nextZIndex ? this.zIndex : nextZIndex;
  145. }
  146. },
  147. watch: {
  148. index: {
  149. handler: function(val) {
  150. this.reset();
  151. this.onSwitch(val);
  152. }
  153. },
  154. currentImg(val) {
  155. this.$nextTick(_ => {
  156. const $img = this.$refs.img[0];
  157. if (!$img.complete) {
  158. this.loading = true;
  159. }
  160. });
  161. }
  162. },
  163. methods: {
  164. hide() {
  165. this.deviceSupportUninstall();
  166. this.onClose();
  167. },
  168. deviceSupportInstall() {
  169. this._keyDownHandler = e => {
  170. e.stopPropagation();
  171. const keyCode = e.keyCode;
  172. switch (keyCode) {
  173. // ESC
  174. case 27:
  175. this.hide();
  176. break;
  177. // SPACE
  178. case 32:
  179. this.toggleMode();
  180. break;
  181. // LEFT_ARROW
  182. case 37:
  183. this.prev();
  184. break;
  185. // UP_ARROW
  186. case 38:
  187. this.handleActions('zoomIn');
  188. break;
  189. // RIGHT_ARROW
  190. case 39:
  191. this.next();
  192. break;
  193. // DOWN_ARROW
  194. case 40:
  195. this.handleActions('zoomOut');
  196. break;
  197. }
  198. };
  199. this._mouseWheelHandler = rafThrottle(e => {
  200. const delta = e.wheelDelta ? e.wheelDelta : -e.detail;
  201. if (delta > 0) {
  202. this.handleActions('zoomIn', {
  203. zoomRate: 0.015,
  204. enableTransition: false
  205. });
  206. } else {
  207. this.handleActions('zoomOut', {
  208. zoomRate: 0.015,
  209. enableTransition: false
  210. });
  211. }
  212. });
  213. on(document, 'keydown', this._keyDownHandler);
  214. on(document, mousewheelEventName, this._mouseWheelHandler);
  215. },
  216. deviceSupportUninstall() {
  217. off(document, 'keydown', this._keyDownHandler);
  218. off(document, mousewheelEventName, this._mouseWheelHandler);
  219. this._keyDownHandler = null;
  220. this._mouseWheelHandler = null;
  221. },
  222. handleImgLoad(e) {
  223. this.loading = false;
  224. },
  225. handleImgError(e) {
  226. this.loading = false;
  227. e.target.alt = '加载失败';
  228. },
  229. handleMouseDown(e) {
  230. if (this.loading || e.button !== 0) return;
  231. const { offsetX, offsetY } = this.transform;
  232. const startX = e.pageX;
  233. const startY = e.pageY;
  234. this._dragHandler = rafThrottle(ev => {
  235. this.transform.offsetX = offsetX + ev.pageX - startX;
  236. this.transform.offsetY = offsetY + ev.pageY - startY;
  237. });
  238. on(document, 'mousemove', this._dragHandler);
  239. on(document, 'mouseup', ev => {
  240. off(document, 'mousemove', this._dragHandler);
  241. });
  242. e.preventDefault();
  243. },
  244. handleMaskClick() {
  245. if (this.maskClosable) {
  246. this.hide();
  247. }
  248. },
  249. reset() {
  250. this.transform = {
  251. scale: 1,
  252. deg: 0,
  253. offsetX: 0,
  254. offsetY: 0,
  255. enableTransition: false
  256. };
  257. },
  258. toggleMode() {
  259. if (this.loading) return;
  260. const modeNames = Object.keys(Mode);
  261. const modeValues = Object.values(Mode);
  262. const index = modeValues.indexOf(this.mode);
  263. const nextIndex = (index + 1) % modeNames.length;
  264. this.mode = Mode[modeNames[nextIndex]];
  265. this.reset();
  266. },
  267. prev() {
  268. if (this.isFirst && !this.infinite) return;
  269. const len = this.urlList.length;
  270. this.index = (this.index - 1 + len) % len;
  271. },
  272. next() {
  273. if (this.isLast && !this.infinite) return;
  274. const len = this.urlList.length;
  275. this.index = (this.index + 1) % len;
  276. },
  277. handleActions(action, options = {}) {
  278. if (this.loading) return;
  279. const { zoomRate, rotateDeg, enableTransition } = {
  280. zoomRate: 0.2,
  281. rotateDeg: 90,
  282. enableTransition: true,
  283. ...options
  284. };
  285. const { transform } = this;
  286. switch (action) {
  287. case 'zoomOut':
  288. if (transform.scale > 0.2) {
  289. transform.scale = parseFloat((transform.scale - zoomRate).toFixed(3));
  290. }
  291. break;
  292. case 'zoomIn':
  293. transform.scale = parseFloat((transform.scale + zoomRate).toFixed(3));
  294. break;
  295. case 'clocelise':
  296. transform.deg += rotateDeg;
  297. break;
  298. case 'anticlocelise':
  299. transform.deg -= rotateDeg;
  300. break;
  301. }
  302. transform.enableTransition = enableTransition;
  303. }
  304. },
  305. mounted() {
  306. this.deviceSupportInstall();
  307. if (this.appendToBody) {
  308. document.body.appendChild(this.$el);
  309. }
  310. // add tabindex then wrapper can be focusable via Javascript
  311. // focus wrapper so arrow key can't cause inner scroll behavior underneath
  312. this.$refs['el-image-viewer__wrapper'].focus();
  313. },
  314. destroyed() {
  315. // if appendToBody is true, remove DOM node after destroy
  316. if (this.appendToBody && this.$el && this.$el.parentNode) {
  317. this.$el.parentNode.removeChild(this.$el);
  318. }
  319. }
  320. };
  321. </script>