Vue3抽屉(Drawer)

news2024/10/6 16:17:13

效果如下图:在线预览

在这里插入图片描述

APIs

参数说明类型默认值必传
width宽度,在 placementrightleft 时使用string | number378false
height高度,在 placementtopbottom 时使用string | number378false
title标题string | slotundefinedfalse
closable是否显示左上角的关闭按钮booleantruefalse
placement抽屉的方向‘top’ | ‘right’ | ‘bottom’ | ‘left’‘right’false
headerStyle设置 Drawer 头部的样式CSSProperties{}false
bodyStyle设置 Drawer 内容部分的样式CSSProperties{}false
extra抽屉右上角的操作区域string | slotundefinedfalse
footer抽屉的页脚string | slotundefinedfalse
footerStyle抽屉页脚的样式CSSProperties{}false
destroyOnClose关闭时是否销毁 Drawer 里的子元素booleanfalsefalse
zIndex设置 Drawerz-indexnumber1000false
open v-model抽屉是否可见booleanfalsefalse

Events

事件名称说明参数
close点击遮罩层或左上角叉或取消按钮的回调(e: Event) => void

创建抽屉组件Drawer.vue

<script setup lang="ts">
import { computed, useSlots, type CSSProperties } from 'vue'
interface Props {
  width?: string | number // 宽度,在 placement 为 right 或 left 时使用
  height?: string | number // 高度,在 placement 为 top 或 bottom 时使用
  title?: string // 标题 string | slot
  closable?: boolean // 是否显示左上角的关闭按钮
  placement?: 'top' | 'right' | 'bottom' | 'left' // 抽屉的方向
  headerStyle?: CSSProperties // 设置 Drawer 头部的样式
  bodyStyle?: CSSProperties // 设置 Drawer 内容部分的样式
  extra?: string // 抽屉右上角的操作区域 string | slot
  footer?: string // 抽屉的页脚 string | slot
  footerStyle?: CSSProperties // 抽屉页脚的样式
  destroyOnClose?: boolean // 关闭时是否销毁 Drawer 里的子元素
  zIndex?: number // 设置 Drawer 的 z-index
  open?: boolean // (v-model) 抽屉是否可见
}
const props = withDefaults(defineProps<Props>(), {
  width: 378,
  height: 378,
  title: undefined,
  closable: true,
  placement: 'right',
  headerStyle: () => ({}),
  bodyStyle: () => ({}),
  extra: undefined,
  footer: undefined,
  footerStyle: () => ({}),
  destroyOnClose: false,
  zIndex: 1000,
  open: false
})
const drawerWidth = computed(() => {
  if (typeof props.width === 'number') {
    return props.width + 'px'
  }
  return props.width
})
const drawerHeight = computed(() => {
  if (typeof props.height === 'number') {
    return props.height + 'px'
  }
  return props.height
})
const slots = useSlots()
const showHeader = computed(() => {
  const titleSlots = slots.title?.()
  const extraSlots = slots.extra?.()
  let n = 0
  if (titleSlots && titleSlots.length) {
    n++
  }
  if (extraSlots && extraSlots.length) {
    n++
  }
  return Boolean(n) || props.title || props.extra || props.closable
})
const showFooter = computed(() => {
  const footerSlots = slots.footer?.()
  return (footerSlots && footerSlots.length) || props.footer
})
const emits = defineEmits(['update:open', 'close'])
function onBlur(e: Event) {
  emits('update:open', false)
  emits('close', e)
}
function onClose(e: Event) {
  emits('update:open', false)
  emits('close', e)
}
</script>
<template>
  <div class="m-drawer" tabindex="-1">
    <Transition name="fade">
      <div v-show="open" class="m-drawer-mask" @click.self="onBlur"></div>
    </Transition>
    <Transition :name="`motion-${placement}`">
      <div
        v-show="open"
        class="m-drawer-wrapper"
        :class="`drawer-${placement}`"
        :style="`z-index: ${zIndex}; ${['top', 'bottom'].includes(placement) ? 'height:' + drawerHeight : 'width:' + drawerWidth};`"
      >
        <div class="m-drawer-content">
          <div class="m-drawer-body-wrapper" v-if="!destroyOnClose">
            <div class="m-drawer-header" :style="headerStyle" v-show="showHeader">
              <div class="m-header-title">
                <svg
                  v-if="closable"
                  focusable="false"
                  @click="onClose"
                  class="u-close"
                  data-icon="close"
                  width="1em"
                  height="1em"
                  fill="currentColor"
                  aria-hidden="true"
                  viewBox="64 64 896 896"
                >
                  <path
                    d="M563.8 512l262.5-312.9c4.4-5.2.7-13.1-6.1-13.1h-79.8c-4.7 0-9.2 2.1-12.3 5.7L511.6 449.8 295.1 191.7c-3-3.6-7.5-5.7-12.3-5.7H203c-6.8 0-10.5 7.9-6.1 13.1L459.4 512 196.9 824.9A7.95 7.95 0 00203 838h79.8c4.7 0 9.2-2.1 12.3-5.7l216.5-258.1 216.5 258.1c3 3.6 7.5 5.7 12.3 5.7h79.8c6.8 0 10.5-7.9 6.1-13.1L563.8 512z"
                  ></path>
                </svg>
                <p class="u-title">
                  <slot name="title">{{ title }}</slot>
                </p>
              </div>
              <div class="m-drawer-extra">
                <slot name="extra">{{ extra }}</slot>
              </div>
            </div>
            <div class="m-drawer-body" :style="bodyStyle">
              <slot></slot>
            </div>
            <div class="m-drawer-footer" :style="footerStyle" v-show="showFooter">
              <slot name="footer">{{ footer }}</slot>
            </div>
          </div>
          <div class="m-drawer-body-wrapper" v-if="destroyOnClose && open">
            <div class="m-drawer-header" :style="headerStyle" v-show="showHeader">
              <div class="m-header-title">
                <svg
                  focusable="false"
                  @click="onClose"
                  class="u-close"
                  data-icon="close"
                  width="1em"
                  height="1em"
                  fill="currentColor"
                  aria-hidden="true"
                  viewBox="64 64 896 896"
                >
                  <path
                    d="M563.8 512l262.5-312.9c4.4-5.2.7-13.1-6.1-13.1h-79.8c-4.7 0-9.2 2.1-12.3 5.7L511.6 449.8 295.1 191.7c-3-3.6-7.5-5.7-12.3-5.7H203c-6.8 0-10.5 7.9-6.1 13.1L459.4 512 196.9 824.9A7.95 7.95 0 00203 838h79.8c4.7 0 9.2-2.1 12.3-5.7l216.5-258.1 216.5 258.1c3 3.6 7.5 5.7 12.3 5.7h79.8c6.8 0 10.5-7.9 6.1-13.1L563.8 512z"
                  ></path>
                </svg>
                <p class="u-title">
                  <slot name="title">{{ title }}</slot>
                </p>
              </div>
              <div class="m-drawer-extra">
                <slot name="extra">{{ extra }}</slot>
              </div>
            </div>
            <div class="m-drawer-body" :style="bodyStyle">
              <slot></slot>
            </div>
            <div class="m-drawer-footer" :style="footerStyle" v-show="showFooter">
              <slot name="footer">{{ footer }}</slot>
            </div>
          </div>
        </div>
      </div>
    </Transition>
  </div>
