NoteZ_技术博客 NoteZ_技术博客
🏠 首页
  • 📚 Web技术
  • 📋 Npm笔记
  • 📑 Markdown
  • 📄 Git笔记
  • 📝 Nginx文档
  • 📓 Linux文档
  • 📖 技术文档
  • 📜 其他文档
  • 🧊 NodeJs
  • 🎡 Express
  • 🔥 Rust
  • 🎉 Koa2
  • 🍃 MongoDB
  • 🐬 MySql
  • 🥦 Oracle
  • 🍁 Python
  • 🍄 JavaScript
  • 🌰 CSS
  • 🧄 HTML
  • 🥑 Canvas
  • 🌽 Nuxt
  • 🍆 React
  • 🥜 Vue
  • 🧅 TypeScript
  • 🌶️ AI
  • 📘 分类
  • 📗 标签
  • 📙 归档
⚜️ 在线编辑 (opens new window)
  • 📁 站点收藏
  • 📦 前端组件库
  • 📊 数据可视化
  • 🌈 开源插件
  • 🎗️ 关于我
  • 🔗 友情链接
GitHub (opens new window)

NoteZ_技术博客

前端界的小学生
🏠 首页
  • 📚 Web技术
  • 📋 Npm笔记
  • 📑 Markdown
  • 📄 Git笔记
  • 📝 Nginx文档
  • 📓 Linux文档
  • 📖 技术文档
  • 📜 其他文档
  • 🧊 NodeJs
  • 🎡 Express
  • 🔥 Rust
  • 🎉 Koa2
  • 🍃 MongoDB
  • 🐬 MySql
  • 🥦 Oracle
  • 🍁 Python
  • 🍄 JavaScript
  • 🌰 CSS
  • 🧄 HTML
  • 🥑 Canvas
  • 🌽 Nuxt
  • 🍆 React
  • 🥜 Vue
  • 🧅 TypeScript
  • 🌶️ AI
  • 📘 分类
  • 📗 标签
  • 📙 归档
⚜️ 在线编辑 (opens new window)
  • 📁 站点收藏
  • 📦 前端组件库
  • 📊 数据可视化
  • 🌈 开源插件
  • 🎗️ 关于我
  • 🔗 友情链接
