image-viewer.vue 8.2 KB

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