首页 > 其他分享 >Vue3 vue-grid-layout布局添加右键事件

Vue3 vue-grid-layout布局添加右键事件

时间:2024-05-15 16:33:37浏览次数:15  
标签:10 vue layout visible grid 右键 background menuConfig

示例

code

<template>
  <div class="dashboard-container">

    <ul class='contextmenu' v-show="menuConfig.visible" :style="{left:menuConfig.left+'px',top:menuConfig.top+'px'}">
      <li @click="test(1)">关闭</li>
      <li @click="test(2)">取消</li>
      <li @click="test(3)">收藏</li>
    </ul>

    <grid-layout
      :layout="layoutData"
      :is-mirrored="false"
      :col-num="12"
      :row-height="30"
      :is-draggable="true"
      :is-resizable="true"
      :vertical-compact="true"
      :use-css-transforms="true"
    >

      <grid-item v-for="item in layoutData"
                 :x="item.x"
                 :y="item.y"
                 :w="item.w"
                 :h="item.h"
                 :i="item.i"
                 :key="item.i">
        <div class="layout-Box" @contextmenu.prevent="openMenu(item, $event)">
          {{ item.i }}
        </div>
      </grid-item>
    </grid-layout>
  </div>
</template>

<script setup>

import {reactive, watch} from "vue";

let layoutData = [
  {"x":0,"y":0,"w":2,"h":2,"i":"0"},
  {"x":2,"y":0,"w":2,"h":4,"i":"1"},
  {"x":4,"y":0,"w":2,"h":5,"i":"2"},
  {"x":6,"y":0,"w":2,"h":3,"i":"3"},
  {"x":8,"y":0,"w":2,"h":3,"i":"4"},
  {"x":10,"y":0,"w":2,"h":3,"i":"5"},
  {"x":0,"y":5,"w":2,"h":5,"i":"6"},
  {"x":2,"y":5,"w":2,"h":5,"i":"7"},
  {"x":4,"y":5,"w":2,"h":5,"i":"8"},
  {"x":6,"y":3,"w":2,"h":4,"i":"9"},
  {"x":8,"y":4,"w":2,"h":4,"i":"10"},
  {"x":10,"y":4,"w":2,"h":4,"i":"11"},
  {"x":0,"y":10,"w":2,"h":5,"i":"12"},
  {"x":2,"y":10,"w":2,"h":5,"i":"13"},
  {"x":4,"y":8,"w":2,"h":4,"i":"14"},
  {"x":6,"y":8,"w":2,"h":4,"i":"15"},
  {"x":8,"y":10,"w":2,"h":5,"i":"16"},
  {"x":10,"y":4,"w":2,"h":2,"i":"17"},
  {"x":0,"y":9,"w":2,"h":3,"i":"18"},
  {"x":2,"y":6,"w":2,"h":2,"i":"19"}
];

const menuConfig = reactive({
  visible: false, left: 0, top: 0
})

const openMenu = (tag, e) => {
  menuConfig.visible = true
  menuConfig.left = e.clientX
  menuConfig.top = e.clientY
}

const closeMenu = () => {
  menuConfig.visible = false
}

const test = (i) => {
  console.log('output-> i::: ', i)
}

watch(() => menuConfig.visible, (newValue, oldValue) => {
  if (menuConfig.visible) {
    document.body.addEventListener('click', closeMenu)
  } else {
    document.body.removeEventListener('click', closeMenu)
  }
}, {deep: true})
</script>


<style lang="scss" scoped>

</style>
<style lang="scss" scoped>


.vue-grid-layout {
  background: #eee;
}

.vue-grid-item:not(.vue-grid-placeholder) {
  background: #ccc;
  border: 1px solid black;
}

.vue-grid-item .resizing {
  opacity: 0.9;
}

.vue-grid-item .static {
  background: #cce;
}

.layout-Box {
  width: 100%;
  height: 100%;
}

