拖拽(高度自定义)

在这里插入图片描述

思路:
监听鼠标按下事件,监听在移动时计算它的高度和上面div盒子的高度

demo

<template>
  <div style="width: 100%;">
    <div
      ref="DrawerBody"
      class="drawer-body"
    >
      <div>11</div>
      <div>11</div>
      <div>11</div>
      <div>11</div>
      <div>11</div>
      <div>11</div>
      <div>11</div>
      <div>11</div>
      <div>11</div>
      <div>11</div>
      <div>11</div>
      <div>11</div>
      <div>11</div>
    </div>
    <div
      ref="DrawerBottom"
      class="drawer-bottom"
    >
      <div
        ref="DrawerBottomBar"
        class="drawer-bottom-bar"
      >
        <div class="drawer-bottom-bar-true" />
      </div>
      <div class="drawer-bottom-body">
        1111
      </div>
    </div>
  </div>
</template>
<script lang="ts" setup>

import { onMounted, ref } from 'vue'

const DrawerBottomBar = ref<any>(null)
const DrawerBottom = ref<any>(null)
const DrawerBody = ref<any>(null)
onMounted(() => {
  DrawerBottomBar.value.onmousedown = (e: any) => {
    const oldHeight = DrawerBottom.value.offsetHeight
    const startY = e.clientY
    document.onmousemove = (e) => {
      console.log(e)
      // 当前鼠标Y值
      const endY = e.clientY
      // 当前鼠标在Y轴移动的距离
      const distance = endY - startY
      // left-top当前高度
      const newHeight = oldHeight - distance
      // 设置dom样式
      DrawerBottom.value.style.height = `${newHeight}px`

      DrawerBody.value.style['padding-bottom'] = `${newHeight}px`
    }
    document.onmouseup = () => {
      document.onmousemove = null
      document.onmouseup = null
    }
    return false
  }
})
</script>
  <style scoped lang="scss">
  .drawer {
   position: relative;
   &-body {
    background: #fafafa;
    padding: 0 24px;
    width: 100%;
    font-family: "Inter";
    height: calc(100vh - 86px);
    overflow: auto;
    &::-webkit-scrollbar {
     display: none; /* Chrome Safari */
    }
   }
   &-bottom {
    width: 100%;
    position: absolute;
    z-index: 10;
    bottom: 0;
    background: #fff;
    height: 360px;
    border-top: 1px solid #e5e6eb;
    box-shadow: 0px -4px 12px rgba(0, 0, 0, 0.12);
    &-bar {
     &-true {
      height: 4px;
      border-radius: 4px;
      background: rgba(0, 0, 0, 0.32);
     }
     cursor: ns-resize;
     position: absolute;
     left: 50%;
     margin-left: -20%;
     top: -20px;
     width: 40%;
     padding: 10px 0;
    }
   }
  }
  </style>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值