GitHub (opens new window)
  • JavaScript笔记

  • CSS笔记

  • HTML笔记

  • Canvas笔记

  • Nuxt笔记

  • React笔记

  • Vue笔记

    • markdown(md-editor-v3)简单使用
    • Prop 验证 与 非 Prop 的 Attribute
    • transition-group列表过渡
    • transition过渡&动画
    • vetur 中 eslint 的问题
    • vite 打包时内存溢出处理方案
    • Vite2 + Vue3添加svg的使用并通过svg实现自定义Icon组件
    • Vite项目构建优化之移除console.log和其他生产日志
    • vsCode 之 Monaco Editor 在 vue-cli3.0 中的使用方法
    • vue cli3 中的使用Luckysheet和Luckyexcel
    • vue 与 Koa 上传示例代码
    • vue 中 axios 如何取消请求,取消前面一个或多个请求
    • Vue 中 node-sass 安装失败简单解决方案
    • vue 中使用 pinia 状态管理教程
    • vue 中使用 web worker
    • vue 中封装 Axios 和 管理 API 接口
    • vue 中的 hash 与 history 两种路由模式
    • Vue 中的鼠标事件汇总
    • Vue 实现 Base64 图片上传
    • vue 强制刷新组件
    • Vue 组件中 v-mode 的本质以及实现方式
    • vue 组件中的.native 原生事件(@click.native)
    • Vue 组件中的.native原生事件(@click.native)
    • Vue 页面路由(router)权限控制和登陆验证
    • Vue 项目中如何在打包时清除 console 和 debugger
    • Vue 项目打包去除 .map 文件方法
    • Vue 预渲染之 prerender-spa-plugin 的配置使用
    • vue-cli 2.x和3.x配置移动端适配px自动转为rem
    • vue-cli3 如何实现pc端自适应
    • vue-cli3.0 开启 Gzip 压缩方法
    • Vue-cli3.0 监听路由变化的几种方式
    • Vue-cli3.0 结合 axios 解决生产环境中的跨域问题
    • vue-cli3.0移动端项目将px转换成rem
    • vue-cli3.x项目兼容ie解决方案
    • vue-router传递参数的几种方式
    • Vue2-ace-editor 基本使用方法
    • vue2.x 插槽slot使用总结
    • Vue2.x 组件通信方式
    • vue3 + vite 自适应(rem 适配)方法
    • vue3+vite:本地代理,配置proxy
    • Vue3.0 基础知识点总结
    • Vue3.0 挂载方法,添加全局属性
    • vue3.0之全局变量app.config.globalProperties的使用
    • vue3.2+elelmenui-plus+vite 按需导入ui组件库
    • vue3.x 中使用 ol (openlayer)地图
    • Vue3.x 组件通信方式
    • Vue3全局变量的定义和使用
    • Vuex使用记录
    • vue中使用html2canvas将html页面转为图片并且下载该图片
    • vue中父子组件之间的通信
    • Vue中的防抖函数封装和使用
    • Vue中配合clipboard.js实现点击按钮复制内容到剪切板
    • vue使用monaco editor时报错 Unexpected usage at EditorSimpleWorker.loadForeignModule
    • Vue列表渲染之数组、对象更新检测
    • Vue实现下拉滚动加载刷新功能
    • Vue开发中安装库经常报错
    • Vue技术框架
    • Vue生命周期
    • Vue监听store中数据变化的两种方式
    • Vue非父子组件之间通信的几种方式
    • Vue项目 解决 vuex 在页面刷新后数据丢失的问题
    • vue项目nginx部署子目录_vue 多项目部署_二级目录 copy
    • 使用 monaco-editor 汉化 右键菜单汉化
    • 使用 vite 脚手架创建 vue3 项目
    • 使用animate库
    • 使用vue实现鼠标框选文件的功能
      • 示例代码
      • 效果图
      • 核心代码
        • html代码
        • css代码
        • js代码
    • 使用vue的transition标签结合css样式完成动画
    • 关于Vue项目优化方案总结
    • 关于解决vue-cli2.xx、vue-cli3.xx项目在ie中白屏的方案总结
    • 利用create-nuxt-app脚手架创建NuxtJS应用
    • 国际化插件 vue-i18n 使用方法
    • 在 vite-vue3.x 中的使用 vscode monaco-editor 方法
    • 在 Vue 中 如何使用 jsencrypt 实现 Rsa 进行加密
    • 在 vue 中使用 jsPlumb 总结
    • 在 vue 中使用 v-viewer 预览图方法
    • 在 Vue 中读取本地文本文件(兼容各种浏览器)
    • 在 vue 或 vuex 中如何使用 axios 进行请求操作
    • 在 vue 项目中 mock 的基本使用方法
    • 在 vue-cli3 项目更改 favicon 图标
    • 在 vue-cli3.0 中使用 Sass 全局变量
    • 在vue3+vite中使用markdown(v-md-editor)编辑器
    • 在vue中如何使用watch深度监听对象中值的变化
    • 基于 vue 列表拖拽排序
    • 基于 vue 图片裁剪(vue-cropper)
    • 基于 vue 的 CodeMirror 代码编辑器
    • 如何在 vue 中使用 swiper 插件创建轮播图
    • 如何在 vue 中实现代码高亮?
    • 如何在 Vue-cli3.0 中使用 prerender-spa-plugin 进行预渲染
    • 如何用 vue 实现一个虚拟列表
    • 清除项目中无用的console.log代码方法
    • 解决vue-cli3.0中提示syntax 'classProperties' isn't currently enabled的错误
    • 记录vite打包vue项目内存溢出问题及解决方法
    • Vite中使用 svg-sprite-loader加载svg文件
    • vite引入svg图片
    • vue 使用CompressionPlugin压缩解决打包后文件过大的问题
    • vue2.x递归组件 树形组件
    • vue3.x递归组件 树形组件
    • 使用Vite快速构建Vue3+ts+pinia脚手架
    • 启动vue项目时报错digital envelope routines__unsupported
    • Pinia 快速入门(使用教程)
    • 关于 Vite Vue 辨别当前所处于什么(生产、开发)环境
  • TypeScript笔记

  • AI相关笔记

  • 开发文档
  • Vue笔记
NoteZ
2022-10-12
目录
示例代码
效果图
核心代码
html代码
css代码
js代码

使用vue实现鼠标框选文件的功能

# 示例代码

