index.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. <template>
  2. <transition name="msgbox-fade">
  3. <div class="preview-wrap" v-if="visible" ref="wrap">
  4. <div class="preview-mask" @click="visible = false" ref="mask"></div>
  5. <i class="el-icon-close" @click.stop="visible = false"></i>
  6. <img class="img" :src="imgUrl" alt="" ref="image"
  7. :style="{transform:`translate(${picLeft}px,${picTop}px) scale(${picScale})`}" @click="handleClickPC">
  8. <a :href="base64Url" :download="base64Stamp" class="icon-download"><i class="el-icon-download"></i></a>
  9. </div>
  10. </transition>
  11. </template>
  12. <script>
  13. import { isMobile, getBase64 } from '@/util/util'
  14. import Hammer from 'hammerjs'
  15. export default {
  16. name: 'imgPreview',
  17. data () {
  18. return {
  19. isPcClick: false, // 用作识别pc点击和拖拽
  20. isMobile: isMobile(),
  21. picW: 0, // 图片宽
  22. picH: 0, // 图片高
  23. curPicW: 0, // 实际展示宽高
  24. curPicH: 0, // 实际展示宽高
  25. picScale: 1, // 缩放比例
  26. picScaleMid: 1, // 缩放比例
  27. picInitTop: 0, // 最初top
  28. picInitLeft: 0, // 最初left
  29. picTop: 0,
  30. picLeft: 0,
  31. image: null, // target图
  32. disX: 0,
  33. disY: 0,
  34. base64Url: '',
  35. base64Stamp: 'meechat_' + (new Date()).getTime()
  36. }
  37. },
  38. methods: {
  39. initEvent () {
  40. if (this.isMobile) {
  41. this.handleH5()
  42. } else {
  43. this.handleDragPC()
  44. this.handleScalePC()
  45. this.$refs.mask.addEventListener('contextmenu', e => e.preventDefault())
  46. }
  47. },
  48. handleClickPC () {
  49. if (!this.isMobile && this.isPcClick) this.visible = false
  50. },
  51. // pc拖拽
  52. handleDragPC () {
  53. this.image.onmousedown = (ev) => {
  54. this.isPcClick = true
  55. this.dragStart(ev)
  56. if (this.image.setCapture) this.image.setCapture()
  57. document.addEventListener('mousemove', this.drag)
  58. document.addEventListener('mouseup', this.dragAfter)
  59. return false
  60. }
  61. },
  62. // pc缩放
  63. handleScalePC () {
  64. document.body.addEventListener('mousewheel', this.scale)
  65. },
  66. // h5拖拽、缩放
  67. handleH5 () {
  68. const mcWrap = new Hammer.Manager(this.$refs.wrap)
  69. const mc = new Hammer.Manager(this.image)
  70. const tap = new Hammer.Tap({ pointers: 1 })
  71. const pan = new Hammer.Pan({ pointers: 1 })
  72. const pinch = new Hammer.Pinch({ pointers: 2 })
  73. mc.add([tap, pan])
  74. mcWrap.add([pinch])
  75. // 监听-tab
  76. mc.on(`tap`, (ev) => {
  77. // ev.preventDefault()
  78. this.visible = false
  79. })
  80. // 监听-拖拽
  81. mc.on(`panstart panmove panend tap, multitap`, (ev) => {
  82. // ev.preventDefault()
  83. if (ev.type == 'panstart') this.dragStart(ev)
  84. else if (ev.type == 'panmove') this.drag(ev)
  85. else if (ev.type == 'panend') this.dragAfter(ev)
  86. })
  87. // 监听-缩放
  88. mcWrap.on(`pinchmove pinchin pinchout pinchend`, (ev) => {
  89. // ev.preventDefault()
  90. this.picScale = (this.picScaleMid * ev.scale).toFixed(2)
  91. if (ev.type == 'pinchend') {
  92. if (this.picScale < 1) this.picScale = 1
  93. this.picScaleMid = this.picScale
  94. }
  95. })
  96. },
  97. dragStart (ev) {
  98. let type = this.isMobile ? 'delta' : 'client'
  99. this.disY = ev[`${type}Y`] - this.picTop
  100. this.disX = ev[`${type}X`] - this.picLeft
  101. // 计算当前图片尺寸
  102. this.curPicW = this.picW * this.picScale
  103. this.curPicH = this.picH * this.picScale
  104. },
  105. // 拖拽
  106. drag (ev) {
  107. let type = this.isMobile ? 'delta' : 'client'
  108. this.isPcClick = false
  109. this.picTop = ev[`${type}Y`] - this.disY
  110. this.picLeft = ev[`${type}X`] - this.disX
  111. },
  112. dragAfter () {
  113. document.removeEventListener('mousemove', this.drag)
  114. document.removeEventListener('mouseup', this.dragAfter)
  115. if (this.image.releaseCapture) {
  116. this.image.releaseCapture()
  117. }
  118. if (this.picScale <= 1 || this.curPicW <= this.winW) {
  119. // 图片小于窗口-位置还原
  120. this.picTop = this.picInitTop
  121. this.picLeft = this.picInitLeft
  122. } else if (this.picScale > 1) {
  123. // 边缘检测
  124. let winW = window.innerWidth
  125. let winH = window.innerHeight
  126. let posX = (this.curPicW - winW) / 2
  127. let posY = (this.curPicH - winH) / 2
  128. if (this.picLeft < -posX) this.picLeft = -posX // 右边缘
  129. if (this.picLeft > posX) this.picLeft = posX // 左边缘
  130. if (this.picTop < -posY) this.picTop = -posY // 下边缘
  131. if (this.picTop > posY) this.picTop = posY // 上边缘
  132. if (posX < 0) this.picLeft = 0
  133. if (posY < 0) this.picTop = 0
  134. }
  135. },
  136. // 缩放
  137. scale (ev) {
  138. let picScale = this.picScale
  139. ev = ev || window.event
  140. if (ev.deltaY > 0) {
  141. this.picScale = picScale > 0.2 ? picScale - 0.1 : picScale
  142. } else {
  143. this.picScale += 0.1
  144. }
  145. if (this.picW * this.picScale - window.innerWidth < 0) this.picLeft = 0
  146. if (this.picH * this.picScale - window.innerHeight < 0) this.picTop = 0
  147. }
  148. },
  149. mounted () {
  150. this.image = this.$refs.image
  151. let img = new Image()
  152. img.src = this.imgUrl
  153. img.onload = () => {
  154. let winW = window.innerWidth
  155. let imgW = img.width
  156. let imgH = img.height
  157. this.picW = imgW > winW ? winW : imgW
  158. this.picH = imgW > winW ? winW * imgH / imgW : imgW
  159. this.initEvent()
  160. }
  161. getBase64(this.imgUrl, (data) => {
  162. this.base64Url = data
  163. })
  164. },
  165. destroyed () {
  166. }
  167. }
  168. </script>
  169. <style lang="scss" scoped>
  170. .preview-wrap{
  171. position: fixed;
  172. left: 0;
  173. right: 0;
  174. top: 0;
  175. bottom: 0;
  176. z-index: 999;
  177. background-color: rgba($color: #000000, $alpha: .8);
  178. text-align: center;
  179. overflow: hidden;
  180. &::after{
  181. content: '';
  182. display: inline-block;
  183. height: 100%;
  184. vertical-align: middle;
  185. }
  186. }
  187. .preview-mask{
  188. position: absolute;
  189. left: 0;
  190. right: 0;
  191. top: 0;
  192. bottom: 0;
  193. }
  194. .img{
  195. max-width: 100%;
  196. max-height: 100%;
  197. cursor: pointer;
  198. }
  199. .el-icon-close,.icon-download{
  200. position: absolute;
  201. right: 6px;
  202. top: 6px;
  203. font-size: 30px;
  204. cursor: pointer;
  205. z-index: 1000;
  206. color: #ffffff;
  207. }
  208. .icon-download{
  209. top: auto;
  210. bottom: 6px;
  211. }
  212. .h5-wrap {
  213. .preview-mask{
  214. background-color: #000000;
  215. }
  216. }
  217. </style>