Vue2 实现图片预览功能

在这里插入图片描述

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <link
      rel="stylesheet"
      href="https://unpkg.com/element-ui/lib/theme-chalk/index.css"
    />
    <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.13/vue.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/@unocss/runtime"></script>
    <script src="https://unpkg.com/element-ui/lib/index.js"></script>
    <style>
      * {
     
        margin: 0;
        padding: 0;
        box-sizing: border-box;
      }

      .overlay {
     
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background-color: rgba(0, 0, 0, 0.5);
      }

      .preview-container {
     
        position: fixed;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
      }

      .preview-container img {
     
        max-height: 80vh;
      }
    </style>
  </head>
  <body>
    <div id="app" class="h-100vh flex justify-center items-center">
      <div class="image-preview" v-if="showPreview">
        <div class="overlay"></div>

        <i
          class="el-icon-download cursor-pointer fixed text-40px top-40px right-90px"
          @click="changeImage('download')"
        ></i>

        <i
          class="el-icon-circle-close cursor-pointer fixed text-40px top-40px right-40px"
          @click="changeImage('change')"
        ></i>

        <div
          class="w-40px h-40px bg-#67686c cursor-pointer rounded-full flex justify-center items-center fixed left-40px"
          @click="changeImage('prev')"
        >
          <i class="el-icon-arrow-left text-#fff text-30px"></i>
        </div>

        <div
          class="w-40px h-40px bg-#67686c cursor-pointer rounded-full flex justify-center items-center fixed right-40px"
          @click="changeImage('next')"
        >
          <i class="el-icon-arrow-right text-#fff text-30px"></i>
        </div>

        <div class="preview-container">
          <img :src="imageUrl" alt="Preview Image" />
        </div>
      </div>

      <div class="thumbnail-container">
        <img
          v-for="(image, index) in images"
          :key="index"
          :src="image"
          class="h-200px w-200px"
          @click="showImage(index)"
        />
      </div>
    </div>
    <script>
      new Vue({
     
        el: '#app',
        data() {
     
          return {
     
            showPreview: false,
            currentIndex: 0,
            images: [
              'https://image.qwq.link/images/2023/11/24/twitter_zuukyuudo9_20231123-001852_1727481805832642619_photo.md.jpg',
              'https://image.qwq.link/images/2023/11/24/109626211_p0.md.jpg',
              'https://image.qwq.link/images/2023/11/24/113276508_p0.md.jpg',
            ],
          };
        },
        computed: {
     
          imageUrl() {
     
            return this.images[this.currentIndex];
          },
        },
        methods: {
     
          showImage(index) {
     
            this.currentIndex = index;
            this.showPreview = true;
          },
          changeImage(type) {
     
            const Fn = {
     
              prev: () => {
     
                if (this.currentIndex > 0) {
     
                  this.currentIndex--;
                } else {
     
                  this.currentIndex = this.images.length - 1;
                }
              },
              next: () => {
     
                if (this.currentIndex < this.images.length - 1) {
     
                  this.currentIndex++;
                } else {
     
                  this.currentIndex = 0;
                }
              },
              download: () => {
     
                // 注意这个只能下载本地图片不能下载网络图片
                var a = document.createElement('a');
                a.download = this.imageUrl || 'pic';
                a.href = this.imageUrl;
                a.click();
              },
              change: () => {
     
                this.showPreview = !this.showPreview;
              },
            };
            Fn[type]();
          },
        },

        created() {
     },
      });
    </script>
  </body>
</html>

相关推荐

  1. vue实现图片

    2023-12-10 03:48:03       6 阅读
  2. vue2自定义封装图片组件

    2023-12-10 03:48:03       31 阅读
  3. Vue【七】实现图片上传与

    2023-12-10 03:48:03       16 阅读
  4. vue3+vite使用viewerjs实现图片

    2023-12-10 03:48:03       9 阅读
  5. vue3之基于el-image实现图片

    2023-12-10 03:48:03       9 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-10 03:48:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-10 03:48:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-10 03:48:03       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-10 03:48:03       20 阅读

热门阅读

  1. Environment Variables Used by GPUDirect Storage

    2023-12-10 03:48:03       33 阅读
  2. 解释 Git 的基本概念和使用方式。

    2023-12-10 03:48:03       19 阅读
  3. 12.5每日一题(备战蓝桥杯小数运算、拆位练习)

    2023-12-10 03:48:03       24 阅读
  4. 【Spring篇】切点表达式语法规范

    2023-12-10 03:48:03       24 阅读
  5. 服务器数据损坏了有办法修复吗 ?

    2023-12-10 03:48:03       36 阅读
  6. [leetcode 双指针]

    2023-12-10 03:48:03       33 阅读
  7. 力扣labuladong——一刷day67

    2023-12-10 03:48:03       34 阅读
  8. 发送、接收消息,界面不及时刷新

    2023-12-10 03:48:03       41 阅读
  9. Linux 基础知识整理(五)

    2023-12-10 03:48:03       38 阅读
  10. linux中slab与slub的实现区别

    2023-12-10 03:48:03       29 阅读
  11. slot插槽

    2023-12-10 03:48:03       37 阅读
  12. 500 行代码 实现 的 Linux 容器 sandbox

    2023-12-10 03:48:03       46 阅读
  13. Spring MVC 接收请求参数所有方式2023-AI

    2023-12-10 03:48:03       32 阅读
  14. LeetCode435. Non-overlapping Intervals

    2023-12-10 03:48:03       27 阅读