当前位置:   article > 正文

vue3+element-plus+ts封装可拖拽抽屉组件_vue 可拉动抽屉

vue 可拉动抽屉
  1. <template>
  2. <el-drawer
  3. modal-class="app-modal"
  4. class="app-drawer"
  5. v-bind="$attrs"
  6. :direction="direction"
  7. :model-value="visible"
  8. :close-on-press-escape="false"
  9. :close-on-click-modal="false"
  10. :destroy-on-close="true"
  11. :size="h"
  12. :before-close="handleClose"
  13. >
  14. <template v-for="item in Object.keys($slots)" #[item]>
  15. <slot :name="item"> </slot>
  16. </template>
  17. <template #title>
  18. <div class="drawer-head">
  19. {{ title }}
  20. <div v-if="direction === 'btt'" class="drawer-icon">
  21. <span class="cursor-move" @mousedown="onDown">
  22. <el-icon><Sort /></el-icon>
  23. </span>
  24. </div>
  25. </div>
  26. </template>
  27. </el-drawer>
  28. </template>
  29. <script lang="ts" setup>
  30. import { PropType, ref, onMounted } from "vue";
  31. import { Sort } from "@element-plus/icons-vue";
  32. const props = defineProps({
  33. visible: {
  34. type: Boolean,
  35. default: false,
  36. },
  37. size: {
  38. type: String,
  39. default: "50%",
  40. },
  41. direction: {
  42. type: String as PropType<"ltr" | "rtl" | "ttb" | "btt">,
  43. default: "btt",
  44. },
  45. title: {
  46. type: String,
  47. default: "",
  48. },
  49. });
  50. const darwerEle = ref<any>();
  51. const dragStatus = ref<any>(); // 1开始 2停止
  52. const h = ref<any>();
  53. onMounted(() => {
  54. h.value = props.size;
  55. });
  56. function onDown(e: any) {
  57. darwerEle.value = e;
  58. dragStatus.value = 1;
  59. document.onmousemove = (event: any) => {
  60. if (darwerEle.value && dragStatus.value === 1) {
  61. const height = event.clientY;
  62. const all = document.documentElement.clientHeight;
  63. const limit = all * (1 - parseInt(props.size) / 100);
  64. if (height <= limit) {
  65. h.value = ((all - height) / all) * 100 + "%";
  66. } else {
  67. h.value = props.size;
  68. }
  69. }
  70. };
  71. document.onmouseup = () => {
  72. // 松开鼠标初始化
  73. darwerEle.value = null;
  74. dragStatus.value = 2;
  75. document.onmouseup = null;
  76. document.onmousemove = null;
  77. return false;
  78. };
  79. document.ondragstart = (ev) => {
  80. ev.preventDefault();
  81. };
  82. document.ondragend = (ev) => {
  83. ev.preventDefault();
  84. };
  85. }
  86. const emits = defineEmits(["update:visible", "close"]);
  87. function handleClose() {
  88. emits("update:visible", false);
  89. emits("close");
  90. }
  91. </script>
  92. <style lang="scss">
  93. .app-modal {
  94. background: transparent;
  95. }
  96. .app-drawer {
  97. background: url("@/assets/imgs/layout/bg-component.png") no-repeat center;
  98. background-size: cover;
  99. max-height: 100%;
  100. min-height: 30%;
  101. .el-drawer__header,
  102. .el-drawer__body {
  103. width: 100%;
  104. margin: 0 auto;
  105. @apply max-w-screen-xl;
  106. }
  107. &.el-drawer.ltr,
  108. &.el-drawer.rtl {
  109. top: 80px;
  110. height: calc(100vh - 80px);
  111. }
  112. }
  113. .drawer-head {
  114. position: relative;
  115. }
  116. .drawer-icon {
  117. position: absolute;
  118. top: -18px;
  119. left: 0;
  120. right: 0;
  121. text-align: center;
  122. }
  123. </style>

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

闽ICP备14008679号