</template>
<style lang="less" scoped>
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s;
}
.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}
.motion-top-enter-active,
.motion-top-leave-active {
  transition: all 0.3s;
}
.motion-top-enter-from,
.motion-top-leave-to {
  transform: translateY(-100%);
}
.motion-right-enter-active,
.motion-right-leave-active {
  transition: all 0.3s;
}
.motion-right-enter-from,
.motion-right-leave-to {
  transform: translateX(100%);
}
.motion-bottom-enter-active,
.motion-bottom-leave-active {
  transition: all 0.3s;
}
.motion-bottom-enter-from,
.motion-bottom-leave-to {
  transform: translateY(100%);
}
.motion-left-enter-active,
.motion-left-leave-active {
  transition: all 0.3s;
}
.motion-left-enter-from,
.motion-left-leave-to {
  transform: translateX(-100%);
}
.m-drawer {
  position: fixed;
  inset: 0;
  z-index: 1000;
  pointer-events: none;
  .m-drawer-mask {
    position: absolute;
    inset: 0;
    z-index: 1000;
    background: rgba(0, 0, 0, 0.45);
    pointer-events: auto;
  }
  .m-drawer-wrapper {
    position: absolute;
    transition: all 0.3s;
    .m-drawer-content {
      width: 100%;
      height: 100%;
      overflow: auto;
      background: #ffffff;
      pointer-events: auto;
      .m-drawer-body-wrapper {
        display: flex;
        flex-direction: column;
        width: 100%;
        height: 100%;
        .m-drawer-header {
          display: flex;
          flex: 0;
          align-items: center;
          padding: 16px 24px;
          font-size: 16px;
          line-height: 1.5;
          border-bottom: 1px solid rgba(5, 5, 5, 0.06);
          .m-header-title {
            display: flex;
            flex: 1;
            align-items: center;
            min-width: 0;
            min-height: 0;
            .u-close {
              display: inline-block;
              margin-inline-end: 12px;
              width: 16px;
              height: 16px;
              fill: rgba(0, 0, 0, 0.45);
              cursor: pointer;
              transition: fill 0.2s;
              &:hover {
                fill: rgba(0, 0, 0, 0.88);
              }
            }
            .u-title {
              flex: 1;
              margin: 0;
              color: rgba(0, 0, 0, 0.88);
              font-weight: 600;
              font-size: 16px;
              line-height: 1.5;
            }
          }
          .m-drawer-extra {
            flex: none;
            color: rgba(0, 0, 0, 0.88);
          }
        }
        .m-drawer-body {
          flex: 1;
          min-width: 0;
          min-height: 0;
          padding: 24px;
          overflow: auto;
        }
        .m-drawer-footer {
          flex-shrink: 0;
          padding: 8px 16px;
          border-top: 1px solid rgba(5, 5, 5, 0.06);
          color: rgba(0, 0, 0, 0.88);
        }
      }
    }
  }
  .drawer-top {
    top: 0;
    inset-inline: 0;
    box-shadow:
      0 6px 16px 0 rgba(0, 0, 0, 0.08),
      0 3px 6px -4px rgba(0, 0, 0, 0.12),
      0 9px 28px 8px rgba(0, 0, 0, 0.05);
  }
  .drawer-right {
    top: 0;
    right: 0;
    bottom: 0;
    box-shadow:
      -6px 0 16px 0 rgba(0, 0, 0, 0.08),
      -3px 0 6px -4px rgba(0, 0, 0, 0.12),
      -9px 0 28px 8px rgba(0, 0, 0, 0.05);
  }
  .drawer-bottom {
    bottom: 0;
    inset-inline: 0;
    box-shadow:
      0 -6px 16px 0 rgba(0, 0, 0, 0.08),
      0 -3px 6px -4px rgba(0, 0, 0, 0.12),
      0 -9px 28px 8px rgba(0, 0, 0, 0.05);
  }
  .drawer-left {
    top: 0;
    bottom: 0;
    left: 0;
    box-shadow:
      6px 0 16px 0 rgba(0, 0, 0, 0.08),
      3px 0 6px -4px rgba(0, 0, 0, 0.12),
      9px 0 28px 8px rgba(0, 0, 0, 0.05);
  }
}
</style>

