当前位置:   article > 正文

【sgPhotoPlayer】自定义组件:图片预览,支持点击放大、缩小、旋转图片

【sgPhotoPlayer】自定义组件:图片预览,支持点击放大、缩小、旋转图片

特性:

  1. 支持设置初始索引值
  2. 支持显示标题、日期、大小、当前图片位置
  3. 支持无限循环切换轮播
  4. 支持鼠标滑轮滚动、左右键、上下键、PageUp、PageDown、Home、End操作切换图片
  5. 支持Esc关闭窗口

 sgPhotoPlayer源码

  1. <template>
  2. <div :class="$options.name" v-if="visible">
  3. <div class="swiper-container">
  4. <el-carousel
  5. class="image-swiper"
  6. ref="carousel"
  7. :initial-index="currentIndex"
  8. :autoplay="false"
  9. :height="'100%'"
  10. :indicator-position="swiperItems.length <= 1 ? 'none' : ''"
  11. :arrow="swiperItems.length <= 1 ? 'never' : ''"
  12. @change="(idx) => (currentIndex = idx)"
  13. >
  14. <el-carousel-item v-for="(a, $i) in swiperItems" :key="$i">
  15. <el-image
  16. draggable="false"
  17. style="width: 600px; height: 400px"
  18. :src="a.sm"
  19. :preview-src-list="swiperItems.map((v) => v.lg)"
  20. :initial-index="$i"
  21. :fit="'cover'"
  22. >
  23. </el-image>
  24. </el-carousel-item>
  25. </el-carousel>
  26. <div class="desc">
  27. <label class="number"
  28. >当前位置:{{ currentIndex + 1 }}/{{ swiperItems.length }}</label
  29. >
  30. <label
  31. class="title"
  32. :title="`${swiperItems[currentIndex].title}(${swiperItems[currentIndex].size})`"
  33. >{{ swiperItems[currentIndex].title }}({{ swiperItems[currentIndex].size }})
  34. </label>
  35. <label class="date">{{ swiperItems[currentIndex].date }}</label>
  36. </div>
  37. </div>
  38. <el-tooltip
  39. :content="`关闭`"
  40. :effect="`light`"
  41. :enterable="false"
  42. :placement="`top-start`"
  43. :popper-class="`sg-el-tooltip`"
  44. :transition="`none`"
  45. ><el-button
  46. class="sg-closeModalBtn"
  47. type="primary"
  48. icon="el-icon-close"
  49. @click="visible = false"
  50. circle
  51. />
  52. </el-tooltip>
  53. <div class="bg" @click="visible = false"></div>
  54. </div>
  55. </template>
  56. <script>
  57. export default {
  58. name: "sgPhotoPlayer",
  59. data() {
  60. return {
  61. currentIndex: 0,
  62. showBigImg: false,
  63. loading: false, //是否加载中
  64. visible: false, //是否显示
  65. swiperItems: [
  66. /* {
  67. title:`标题`,
  68. date:`时间`,
  69. size:`文件大小`,
  70. sm: "static/img/sm/1.jpg",//小图路径
  71. lg: "static/img/lg/1.jpg",//大图路径
  72. }, */
  73. ],
  74. };
  75. },
  76. props: [
  77. "data",
  78. "value", //是否显示
  79. ],
  80. watch: {
  81. value: {
  82. handler(d) {
  83. this.visible = d;
  84. },
  85. deep: true,
  86. immediate: true,
  87. },
  88. visible(d) {
  89. this.$emit("input", d);
  90. if (d) {
  91. this.$nextTick(() => {
  92. this.addEvents();
  93. });
  94. } else {
  95. this.removeEvents();
  96. }
  97. },
  98. data: {
  99. handler(newValue, oldValue) {
  100. if (newValue && Object.keys(newValue).length) {
  101. this.currentIndex = newValue.currentIndex; //默认显示的图片索引
  102. this.swiperItems = newValue.photos;
  103. }
  104. },
  105. deep: true, //深度监听
  106. immediate: true, //立即执行
  107. },
  108. },
  109. mounted() {},
  110. destroyed() {
  111. this.removeEvents();
  112. },
  113. methods: {
  114. addEvents(d) {
  115. this.removeEvents();
  116. addEventListener("mousewheel", this.mousewheel);
  117. addEventListener("keydown", this.keydown);
  118. addEventListener("keyup", this.keyup);
  119. },
  120. removeEvents(d) {
  121. removeEventListener("mousewheel", this.mousewheel);
  122. removeEventListener("keydown", this.keydown);
  123. removeEventListener("keyup", this.keyup);
  124. },
  125. mousewheel(e) {
  126. this.showBigImg = Boolean(document.querySelector(`.el-image-viewer__mask`));
  127. if (this.showBigImg) return;
  128. if (e.deltaY < 0) return this.$refs.carousel.prev();
  129. if (e.deltaY > 0) return this.$refs.carousel.next();
  130. },
  131. keydown(e) {
  132. let key = e.key.toLocaleLowerCase();
  133. if (
  134. [
  135. "escape",
  136. "home",
  137. "end",
  138. "pageup",
  139. "arrowup",
  140. "arrowleft",
  141. "pagedown",
  142. "arrowdown",
  143. "arrowright",
  144. ].includes(key)
  145. ) {
  146. e.preventDefault();
  147. return false;
  148. }
  149. },
  150. keyup(e) {
  151. this.showBigImg = Boolean(document.querySelector(`.el-image-viewer__mask`));
  152. let key = e.key.toLocaleLowerCase();
  153. if (
  154. ["escape", "home", "end", "pageup", "arrowup", "pagedown", "arrowdown"].includes(
  155. key
  156. ) &&
  157. this.showBigImg
  158. )
  159. return;
  160. switch (key) {
  161. case "escape":
  162. this.visible = false;
  163. break;
  164. case "home":
  165. this.$refs.carousel.setActiveItem(0);
  166. break;
  167. case "end":
  168. this.$refs.carousel.setActiveItem(this.swiperItems.length - 1);
  169. break;
  170. case "pageup":
  171. case "arrowup":
  172. case "arrowleft":
  173. this.$refs.carousel.prev();
  174. break;
  175. case "pagedown":
  176. case "arrowdown":
  177. case "arrowright":
  178. this.$refs.carousel.next();
  179. break;
  180. }
  181. },
  182. },
  183. };
  184. </script>
  185. <style lang="scss" scoped>
  186. .sgPhotoPlayer {
  187. position: fixed;
  188. left: 0;
  189. top: 0;
  190. width: 100%;
  191. height: 100%;
  192. z-index: 2000;
  193. display: flex;
  194. justify-content: center;
  195. align-items: center;
  196. .swiper-container {
  197. position: absolute;
  198. width: 600px;
  199. height: 450px;
  200. margin: auto;
  201. top: 0;
  202. left: 0;
  203. right: 0;
  204. bottom: 0;
  205. >>> .image-swiper {
  206. width: 100%;
  207. height: 100%;
  208. overflow: hidden;
  209. .el-carousel__indicators {
  210. bottom: revert;
  211. top: 400px;
  212. margin-top: 10px;
  213. display: flex;
  214. justify-content: center;
  215. flex-wrap: wrap;
  216. width: 100%;
  217. li {
  218. padding: 2px;
  219. }
  220. }
  221. .el-carousel__container {
  222. margin-top: -30px;
  223. .el-carousel__arrow,
  224. .el-carousel__item {
  225. transition: 0.382s !important;
  226. }
  227. .el-carousel__arrow {
  228. // transform: translateY(-40px);
  229. }
  230. .el-carousel__item {
  231. display: flex;
  232. justify-content: center;
  233. flex-direction: column;
  234. }
  235. }
  236. }
  237. .desc {
  238. width: 100%;
  239. display: flex;
  240. justify-content: space-between;
  241. align-items: center;
  242. flex-wrap: nowrap;
  243. box-sizing: border-box;
  244. margin: auto;
  245. /*文本阴影*/
  246. color: white;
  247. text-shadow: 1px 1px 1px black;
  248. line-height: 1.6;
  249. & > * {
  250. font-family: "Microsoft YaHei", "DIN-Light";
  251. font-weight: normal;
  252. font-size: 14px !important;
  253. white-space: nowrap;
  254. /*单行省略号*/
  255. overflow: hidden;
  256. white-space: nowrap;
  257. text-overflow: ellipsis;
  258. }
  259. .number {
  260. flex-shrink: 0;
  261. width: 140px;
  262. }
  263. .title {
  264. box-sizing: border-box;
  265. padding: 0 10px;
  266. }
  267. .date {
  268. flex-shrink: 0;
  269. width: 140px;
  270. }
  271. }
  272. }
  273. .bg {
  274. background-color: #000000cc;
  275. width: 100%;
  276. height: 100%;
  277. position: absolute;
  278. left: 0;
  279. top: 0;
  280. z-index: -1;
  281. }
  282. }
  283. </style>

应用

  1. // 打开图片
  2. openPhoto(d) {
  3. this.photoData= {
  4. currentIndex: this.photos.findIndex((v) => v.ID == d.ID), //当前图片索引值
  5. photos: this.photos.map((v) => ({
  6. sm: v.smURL,//小图路径
  7. lg: v.lgURL,//大图路径
  8. title: this.$g.getFileFullName(v),//标题
  9. date: v.GXSJ,//时间
  10. size: this.$g.getFileSize(v),//文件大小
  11. })),
  12. };
  13. this.showPhotoPlayer = true;
  14. },

基于elment-UI的el-carousel和el-image组件el-carousel和el-image组合实现swiper左右滑动图片,点击某张图片放大预览的效果_el-carousel 配合el-image preview-src-list-CSDN博客文章浏览阅读970次。【代码】el-carousel和el-image组合实现swiper左右滑动图片,点击某张图片放大预览的效果。_el-carousel 配合el-image preview-src-listhttps://blog.csdn.net/qq_37860634/article/details/131516077

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/274383
推荐阅读
相关标签
  

闽ICP备14008679号