.contextmenu {
  margin: 0;
  background: #fff;
  z-index: 100;
  position: absolute;
  list-style-type: none;
  padding: 5px 0;
  border-radius: 4px;
  font-size: 12px;
  font-weight: 400;
  color: #333;
  box-shadow: 2px 2px 3px 0 rgba(0, 0, 0, .3);

  li {
    margin: 0;
    padding: 7px 16px;
    cursor: pointer;

    &:hover {
      background: #eee;
    }
  }
}

</style>

标签:10,vue,layout,visible,grid,右键,background,menuConfig
From: https://www.cnblogs.com/openmind-ink/p/18194125

相关文章

  • vue3 前端大屏项目适配方法
    1scale()方法//屏幕内的内容//样式部分.contain{width:100vw;height:200vh;background:url(.pic);backgrouns-size:cover}.screen{display:inline-block;width:1920px;//设计稿的宽度height:1080px;//设计稿的高度transform-origin:l......
  • vue---移动端登录页面
    最近在用vue做移动端,做了一个好看的移动端登录界面,保存一下,以后方便直接使用。具体代码:<template><divclass="main-login-container"><divclass="login-top"><imgclass="img":src="logintop"></div><divclass=&q......
  • Vue数组变化的侦测的学习
    数组的监听有两种方法变更方法和替换数组####两者的区别就是变更方法会引起ui的自动更新即直接会显示在页面上,但是替换数组不会,变更方法push()pop()shift()unshift()splice()sort()reverse()替换数组concat()filter()slice()如何实现替换数组相当于是变更方法......
  • Vue 事件处理 传参 以及事件的修饰符 的学习
    事件处理在vue中使用v-on来进行监听事件,可以简写成@两种展示方式内联事件处理器(较为简单理解就行)方法事件处理器两者的区别就是内联事件处理器是直接写在v-on里面的,<button@click="count++">自增</button>方法事件处理器是写在js里面的<button@click="addCount">点......
  • 【vue3入门】-【22】 动态组件&组件保持存活&异步组件
    动态组件有些场景下会需要在两个组件之间来回切换,比如tab页面App.vue<template><!--组件标签--><component:is="tabComponent"></component><button@click="changeHandle">切换组件</button></template><script>importC......
  • vue2小效果的实现
    代码量:100行左右搏客量:1所用时间:1h<!--components文件中的Person.vue--><template><divclass="person"><h2>姓名:{{name}}</h2><h2>年龄:{{age}}</h2><button@click="changeName">......
  • Vue3 列表渲染以及key值的状态管理 的学习
    列表渲染使用v-for指令来进行一个数组的渲染,基于iteminitems其中item是迭代名,items是源数据的数组且迭代名称也可以自由定义,只需要与后面插值表达式的一致即可v-for也可以支持第二个参数进行位置的索引(item,index)initems还有一个小的注意点in也可以换成of一样的用......
  • 你是怎么处理vue项目中的错误的?
    一、错误类型任何一个框架,对于错误的处理都是一种必备的能力在Vue 中,则是定义了一套对应的错误处理规则给到使用者,且在源代码级别,对部分必要的过程做了一定的错误处理。主要的错误来源包括:后端接口错误代码中本身逻辑错误二、如何处理后端接口错误通过axios的intercep......
  • vue3 多服务器域名跨域代理和请求配置
    多服务器域名的跨域配置:同样是在vue.config.js文件中对devServer.proxy进行配置,如下:const{defineConfig}=require('@vue/cli-service')module.exports=defineConfig({transpileDependencies:true,lintOnSave:false,//关闭eslint检查,devServer:{......
  • vue给元素添加校验rules
    1.使用validator添加校验规则:在Elemengplus(Vue3版本的ElementPlus)框架中,给el-dialog中的input框添加日期格式验证,可以使用el-form和el-form-item组件来配合实现,并通过el-input组件的v-model绑定数据,结合el-form的验证规则rules实现。以下是一个简单的例子,演示如何给日期输入框......