在要使用的页面引入

<script setup lang="ts">
import Drawer from './Drawer.vue'
import { ref } from 'vue'

const open1 = ref<boolean>(false)
const open2 = ref<boolean>(false)
const open3 = ref<boolean>(false)
const open4 = ref<boolean>(false)
const open5 = ref<boolean>(false)
const options = ref([
  {
    label: 'top',
    value: 'top'
  },
  {
    label: 'right',
    value: 'right'
  },
  {
    label: 'bottom',
    value: 'bottom'
  },
  {
    label: 'left',
    value: 'left'
  }
])
const placement = ref('right')
const extraPlacement = ref('right')
const footerPlacement = ref('right')
function onClose() {
  // 点击遮罩层或左上角叉或取消按钮的回调
  open3.value = false
  open4.value = false
  console.log('close')
}
</script>
<template>
  <div>
    <h1>{{ $route.name }} {{ $route.meta.title }}</h1>
    <h2 class="mt30 mb10">基本使用</h2>
    <Button type="primary" @click="open1 = true">Open</Button>
    <Drawer v-model:open="open1" title="Basic Drawer" @close="onClose">
      <p>Some contents...</p>
      <p>Some contents...</p>
      <p>Some contents...</p>
    </Drawer>
    <h2 class="mt30 mb10">自定义位置</h2>
    <Radio v-model:value="placement" :options="options" style="margin-right: 8px" />
    <Button type="primary" @click="open2 = true">Open</Button>
    <Drawer
      v-model:open="open2"
      title="Basic Drawer"
      :closable="false"
      extra="extra"
      footer="footer"
      :placement="placement"
    >
      <p>Some contents...</p>
      <p>Some contents...</p>
      <p>Some contents...</p>
    </Drawer>
    <h2 class="mt30 mb10">额外操作</h2>
    <Radio v-model:value="extraPlacement" :options="options" style="margin-right: 8px" />
    <Button type="primary" @click="open3 = true">Open</Button>
    <Drawer v-model:open="open3" title="Basic Drawer" :placement="extraPlacement">
      <template #extra>
        <Button style="margin-right: 8px" @click="onClose">Cancel</Button>
        <Button type="primary" @click="onClose">Submit</Button>
      </template>
      <p>Some contents...</p>
      <p>Some contents...</p>
      <p>Some contents...</p>
    </Drawer>
    <h2 class="mt30 mb10">抽屉页脚</h2>
    <Radio v-model:value="footerPlacement" :options="options" style="margin-right: 8px" />
    <Button type="primary" @click="open4 = true">Open</Button>
    <Drawer
      v-model:open="open4"
      title="Basic Drawer"
      :placement="footerPlacement"
      :footer-style="{ textAlign: 'right' }"
    >
      <p>Some contents...</p>
      <p>Some contents...</p>
      <p>Some contents...</p>
      <template #footer>
        <Button style="margin-right: 8px" @click="onClose">Cancel</Button>
        <Button type="primary" @click="onClose">Submit</Button>
      </template>
    </Drawer>
    <h2 class="mt30 mb10">自定义 header & body 样式</h2>
    <Button type="primary" @click="open5 = true">Open</Button>
    <Drawer
      v-model:open="open5"
      :closable="false"
      title="Basic Drawer"
      :header-style="{ textAlign: 'center' }"
      :body-style="{ textAlign: 'center' }"
    >
      <p>Some contents...</p>
      <p>Some contents...</p>
      <p>Some contents...</p>
    </Drawer>
  </div>
