image-viewer.vue 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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"></div>
  5. <!-- CLOSE -->
  6. <span class="el-image-viewer__btn el-image-viewer__close" @click="hide">
  7. <i class="el-icon-circle-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. 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. },
  155. deviceSupportInstall() {
  156. this._keyDownHandler = rafThrottle(e => {
  157. const keyCode = e.keyCode;
  158. switch (keyCode) {
  159. // ESC
  160. case 27:
  161. this.hide();
  162. break;
  163. // SPACE
  164. case 32:
  165. this.toggleMode();
  166. break;
  167. // LEFT_ARROW
  168. case 37:
  169. this.prev();
  170. break;
  171. // UP_ARROW
  172. case 38:
  173. this.handleActions('zoomIn');
  174. break;
  175. // RIGHT_ARROW
  176. case 39:
  177. this.next();
  178. break;
  179. // DOWN_ARROW
  180. case 40:
  181. this.handleActions('zoomOut');
  182. break;
  183. }
  184. });
  185. this._mouseWheelHandler = rafThrottle(e => {
  186. const delta = e.wheelDelta ? e.wheelDelta : -e.detail;
  187. if (delta > 0) {
  188. this.handleActions('zoomIn', {
  189. zoomRate: 0.015,
  190. enableTransition: false
  191. });
  192. } else {
  193. this.handleActions('zoomOut', {
  194. zoomRate: 0.015,
  195. enableTransition: false
  196. });
  197. }
  198. });
  199. on(document, 'keydown', this._keyDownHandler);
  200. on(document, mousewheelEventName, this._mouseWheelHandler);
  201. },
  202. deviceSupportUninstall() {
  203. off(document, 'keydown', this._keyDownHandler);
  204. off(document, mousewheelEventName, this._mouseWheelHandler);
  205. this._keyDownHandler = null;
  206. this._mouseWheelHandler = null;
  207. },
  208. handleImgLoad(e) {
  209. this.loading = false;
  210. },
  211. handleImgError(e) {
  212. this.loading = false;
  213. e.target.alt = '加载失败';
  214. },
  215. handleMouseDown(e) {
  216. if (this.loading || e.button !== 0) return;
  217. const { offsetX, offsetY } = this.transform;
  218. const startX = e.pageX;
  219. const startY = e.pageY;
  220. this._dragHandler = rafThrottle(ev => {
  221. this.transform.offsetX = offsetX + ev.pageX - startX;
  222. this.transform.offsetY = offsetY + ev.pageY - startY;
  223. });
  224. on(document, 'mousemove', this._dragHandler);
  225. on(document, 'mouseup', ev => {
  226. off(document, 'mousemove', this._dragHandler);
  227. });
  228. e.preventDefault();
  229. },
  230. reset() {
  231. this.transform = {
  232. scale: 1,
  233. deg: 0,
  234. offsetX: 0,
  235. offsetY: 0,
  236. enableTransition: false
  237. };
  238. },
  239. toggleMode() {
  240. if (this.loading) return;
  241. const modeNames = Object.keys(Mode);
  242. const modeValues = Object.values(Mode);
  243. const index = modeValues.indexOf(this.mode);
  244. const nextIndex = (index + 1) % modeNames.length;
  245. this.mode = Mode[modeNames[nextIndex]];
  246. this.reset();
  247. },
  248. prev() {
  249. if (this.isFirst && !this.infinite) return;
  250. const len = this.urlList.length;
  251. this.index = (this.index - 1 + len) % len;
  252. },
  253. next() {
  254. if (this.isLast && !this.infinite) return;
  255. const len = this.urlList.length;
  256. this.index = (this.index + 1) % len;
  257. },
  258. handleActions(action, options = {}) {
  259. if (this.loading) return;
  260. const { zoomRate, rotateDeg, enableTransition } = {
  261. zoomRate: 0.2,
  262. rotateDeg: 90,
  263. enableTransition: true,
  264. ...options
  265. };
  266. const { transform } = this;
  267. switch (action) {
  268. case 'zoomOut':
  269. if (transform.scale > 0.2) {
  270. transform.scale = parseFloat((transform.scale - zoomRate).toFixed(3));
  271. }
  272. break;
  273. case 'zoomIn':
  274. transform.scale = parseFloat((transform.scale + zoomRate).toFixed(3));
  275. break;
  276. case 'clocelise':
  277. transform.deg += rotateDeg;
  278. break;
  279. case 'anticlocelise':
  280. transform.deg -= rotateDeg;
  281. break;
  282. }
  283. transform.enableTransition = enableTransition;
  284. }
  285. },
  286. mounted() {
  287. this.deviceSupportInstall();
  288. // add tabindex then wrapper can be focusable via Javascript
  289. // focus wrapper so arrow key can't cause inner scroll behavior underneath
  290. this.$refs['el-image-viewer__wrapper'].focus();
  291. }
  292. };
  293. </script>