<template>
  <div
    ref="fileTable"
    tabindex="0"
    class="file-table"
    @click.stop="handleClickWarp(data)"
    @contextmenu.prevent.stop="
      handleContextmenu({ event: $event, data: data, type: 'empty' })
    "
    @mousedown.left="onmousedownClick"
    @mouseup="handMouseUp"
    @mousemove="onmouseMove"
  >
    <!-- @click.stop="handleBoxClick(data)" -->
    <div class="list-inner" v-if="tableData && tableData.length">
      <ul class="block-box" v-if="type === 'block'">
        <li
          v-for="item in tableData"
          :key="item.hash + '_' + item.fileId + item.isSelect"
          :id="item.hash"
          :class="[
            { cursor: item.mime === 'directory' },
            { active: item.isSelect },
            { isVirtualization: item.isVirtualization },
            { mousehover: !mouseState.isMove },
          ]"
          @click.stop="handleItemClick(item, true)"
          @dblclick.stop="handleDblClick(item)"
          @contextmenu.prevent.stop="
            handleContextmenu({ event: $event, data: item, type: 'file' })
          "
          :title="item.name"
          name="file"
        >
          <div class="icon-warp">
            <img :src="item.icon" />
          </div>
          <div v-if="item.isInput" class="edit">
            <input
              type="text"
              :id="'input_' + item.hash"
              autofocus="autofocus"
              v-model="item.reName"
              @click.stop
              @keyup="handleKeyUp($event, item.reName)"
              @blur="handleOnBlur(item.reName)"
            />
          </div>
          <p v-else class="name">{{ item.name }}</p>
        </li>
      </ul>
      <Table :columns="columns" :data="tableData" v-if="type === 'table'">
        <template slot-scope="{ row }" slot="name">
          <div
            class="name-warp"
            @click.stop="handleItemClick(row, true)"
            @dblclick.stop="handleDblClick(row)"
            @contextmenu.prevent.stop="
              handleContextmenu({ event: $event, data: row, type: 'file' })
            "
          >
            <div class="icon-warp">
              <img :src="row.icon" />
            </div>
            <div v-if="row.isInput" class="edit">
              <input
                type="text"
                :id="'input_' + row.hash"
                autofocus="autofocus"
                v-model="row.reName"
                @keyup="handleKeyUp($event, row.reName)"
                @blur="handleOnBlur(row.reName)"
              />
            </div>
            <p v-else class="name">{{ row.name }}</p>
          </div>
        </template>
        <template slot-scope="{ row }" slot="power">
          <div class="power-warp">
            <p>
              {{ row.read == 1 ? "可读取" : "不可读" }} 和
              {{ row.write == 1 ? "可写入" : "不可写" }}
            </p>
          </div>
        </template>
        <template slot-scope="{ row }" slot="changeTime">
          <div><Time :time="row.ts" /></div>
        </template>
        <template slot-scope="{ row }" slot="size">
          <span>{{ row.formatSize }}</span>
        </template>
        <template slot-scope="{ row }" slot="type">
          <span v-if="row.mime == 'directory'">{{ fileMapTip["folder"] }}</span>
          <span v-else>{{
            fileMapTip[row.suffix] || fileMapTip["unknown"]
          }}</span>
        </template>
      </Table>
      <div id="rectangular"></div>
    </div>
    <NotDataLoading :loading="loading" :data="tableData">
      暂无任何文件
    </NotDataLoading>
  </div>