</template>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1865484.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Docker(八)-Docker运行mysql8容器实例

1.运行mysql8容器实例并挂载数据卷 -e:配置环境变量 --lower_case_table_names1 设置忽略表名大小写一定要放在镜像之后运行mysql8容器实例之前&#xff0c;先查看是否存在mysql8镜像以及是否存在已运行的mysql实例docker run -d -p 3306:3306 --privilegedtrue -v 【宿主机日…

Jenkins流水线发布,一篇就解决你的所有疑惑

这次搭建的项目比较常规,前端是react写的,后端是springboot,并且由于是全栈开发,所以是在同一个项目中。接下来我演示下怎么用jenkins进行自动化发布。 1.jenkins必装插件 这里用到的是jenkinsFile主要是基于Groovy这个沙盒,有些前置插件。这里使用maven进行打包,所以需…

公益培训|半导体与集成电路项目制培训项目

关于我们 硬蛋产业学院&#xff0c;基于硬蛋创新(http://00400.HK)在芯片产业的资源和技术优势&#xff0c;引进全球领先的芯片应用技术&#xff0c;为国内培养芯片应用技术人才&#xff0c;助力芯片应用产业发展。 硬蛋产业学院在国家各主管部门、广东省、深圳市及社会各界的大…

一码多址与同义词解决方案

