• vue3 分屏 可拖拽分屏


    1. <script setup>
    2. import { ref } from "vue";
    3. const props = defineProps({
    4. horizontal: Boolean,
    5. ratio: String,
    6. });
    7. const one = ref(null);
    8. const two = ref(null);
    9. // methods
    10. function parseRatio(ratio) {
    11. // ratio: strings like "1/2"
    12. const rn = ratio
    13. ?.split("/")
    14. ?.map(Number)
    15. ?.filter((val) => !isNaN(val));
    16. if (!rn || rn.length !== 2) {
    17. return [1, 1];
    18. }
    19. return rn;
    20. }
    21. const [initGrow1, initGrow2] = parseRatio(props.ratio); // 分屏占比
    22. // states
    23. const grow1 = ref(initGrow1);
    24. const grow2 = ref(initGrow2);
    25. function startResize(mde) {
    26. one.value?.classList.add("forbid-select");
    27. two.value?.classList.add("forbid-select");
    28. const initialPos = props.horizontal ? mde.clientY : mde?.clientX;
    29. const sizeOne = props.horizontal
    30. ? one?.value?.offsetHeight
    31. : one?.value?.offsetWidth;
    32. const sizeTwo = props.horizontal
    33. ? two?.value?.offsetHeight
    34. : two?.value?.offsetWidth;
    35. function handleMouseMove(mme) {
    36. // forbid select
    37. let pos = props.horizontal ? mme.clientY : mme?.clientX;
    38. let newSizeOne = sizeOne + pos - initialPos;
    39. const totalGrow = grow1.value + grow2.value;
    40. grow1.value = totalGrow * (newSizeOne / (sizeOne + sizeTwo));
    41. grow2.value = totalGrow - grow1.value;
    42. }
    43. function handleMouseUp(mue) {
    44. one.value?.classList.remove("forbid-select");
    45. two.value?.classList.remove("forbid-select");
    46. document.removeEventListener("mousemove", handleMouseMove);
    47. document.removeEventListener("mouseup", handleMouseUp);
    48. }
    49. document.addEventListener("mousemove", handleMouseMove);
    50. document.addEventListener("mouseup", handleMouseUp);
    51. }
    52. script>
    53. <style scoped>
    54. .forbid-select {
    55. -moz-user-select: none;
    56. -webkit-user-select: none;
    57. -ms-user-select: none;
    58. user-select: none;
    59. }
    60. .split {
    61. width: 100%;
    62. height: 100%;
    63. display: flex;
    64. }
    65. .split .resizer {
    66. width: 6px;
    67. cursor: w-resize;
    68. transition: 0.3s;
    69. }
    70. .split .resizer:hover {
    71. background-color: #dee5f3;
    72. }
    73. .split .sub {
    74. width: 100%;
    75. height: 100%;
    76. flex-grow: 1;
    77. flex-basis: 0%;
    78. align-items: stretch;
    79. align-content: stretch;
    80. overflow-y: auto;
    81. overflow-x: hidden;
    82. scrollbar-width: thin;
    83. }
    84. .split .sub::-webkit-scrollbar {
    85. width: 8px;
    86. }
    87. .split .sub::-webkit-scrollbar-track {
    88. background-color: #fff;
    89. }
    90. .split .sub::-webkit-scrollbar-thumb {
    91. background-color: #666;
    92. }
    93. .split .horizontal {
    94. flex-direction: column;
    95. }
    96. .split .horizontal .resizer {
    97. height: 5px;
    98. width: 100%;
    99. cursor: n-resize;
    100. }
    101. style>

    示例 二分屏

    1. <splitScreen ratio="1/2">
    2. <template #one>
    3. <div>onediv>
    4. template>
    5. <template #two>
    6. <div>twodiv>
    7. template>
    8. splitScreen>

    三分屏

    1. <splitScreen ratio="1/2">
    2. <template #one>
    3. <div>onediv>
    4. template>
    5. <template #two>
    6. <splitScreen ratio="3/2">
    7. <template #one>
    8. <div>onediv>
    9. template>
    10. <template #two>
    11. <div>twodiv>
    12. template>
    13. splitScreen>
    14. template>
    15. splitScreen>

  • 相关阅读:
    期末前端web大作业——动漫客栈响应式bootstarp(7页) 排版整洁,内容丰富,主题鲜明
    使用matlab制作声音采样率转换、播放以及显示的界面
    独立站saas搭建模式
    VIT(Vision Transformer)学习(三)-纯VIT之swin transformer模型理解
    亏损扩大/毛利偏低,北斗智联与「智能座舱第一阵营」的不等号
    【多线程案例】阻塞队列,实现生产者消费者模型
    基于Django开发的推荐系统与数据分析系统
    Go sync.waitGroup
    前端js实现井字游戏和版本号对比js逻辑【适用于vue和react】
    Linux系统 (三)- 权限介绍
  • 原文地址:https://blog.csdn.net/weixin_46448762/article/details/133883828