</template>
<script>
import { formatBytes } from "@/utils/index";
import { fileImgMapSuffix, getRequestId } from "@/utils/files.js";
import { cloneDeep } from "lodash";
export default {
  name: "FileTable",
  data() {
    return {
      tableData: [],
      content: {},
      isReName: false, 
      type: "block", // table block
      sort: "size",
      mouseState: {
        isDown: false,
        isMove: false,
      },
      columns: [
        {
          title: "名称",
          slot: "name",
          // sortable: true,
        },
        {
          title: "权限",
          slot: "power",
          width: 240,
        },
        {
          title: "修改于",
          slot: "changeTime",
          width: 160,
        },
        {
          title: "大小",
          slot: "size",
          // sortable: true,
          width: 160,
        },
        {
          title: "类别",
          slot: "type",
          width: 200,
        },
      ],
    };
  },
  props: ["data", "loading", "copyInfo"],
  watch: {
    copyInfo: {
      handler() {
        this.setData(this.data);
      },
    },
    data: {
      handler(newVal) {
        // console.log(newVal);
        if (newVal) {
          this.setData(newVal);
        } else {
          this.setData({ children: [{}] });
        }
      },
      // deep: true,
      immediate: true,
    }, 
  },
  created() {
    this.isDownESC = false;

    this.keyEvent = { ctrlKey: "", keyCode: 0 };
  },
  mounted() {
    // 键盘操作

    this.keyFiles = [];
    this.$refs.fileTable.onkeydown = (e) => {
    
        // console.log(e);
        // this.$parent.copyInfo = {};
        e = e || window.event;
        // e.preventDefault(); //关闭浏览器快捷键
        // e.keyCode = e.charCode || e.keyCode;
        this.keyEvent = e;
        // console.log(this.keyEvent);
        // console.log({ ...this.keyState });

        if (this.keyEvent.ctrlKey && this.keyEvent.keyCode == 65) {
          // ctr+A
          this.handleIsSelect(this.data.children, true);
          this.$store.commit("setMouseClickType", "keydown");
          this.$store.commit("setKeyFiles", this.data.children);
          e.preventDefault();
        } else if (this.keyEvent.ctrlKey && this.keyEvent.keyCode == 67) {
          // ctr+C
          console.log("ctr+C");
          if (!this.isReName) {
            this.$parent.clickMenuDropdown({ type: "duplicate" });
          }
        } else if (this.keyEvent.ctrlKey && this.keyEvent.keyCode == 86) {
          // ctr+V
          console.log("ctr+V");
          this.$parent.clickMenuDropdown({ type: "paste" });
        } else if (this.keyEvent.ctrlKey && this.keyEvent.keyCode == 88) {
          // ctr+X
          console.log("ctr+X");
          this.$parent.clickMenuDropdown({ type: "shear" });
        } else if (this.keyEvent.keyCode == 27) {
          // ESC 退出重命名
          console.log("ESC");
          this.isDownESC = true;
          this.tableData.forEach((item) => {
            item.reName = "";
            item.isInput = false;
          });
        } else if (
          this.$store.state.keyFiles.length &&
          this.keyEvent.keyCode == 46
        ) {
          // Delete
          console.log("Delete");
          // 判断正在重命名
          let file = this.$store.state.keyFiles[0];
          if (file.hash == this.data.hash) return;
          if (file && !document.querySelector(`#input_${file.hash}`)) {
            this.$parent.clickMenuDropdown({ type: "remove" });
          }
        } 
    };
    this.$refs.fileTable.onkeyup = (e) => {
      // e.preventDefault(); //关闭浏览器快捷键
      this.keyEvent = { ctrlKey: "", keyCode: 0 };
    };
  },
  methods: {
    setData(data) {
      this.itemObj = {};
      if (data.hash) {
        let tableData = fileImgMapSuffix(data.children || []);
        let keyFiles = this.$store.state.keyFiles;

        tableData.forEach((item) => {
          // 文件剪切时是否虚化
          item.isVirtualization = false;
          if (keyFiles.length > 0) {
            keyFiles.forEach((each) => {
              if (
                this.copyInfo.menuType === "shear" &&
                each.hash == item.hash
              ) {
                item.isVirtualization = true;
              }
            });
          }

          item.formatSize = formatBytes(item.size);
          item.fileId = getRequestId(8);
          item.isSelect = false;
        });

        this.tableData = this.handleSortFiles({
          type: window.sessionStorage.getItem("file-order-type") || "date",
          sort: window.sessionStorage.getItem("file-order-sort") || "asc",
          content: tableData,
        });

      }
      this.$parent.childrenInfo = data;
      this.handleItemClick(data);

    },
    handleSortFiles({ type, sort, content }) {
      let data = cloneDeep(content || this.tableData);
      // console.log(type);

      let files = [];
      let folder = [];

      data.forEach((item) => {
        if (item.mime == "directory") {
          folder.push(item);
        } else {
          files.push(item);
        }
      });

      //  按时间排序
      if (type == "date") {
        files.sort(function (a, b) {
          if (sort == "asc") {
            return a.ts - b.ts;
          } else {
            return b.ts - a.ts;
          }
        });
        folder.sort(function (a, b) {
          if (sort == "asc") {
            return a.ts - b.ts;
          } else {
            return b.ts - a.ts;
          }
        });
        if (sort == "asc") {
          return [...folder, ...files];
        } else {
          return [...files, ...folder];
        }
      } else if (type == "name") {
        let sortFile = (data) => {
          let chineseChars = [],
            chars = [];
          data.forEach((item) => {
            // 判断是否为中文
            if (/^[\u4e00-\u9fa5]*$/.test(item.name.charAt(0))) {
              chineseChars.push(item); // 姓名首字符为中文的
            } else {
              chars.push(item); // 姓名首字符非中文的(字母,数字)
            }
          });
          chars.sort((a, b) => {
            if (sort == "asc") {
              return a.name.charCodeAt(0) - b.name.charCodeAt(0);
            } else {
              return b.name.charCodeAt(0) - a.name.charCodeAt(0);
            }
          });
          chineseChars.sort((a, b) => {
            if (sort == "asc") {
              return a.name.localeCompare(b.name);
            } else {
              return b.name.localeCompare(a.name);
            }
          });
          return chars.concat(chineseChars); // list为最终想要的数组
        };

        // return [...sortFile(folder), ...sortFile(files)];
        if (sort == "asc") {
          return [...sortFile(folder), ...sortFile(files)];
        } else {
          return [...sortFile(files), ...sortFile(folder)];
        }
      } else if (type == "size") {
        files.sort(function (a, b) {
          if (sort == "asc") {
            return a.size - b.size;
          } else {
            return b.size - a.size;
          }
        });
        folder.sort(function (a, b) {
          if (sort == "asc") {
            return a.size - b.size;
          } else {
            return b.size - a.size;
          }
        });
        if (sort == "asc") {
          return [...folder, ...files];
        } else {
          return [...files, ...folder];
        }
      }
      return content;
    },

    handleIsSelect(file = [], isSelect = true) {
      // if (file) {
      let arrHash = file.map((item) => item.hash) || [];

      this.tableData.forEach((item) => {
        item.isSelect = false;
        if (arrHash.indexOf(item.hash) > -1) {
          item.isSelect = isSelect;
        }
      });
    
    },
  
    // 监听键盘事件
    handleKeyUp(e, name) {
      if (this.isDownESC) {
        this.isDownESC = false;
        return;
      }
      if (e.keyCode == 13) {
        this.handleOnBlur(name);
        this.handleRename({});
      }
    },
    handleOnBlur(rename) {
      // console.log(rename);
      this.isReName = false;
      if (!rename) {
        return;
      }
      if (rename.slice(0, 1) == ".") {
        this.$mitt.emit("confirm-modal", {
          type: "info",
          title: "系统提示",
          content: "暂不支持以”.“开头的文件名称!",
        });
        return;
      }
      if (this.isDownESC) {
        this.isDownESC = false;
        return;
      }
   
      if (this.createType == "mkdir" || this.createType == "mkfile") {
        this.$emit("created-file-folder", {
          name: rename,
          type: this.createType,
        });
      } else if (this.createType == "rename" && this.itemObj.name !== rename) {
        this.$emit("rename-file-folder", rename);
      } else {
        this.handleRename({});
      }

    
    },

    createFolderFile(row, type) {
      this.handleRename(row, type);
    },

    handleClickWarp(row) {
  
      if (!this.mouseState.isMove) {
        // this.$store.commit("setIsShowFileMetadata", false);
        this.$parent.childrenInfo = row;
        this.handleItemClick(row);
      }
    },
    handleItemClick(row = {}, mouseClickType) {
      if (mouseClickType) {
        this.$store.commit("setMouseClickType", "click");
      }

      this.$store.commit(
        "setIsShowFileMetadata",
        row && row.mime !== "directory"
      );

      if (
        !(
          this.copyInfo.menuType == "shear" ||
          this.copyInfo.menuType == "duplicate"
        )
      ) {
        if (
          this.keyEvent &&
          this.keyEvent.ctrlKey &&
          this.keyEvent.keyCode == 17
        ) {
          this.keyFiles.push(row); 
        } else {
          this.keyFiles = [row];
          this.$parent.childrenInfo = row;
          if (row.hash !== this.itemObj.hash) {
            this.handleRename();
          }
        }
        this.handleIsSelect(this.keyFiles, true);
        this.$store.commit("setKeyFiles", this.keyFiles);
      }
    },
    handleRename(row = {}, type) {
      this.isReName = true;
      if (row.hash) {
        this.$parent.childrenInfo = row;
        // this.active = row.hash;
        this.handleIsSelect([row], true);
      }
      this.itemObj = row;
      this.createType = type;
      this.tableData.forEach((item) => {
        if (item.hash === row.hash) {
          item.reName = row.name;
          item.isInput = true;
          this.$nextTick(() => {
            let dom = document.querySelector(`#input_${item.hash}`);
            dom.focus();
            let lastIndexOf = row.name.length;
            if (row.mime != "directory") {
              lastIndexOf = row.name.lastIndexOf(".");
            }
            this.setInputSelection(dom, 0, lastIndexOf);

          }, 500);
        } else {
          item.reName = "";
          item.isInput = false;
        }
      });
    },
    handleDblClick(row) {
      if (row.mime === "directory") {
        this.$emit("on-dblclick", { data: row, isDblClick: true });
      }
    },
    handleContextmenu({ event, data, type }) {
      this.$store.commit("setMouseClickType", "contextmenu");
      this.$store.commit("setIsShowFileMetadata", false);
      let keyFiles = this.$store.state.keyFiles;
      if (keyFiles.length <= 1) {
        this.handleItemClick(data);
      }
      this.$emit("on-contextmenu", { event, data, type });
      // this.active = data.hash;
    },
    // 选中input中部分文字
    setInputSelection(input, startPos, endPos) {
      input.focus();
      if (typeof input.selectionStart != "undefined") {
        input.selectionStart = startPos;
        input.selectionEnd = endPos;
      } else if (document.selection && document.selection.createRange) {
        // IE branch
        input.select();
        var range = document.selection.createRange();
        range.collapse(true);
        range.moveEnd("character", endPos);
        range.moveStart("character", startPos);
        range.select();
      }
    },

    /********* 鼠标框选事件*********************/

    //鼠标左键按下方法
    onmousedownClick(event) {
      this.selectIdList = [];
      this.mouseState.isDown = true;

      this.start_x = event.clientX;
      this.start_y = event.clientY;
   
    },
    onmouseMove(event) {
      if (!this.mouseState.isDown) return;
      this.mouseState.isMove = true;

      let end_x = event.clientX;
      let end_y = event.clientY;
      let that = this;
      let divElement = document.getElementById("rectangular");
      divElement.style.display = "block";
      divElement.className = "rectangular";
    
      if (that.start_x < end_x) {
        divElement.style.width = end_x - that.start_x + "px";
        divElement.style.height =
          (that.start_y > end_y > 0
            ? that.start_y - end_y
            : end_y - that.start_y) + "px";
        divElement.style.left = that.start_x + "px";
        divElement.style.right = end_x + "px";
        //从下往上
        if (that.start_y > end_y) {
          divElement.style.top = end_y + "px";
          divElement.style.bottom = that.start_y + "px";
        } else {
          divElement.style.top = that.start_y + "px";
          divElement.style.bottom = end_y + "px";
        }
      } else {
        divElement.style.width = that.start_x - end_x + "px";
        divElement.style.height =
          (that.start_y > end_y > 0
            ? that.start_y - end_y
            : end_y - that.start_y) + "px";
        divElement.style.left = end_x + "px";
        divElement.style.right = that.start_x + "px";
        //从下往上
        if (that.start_y > end_y) {
          divElement.style.top = end_y + "px";
          divElement.style.bottom = that.start_y + "px";
        } else {
          divElement.style.top = that.start_y + "px";
          divElement.style.bottom = end_y + "px";
        }
      }
    },
    //鼠标左键抬起方法
    handMouseUp(event) {
      let { isMove, isDown } = this.mouseState;

      if (isDown && !isMove) {
        this.mouseState.isDown = false;
      }

      // 移动样式清空
      let divElement = document.getElementById("rectangular");
      divElement.style.display = "none";

      if (isMove && isDown) {
        this.mouseState.isDown = false;
        setTimeout(() => {
          this.mouseState.isMove = false;
        }, 300);

        this.end_x = event.clientX;
        this.end_y = event.clientY;

        let files = document.getElementsByName("file");

        let a = 0;
        for (let i = 0; i < files.length; i++) {
          const file = files[i];
          let id = file.id;
          // console.log(id);
          let el = file.getBoundingClientRect();
          let top = el.top; //上边界
          let left = el.left; //左边界
          let right = el.right; //右边界
          let bottom = el.bottom; //下边界
          //原地点击
          if (this.start_x == this.end_x) {
            if (
              !(
                this.end_x >= left &&
                this.end_x <= right &&
                this.end_y > top &&
                this.end_y < bottom
              )
            ) {
              a += 1;
            }
          } else {
            //从左往右
            if (this.start_x < this.end_x) {
              //从下到上
              if (this.start_y > this.end_y) {
                if (top < this.start_y && bottom > this.end_y) {
                  if (left < this.end_x && right > this.start_x) {
                    if (this.selectIdList.indexOf(id) === -1) {
                      this.selectIdList.push(id);
                      // file.className = file.className + " ctrlClick";
                      // file.style = "border: 1px solid rgba(24, 135, 243, 0.5)";
                    }
                  }
                }
              }
              //从上到下
              else {
                if (top < this.end_y && bottom > this.start_y) {
                  if (left < this.end_x && right > this.start_x) {
                    if (this.selectIdList.indexOf(id) === -1) {
                      this.selectIdList.push(id);
                      // file.className = file.className + " ctrlClick";
                      // file.style = "border: 1px solid rgba(24, 135, 243, 0.5)";
                    }
                  }
                }
              }
              //从右往左
            } else {
              //从下到上
              if (this.start_y > this.end_y) {
                if (top < this.start_y && bottom > this.end_y) {
                  if (left < this.start_x && right > this.end_x) {
                    if (this.selectIdList.indexOf(id) === -1) {
                      this.selectIdList.push(id);
                      // file.className = file.className + " ctrlClick";
                      // file.style = "border: 1px solid rgba(24, 135, 243, 0.5)";
                    }
                  }
                }
              }
              //从上到下
              else {
                if (top < this.end_y && bottom > this.start_y) {
                  if (left < this.start_x && right > this.end_x) {
                    if (this.selectIdList.indexOf(id) === -1) {
                      this.selectIdList.push(id);
                      // file.className = file.className + " ctrlClick";
                      // file.style = "border: 1px solid rgba(24, 135, 243, 0.5)";
                    }
                  }
                }
              }
            }
          }
        }

        this.keyFiles = this.tableData.filter(
          (item) => this.selectIdList.indexOf(item.hash) > -1
        );
       
        this.handleIsSelect(this.keyFiles, true);

        this.$store.commit("setKeyFiles", this.keyFiles);
      }
    },
  },
};
</script>
<style lang="scss" scoped>
.file-table {
  height: 100%;
  outline: none;

  #rectangular {
    background-color: rgba(235, 239, 243, 0.45);
    position: fixed;
    border: 1px solid rgba(24, 135, 243, 1);
    z-index: 20;
  }
  ul {
    display: flex;
    // padding: 14px 0 0 14px;
    padding: 20px;
    flex-wrap: wrap;
    li {
      width: 128px;
      // max-height: 150px;
      position: relative;
      vertical-align: top;
      display: inline-block;
      text-align: center;
      word-wrap: break-word;
      overflow: hidden;
      border: 1px solid transparent;
      padding: 15px;
      margin-right: 5px;
      margin-bottom: 5px;
      border-radius: 6px;
      font-size: 12px;
      user-select: none;
      &.cursor {
        cursor: pointer;
      }
      &.mousehover {
        &:hover {
          background-color: #f1f4f9;
          // border: 1px solid rgba(0, 140, 255, 0.3);
        }
      }
      // &:hover,
      &.active {
        background-color: #f1f4f9;
        // border: 1px solid rgba(0, 140, 255, 0.3);
      }
      &.isVirtualization {
        opacity: 0.5;
      }
      .name {
        max-width: 90px;
        line-height: 20px;
        text-align: center;
        max-height: 58px;
        text-overflow: -o-ellipsis-lastline;
        overflow: hidden;
        text-overflow: ellipsis;
        display: -webkit-box;
        -webkit-line-clamp: 3;
        line-clamp: 3;
        -webkit-box-orient: vertical;
      }
    }
  }
  .edit {
    max-width: 90px;
    input {
      width: 100%;
      padding: 2px 3px;
      line-height: 12px;
      border: 1px solid #ccc;
      border-radius: 3px;
      &:focus {
        outline: none;
      }
    }
  }
  .no-file-warp {
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    p {
      font-size: 16px;
      color: #afb6bc;
    }
  }
  .icon-warp {
    display: flex;
    justify-content: center;
    align-items: center;
    overflow: hidden;
    .img-viewer {
      max-width: 85px;
    }
    img {
      display: block;
    }
  }
  .block-box {
    .icon-warp {
      width: 85px;
      height: 62px;
    }
    img {
      width: 40px;
    }
  }

  // table
  .name-warp {
    display: flex;
    .icon-warp {
      margin-right: 5px;
    }
    img {
      width: 16px;
    }
  }
}
</style>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825