随着地址库中的数据不断的丰富&#xff0c;地址库中一码多址和同义词的数据也会越来越多&#xff0c;一码多址和同义词在统一地址管理平台中的概念并不相同。 一码多址指的是多个地址编码相同&#xff0c;例如通过民政地址找到编码&#xff0c;再通过编码找到房产地址描述。 本…

使用Python和jieba库生成中文词云

使用Python和jieba库生成中文词云 在文本分析和数据可视化的领域中&#xff0c;词云是一种展示文本数据中关键词频率的直观方式。Python作为一种强大的编程语言&#xff0c;提供了多种库来帮助我们生成词云&#xff0c;如wordcloud和jieba。在本文中&#xff0c;我们将通过一个…

谷歌SEO是什么意思?

谷歌SEO&#xff08;Search Engine Optimization&#xff09;是通过优化网站内容和结构&#xff0c;使其在谷歌搜索引擎中排名更高的策略和技术。这不仅仅是提高网站排名&#xff0c;更是吸引目标受众、增加流量并最终提升业务转化的关键方法之一。谷歌搜索引擎优化&#xff08…

在win7系统电脑安装node16的版本(已成功安装运行)

很多银行的项目行方都要求内网开发&#xff0c;但是我遇到的几个银行基本都是win7系统的电脑&#xff0c;而前端的项目又是需要高版本的node才能跑起来&#xff0c;所有就记录此解决方案文章&#xff01; 这是下载node安装包的地址&#xff1a;Index of /dist/ 在这里先下载自…

C# unknow column “p0.TaskTypeId‘ in ‘field list‘

这个问题就是数据库出现问题&#xff0c;去 日志中去看 &#xff0c;找个具体表去 看实体类&#xff0c;与数据库中的表&#xff0c;是否存在字段。

【Qt】初识QtQt Creator

一.简述Qt 1.什么是Qt Qt 是⼀个 跨平台的 C 图形⽤⼾界⾯应⽤程序框架 。它为应⽤程序开发者提供了建⽴艺术级图形界⾯所需的所有功能。它是完全⾯向对象的&#xff0c;很容易扩展。Qt 为开发者提供了⼀种基于组件的开发模式&#xff0c;开发者可以通过简单的拖拽和组合来实现…

【Java Web】Tomcat服务器

目录 一、Tomcat是什么 二、Tomcat安装 三、Tomcat相关目录 四、Web项目标准目录结构规范 五、Tomcat项目部署方式 六、IDEA关联本地Tomcat 七、HTTP协议 7.1 http的交互方式 7.1.1 http长连接和短连接 7.1.2 http1.1缓存机制 7.2 http数据报文格式 八、常见响应状态码 一、Tom…

大数据平台需要存算分离吗?某保险集团:以 ZBS 优化资源利用率,缩短业务用时超一半

金融机构普遍采用“存算一体”架构支撑基于 Hadoop 框架的大数据平台。而随着金融业务的多元化发展&#xff0c;不同业务对计算和存储的需求差异较大&#xff0c;由于“存算一体”架构共享存储与计算资源&#xff0c;经常会出现资源需求不均衡、资源利用率低下、难以灵活调度等…

