imagePreviewer.vue 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. <template>
  2. <popup v-if="ifShow">
  3. <div class="preview-wrapper">
  4. <div class="title">
  5. <i class="iconfont icon-material_image title-icon"/>
  6. {{imageTitleList[currentIndex]}}
  7. </div>
  8. <img class="close-btn" :src="require('@/assets/images/icons/material_preview_close@2x.png')" @click=onClickClose />
  9. <img
  10. class="image"
  11. :src="imageList[currentIndex]"
  12. :style="imageStyle"
  13. @wheel.prevent="onImageWheel"
  14. />
  15. <div class="toolbar">
  16. <i
  17. class="iconfont icon-material_preview_previous hover-tips" :class="{disabled: currentIndex === 0}" @click="onClickPrevious()">
  18. <div>
  19. <div class="remark">{{$i18n.t(`material.components.prev`)}}</div>
  20. </div>
  21. </i>
  22. <i class="iconfont icon-material_preview_next1 hover-tips append-splitter" :class="{disabled: currentIndex === imageList.length - 1}" @click="onClickNext()">
  23. <div>
  24. <div class="remark">{{$i18n.t(`material.components.next`)}}</div>
  25. </div>
  26. </i>
  27. <i v-if="canScale" class="iconfont icon-material_preview_enlarge hover-tips" @click="onClickZoomIn()">
  28. <div>
  29. <div class="remark">{{$i18n.t(`material.components.zoom_in`)}}</div>
  30. </div>
  31. </i>
  32. <i v-if="canScale" class="iconfont icon-material_preview_narrow hover-tips" @click="onClickZoomOut()">
  33. <div>
  34. <div class="remark">{{$i18n.t(`material.components.zoom_out`)}}</div>
  35. </div>
  36. </i>
  37. <i class="iconfont icon-material_preview_next hover-tips-warn" @click="onClickDelete()">
  38. <div>
  39. <div class="remark">{{$i18n.t(`material.components.delete`)}}</div>
  40. </div>
  41. </i>
  42. <i v-if="canFullScreen && objectFit === 'scale-down'" class="iconfont icon-material_preview_full_screen hover-tips" @click="onClickFullScreen()">
  43. <div>
  44. <div class="remark">{{$i18n.t(`material.components.fullscreen`)}}</div>
  45. </div>
  46. </i>
  47. <i v-if="canFullScreen && objectFit === 'contain'" class="iconfont icon-material_preview_drop_out hover-tips" @click="onClickCancelFullScreen()">
  48. <div>
  49. <div class="remark">{{$i18n.t(`material.components.cancel_fullscreen`)}}</div>
  50. </div>
  51. </i>
  52. </div>
  53. </div>
  54. </popup>
  55. </template>
  56. <script>
  57. import Popup from "@/components/shared/popup";
  58. export default {
  59. props: {
  60. imageTitleList: {
  61. type: Array,
  62. default: () => {
  63. return [
  64. 'aaa',
  65. 'bbb'
  66. ]
  67. }
  68. },
  69. imageList: {
  70. type: Array,
  71. default: () => {
  72. return [
  73. 'https://ossxiaoan.4dage.com/720yun_fd_manage/fodder/20220125_114634855.jpg',
  74. 'https://ossxiaoan.4dage.com/720yun_fd_manage/fodder/20220125_142545584.jpg',
  75. ]
  76. }
  77. },
  78. canScale: {
  79. type: Boolean,
  80. default: true
  81. },
  82. canFullScreen: {
  83. type: Boolean,
  84. default: true
  85. },
  86. },
  87. components:{
  88. Popup
  89. },
  90. data(){
  91. return {
  92. ifShow: false,
  93. currentIndex: 0,
  94. scaleRate: 1,
  95. objectFit: 'scale-down',
  96. }
  97. },
  98. computed: {
  99. imageStyle() {
  100. return {
  101. transform: `scale(${this.scaleRate})`,
  102. objectFit: this.objectFit,
  103. }
  104. }
  105. },
  106. watch: {
  107. imageList: {
  108. handler: function (newList) {
  109. if (newList.length - 1 < this.currentIndex) {
  110. this.currentIndex = newList.length - 1
  111. }
  112. }
  113. }
  114. },
  115. methods:{
  116. show(index) {
  117. Number.isSafeInteger(index) && (this.currentIndex = index)
  118. this.ifShow = true
  119. },
  120. onImageWheel(e) {
  121. if (e.deltaY < 0) {
  122. this.scaleRate = this.scaleRate * 1.1
  123. } else {
  124. this.scaleRate = this.scaleRate * 0.9
  125. }
  126. },
  127. onClickPrevious() {
  128. if (this.currentIndex > 0) {
  129. this.currentIndex--
  130. }
  131. },
  132. onClickNext() {
  133. if (this.currentIndex < this.imageList.length - 1) {
  134. this.currentIndex++
  135. }
  136. },
  137. onClickZoomIn() {
  138. this.scaleRate = this.scaleRate * 1.1
  139. },
  140. onClickZoomOut() {
  141. this.scaleRate *= 0.9
  142. },
  143. onClickDelete() {
  144. this.$emit('click-delete', this.currentIndex)
  145. },
  146. onClickFullScreen() {
  147. this.scaleRate = 1
  148. this.objectFit = 'contain'
  149. },
  150. onClickCancelFullScreen() {
  151. this.scaleRate = 1
  152. this.objectFit = 'scale-down'
  153. },
  154. onClickClose() {
  155. this.ifShow = false
  156. }
  157. },
  158. mounted() {
  159. this.$bus.on('clickEsc',()=>{
  160. this.onClickClose()
  161. })
  162. },
  163. }
  164. </script>
  165. <style scoped lang="less">
  166. .preview-wrapper {
  167. position: fixed;
  168. top: 0;
  169. left: 0;
  170. right: 0;
  171. bottom: 0;
  172. .title {
  173. position: absolute;
  174. top: 16px;
  175. left: 30px;
  176. z-index: 2;
  177. height: 36px;
  178. font-size: 14px;
  179. color: #FFFFFF;
  180. background: rgba(0, 0, 0, 0.6);
  181. border-radius: 18px;
  182. display: flex;
  183. justify-content: center;
  184. align-items: center;
  185. padding-left: 16px;
  186. padding-right: 16px;
  187. .title-icon {
  188. margin-right: 6px;
  189. }
  190. }
  191. .close-btn {
  192. position: absolute;
  193. top: 16px;
  194. right: 30px;
  195. width: 36px;
  196. height: 36px;
  197. z-index: 2;
  198. cursor: pointer;
  199. }
  200. .toolbar {
  201. position: absolute;
  202. bottom: 147px;
  203. left: 50%;
  204. transform: translateX(-50%);
  205. height: 60px;
  206. display: flex;
  207. justify-content: center;
  208. align-items: center;
  209. padding: 0 43px;
  210. background: rgba(0, 0, 0, 0.6);
  211. border-radius: 8px;
  212. z-index: 2;
  213. .iconfont {
  214. cursor: pointer;
  215. color: white;
  216. margin-right: 36px;
  217. font-size: 22px;
  218. &.disabled {
  219. opacity: 0.5;
  220. pointer-events: none;
  221. }
  222. &:last-child {
  223. margin-right: 0;
  224. }
  225. }
  226. .append-splitter {
  227. &::after {
  228. pointer-events: none;
  229. content: "|";
  230. position: absolute;
  231. right: -18px;
  232. top: -4px;
  233. font-size: 20px;
  234. color: rgba(255, 255, 255, 0.5);
  235. }
  236. }
  237. }
  238. .image {
  239. top: 0;
  240. left: 0;
  241. width: 100%;
  242. height: 100%;
  243. position: absolute;
  244. z-index: 1;
  245. }
  246. }
  247. .hover-tips, .hover-tips-warn {
  248. position: relative;
  249. font-size: 18px;
  250. &:hover {
  251. > div {
  252. display: block;
  253. }
  254. }
  255. // tip的方框
  256. > div {
  257. background: rgba(0, 0, 0, 0.6);
  258. border: none;
  259. cursor: default;
  260. display: none;
  261. z-index: 10000;
  262. position: absolute;
  263. left: 50%;
  264. transform: translateX(-50%);
  265. top: -60px;
  266. color: #fff;
  267. pointer-events: none;
  268. text-align: center;
  269. word-break: keep-all;
  270. padding: 0 8px;
  271. font-size: 12px;
  272. border-radius: 3px;
  273. // tip的箭头
  274. &::before {
  275. border: 7px solid transparent;
  276. border-top: 7px solid rgba(0, 0, 0, 0.6);
  277. width: 0;
  278. height: 0px;
  279. content: "";
  280. display: inline-block;
  281. position: absolute;
  282. bottom: -14px;
  283. left: 50%;
  284. transform: translateX(-50%);
  285. }
  286. // tip的文字
  287. .remark {
  288. line-height: 2.5;
  289. color: #fff;
  290. }
  291. }
  292. }
  293. </style>