# 效果图

# 核心代码

# html代码

<div @mousedown.left="onmousedownClick" @mouseup="handMouseUp"> 
    <ul class="block-box">
      <li
        v-for="item in tableData"
        :key="item.hash + '_' + item.fileId + item.isSelect"
        :id="item.hash"
        :title="item.name"
        name="file"
      >
        <p class="name">{{ item.name }}</p>
      </li>
    </ul>
    <div id="rectangular"></div> 
</div>
1
2
3
4
5
6
7
8
9
10
11
12
13
14

# css代码

.rectangular{
    background-color: rgba(235, 239, 243,0.45);
    position: fixed;
    border: 1px solid rgba(24, 135, 243,1);
    z-index: 20;
}
1
2
3
4
5
6

# js代码

//以下四个方法都是关于多选的
//按住ctrl多选
keyDownCtrl(event, item) {
    //获取点击对象
    let div = event.currentTarget;
    let orginClassName = div.className;

    this.isRightClickCtrl = true;
    if (this.selectIdList.indexOf(item.fileId) == -1) {
        this.selectIdList.push(item.fileId);
        div.className = orginClassName + " " + "ctrlClick"
        div.style = 'border: 1px solid rgba(24, 135, 243, 0.5)'
    }
},
//还原样式
setFileDivClass() {
    let files = document.getElementsByName("file");
    for (let i = 0; i < files.length; i++) {
        const file = files[i];
        file.style="";
        file.className = this.selectDivClassName;
    }
},