贪吃蛇项目:GameRun与GameEnd部分:游戏的主体运行与善后部分

准备工作&#xff1a;打印得分信息 在进行GameStart之前&#xff0c;我们需要在地图的右侧打印帮助信息&#xff0c;以及目前玩家的得分情况和一个食物在当前速度下的得分情况&#xff08;加速的状态下按比例增加食物的分数&#xff0c;减速的状态下则相反&#xff09;&#xf…

历史与未来的交响曲:历史建筑保护与现代技术的完美融合

在时间的长河中&#xff0c;历史建筑如同凝固的诗篇&#xff0c;记录着过往的辉煌与沧桑。然而&#xff0c;岁月的侵蚀、自然灾害的威胁以及现代化进程的冲击&#xff0c;使这些宝贵的文化遗产面临前所未有的挑战。作为建筑设计领域的探索者&#xff0c;我们肩负着保护历史记忆…

多空分歧中,盘点全球“新股王”背后的这些在港概念股

冲榜成功。6月18日收盘&#xff0c;英伟达(NVDA.US)股价创新高&#xff0c;报135.58美元&#xff0c;总市值3.34万亿美元&#xff0c;一举超越微软、苹果公司&#xff0c;成为全球“新股王”。 不过&#xff0c;随着这一历史性时刻的出现&#xff0c;由于当前无法准确预估市场…

vue配置中的process.env

项目中的.env开头的文件是否知道是干什么的呢 主要是为了区分测试环境还是生产环境env.development为测试环境 # 测试环境 NODE_ENV development VUE_APP_BASE_API http://xxxxxxxxx // 命名一定要以 VUE_APP_ 开头&#xff0c;要不然根本取不到 .env.production为生产环境…

浏览器扩展V3开发系列之 chrome.runtime 的用法和案例

【作者主页】&#xff1a;小鱼神1024 【擅长领域】&#xff1a;JS逆向、小程序逆向、AST还原、验证码突防、Python开发、浏览器插件开发、React前端开发、NestJS后端开发等等 chrome.runtime API 提供了一系列的方法和事件&#xff0c;可以通过它来管理和维护 Chrome 扩展的生命…

用两个钟,我又在VMWARE上搞了一套内部网配置

最近要学es&#xff0c;所以打算自己用虚拟机搞个NAT&#xff0c;又搞了两个钟。为了不再费劲尝试&#xff0c;也为了造福大众&#xff0c;所以选择搞一份NAT笔记&#xff01;&#xff01;&#xff01;&#xff01; 1.初始化网关和DNS 我们给网关配置一个地址192.168.96.1&…

计算机图形学入门20:加速光线追踪

1.前言 前文说了Whitted-style光线追踪技术的原理以及光线与平面的交点计算方式&#xff0c;对于现在应用最广的Polygon Mesh显式曲面来说&#xff0c;一个复杂场景中的多边形面总数可能达到千万甚至亿万以上&#xff0c;如果每个像素发射光线都和场景中每个平面进行求交点计算…

植物大战僵尸杂交版v2.1最新直装版,苹果+安卓+PC+防闪退工具+修改工具+高清工具+通关存档整合包更新

今天我要和各位聊聊一款让全网疯狂的游戏——《植物大战僵尸杂交版》。这可不是简单的游戏&#xff0c;它可是让B站的UP主“潜艇伟伟迷”一夜成名的大作&#xff0c;让无数玩家为之疯狂的魔改神作&#xff01; 记得2009年&#xff0c;《植物大战僵尸》横空出世&#xff0c;那时…

SpringBoot优点达项目实战:登录功能实现(四)

SpringBoot优点达项目实战&#xff1a;登录功能实现&#xff08;四&#xff09; 文章目录 SpringBoot优点达项目实战&#xff1a;登录功能实现&#xff08;四&#xff09;1、查看接口2、查看数据库3、代码实现1、创建实体类2、controller实现3、service层实现4、Mapper层 4、测…