//鼠标左键按下方法
onmousedownClick(event) {
    this.isRightClick = true;
    this.start_x = event.clientX;
    this.start_y = event.clientY;
    console.log(" this.start_x="+ this.start_x);
    console.log(" this.start_y="+ this.start_y);
    let that=this;
    
  document.onmousemove=function (event) {
      let end_x=event.clientX;
      let end_y=event.clientY;
      let divElement=document.getElementById("rectangular");
      divElement.style.display='block'
      divElement.className='rectangular';
      //从左往右
     // 画矩形
      if(that.start_x<end_x){
          divElement.style.width=(end_x-that.start_x)+"px";
          divElement.style.height=(that.start_y>end_y>0?(that.start_y-end_y):(end_y-that.start_y))+"px";
          divElement.style.left=that.start_x+"px";
          divElement.style.right=end_x+"px";
          //从下往上
          if(that.start_y>end_y){
              divElement.style.top=end_y+"px";
              divElement.style.bottom=that.start_y+"px";
          }else{
              divElement.style.top=that.start_y+"px";
              divElement.style.bottom=end_y+"px";
          }
      }else{
          divElement.style.width=(that.start_x-end_x)+"px";
          divElement.style.height=(that.start_y>end_y>0?(that.start_y-end_y):(end_y-that.start_y))+"px";
          divElement.style.left=end_x+"px";
          divElement.style.right=that.start_x+"px";
          //从下往上
          if(that.start_y>end_y){
              divElement.style.top=end_y+"px";
              divElement.style.bottom=that.start_y+"px";
          }else{
              divElement.style.top=that.start_y+"px";
              divElement.style.bottom=end_y+"px";
          }
      }
  }
  document.onmouseup=function () {
      //禁用鼠标移动时间
      document.onmousemove=function () {
          return false;
      }
      // 移动样式清空
      let divElement=document.getElementById("rectangular");
      divElement.style.display='none';
  }
},
//鼠标左键抬起方法
handMouseUp: function (event) {

    this.end_x = event.clientX;
    this.end_y = event.clientY;
    console.log(" this.end_x=" + this.end_x);
    console.log(" this.end_y=" + this.end_y);
    //核心内容,根据你的鼠标移动矩形区域来判断div是否在里面
    this.isRightClick = false;
    let files = document.getElementsByName("file");
    let a = 0;
    for (let i = 0; i < files.length; i++) {
        const file = files[i];
        let id = file.id;
        let el = file.getBoundingClientRect();
        let top = el.top //上边界
        let left = el.left;//左边界
        let right = el.right;//右边界
        let bottom = el.bottom;//下边界
        //原地点击
        if (this.start_x == this.end_x) {
            if (!(this.end_x >= left && this.end_x <= right && this.end_y > top && this.end_y < bottom)) {
                a += 1;
            }
        } else {
            //从左往右
            if (this.start_x < this.end_x) {
                //从下到上
                if (this.start_y > this.end_y) {
                    if (top < this.start_y && bottom > this.end_y) {
                        if(left<this.end_x&&right>this.start_x){
                            if (this.selectIdList.indexOf(id) === -1) {
                                this.selectIdList.push(id);
                                file.className = file.className + " ctrlClick";
                                file.style = 'border: 1px solid rgba(24, 135, 243, 0.5)'
                            }
                        }
                    }
                }
                //从上到下
                else{
                    if (top < this.end_y && bottom > this.start_y) {
                        if(left<this.end_x&&right>this.start_x){
                            if (this.selectIdList.indexOf(id) === -1) {
                                this.selectIdList.push(id);
                                file.className = file.className + " ctrlClick";
                                file.style = 'border: 1px solid rgba(24, 135, 243, 0.5)'
                            }
                        }
                    }
                }
            //从右往左
            }else{
                //从下到上
                if (this.start_y > this.end_y) {
                    if (top < this.start_y && bottom > this.end_y) {
                        if(left<this.start_x&&right>this.end_x){
                            if (this.selectIdList.indexOf(id) === -1) {
                                this.selectIdList.push(id);
                                file.className = file.className + " ctrlClick";
                                file.style = 'border: 1px solid rgba(24, 135, 243, 0.5)'
                            }
                        }
                    }
                }
                //从上到下
                else{
                    if (top < this.end_y && bottom > this.start_y) {
                        if(left<this.start_x&&right>this.end_x){
                            if (this.selectIdList.indexOf(id) === -1) {
                                this.selectIdList.push(id);
                                file.className = file.className + " ctrlClick";
                                file.style = 'border: 1px solid rgba(24, 135, 243, 0.5)'
                            }
                        }
                    }
                }

            }
        }
    }
    //如果点击空白处
    if (a == files.length) {
        this.selectIdList = [];
        this.setFileDivClass();
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#Vue
上次更新: 2024/01/30, 00:35:17
使用animate库
使用vue的transition标签结合css样式完成动画

← 使用animate库 使用vue的transition标签结合css样式完成动画→

最近更新
01
Gitea数据备份与还原
03-10
02
Linux 中使用 rsync 同步文件目录教程
03-10
03
Linux 使用 rsync 互相传输同步文件的简单步骤
03-08
更多文章>
Theme by Vdoing | Copyright © 2019-2025 NoteZ,All rights reserved | 冀ICP备2021027292号-1
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式