当前位置: 首页 > news >正文

uniapp 微信小程序项目中 地图 map组件 滑动面板

组件代码 components/custom-slider/index.vue

<template>
  <view
    class="panel"
    @touchstart="handleTouchStart"
    @touchmove.stop="throttleHandleTouchMove"
    @touchend="handleTouchEnd"
    :style="{ transform: `translateY(${panelTranslateY}px)` }"
  >
    <view class="panel-handle"></view>
    <view class="panel-content">
      <slot></slot>
    </view>
  </view>
</template>
<script>
function throttle(func, delay) {
  let timer = null
  return function (...args) {
    if (!timer) {
      func.apply(this, args)
      timer = setTimeout(() => {
        timer = null
      }, delay)
    }
  }
}
export default {
  props: {
    baseHeight: {
      type: Number,
      default: 200
    }
  },
  data() {
    const { windowHeight } = wx.getSystemInfoSync()
    return {
      isDragging: false,
      screenHeight: windowHeight,
      panelTranslateY: 0,
      totalHeight: 0,
      initPanelHeight: 0,
      lastMoveTime: 0,
      lastY: 0,
      startY: 0,
      accelerationFactor: 1,
      elasticFactor: 0.3
    }
  },
  created() {
    this.throttleHandleTouchMove = throttle(this.handleTouchMove, 16)
  },
  methods: {
    getClientRect(selector) {
      const query = wx.createSelectorQuery().in(this)
      return new Promise((resolve) => {
        query
          .select(selector)
          .boundingClientRect((rect) => {
            resolve(rect)
          })
          .exec()
      })
    },
    async initPanel() {
      this.resetPanel()
      await this.getPanel()
      this.panelTranslateY = this.initPanelHeight
    },
    resetPanel() {
      this.panelTranslateY = 0
      this.totalHeight = 0
    },
    async getPanel() {
      await this.$nextTick()
      const panelRect = await this.getClientRect('.panel')
      const { height } = panelRect
      this.totalHeight = height
      this.initPanelHeight = height - this.baseHeight
    },
    handleTouchStart(e) {
      this.startY = e.touches[0].clientY
      this.isDragging = true
      this.lastMoveTime = Date.now()
      this.lastY = this.startY
      this.accelerationFactor = 1
    },
    handleTouchMove(e) {
      if (this.isDragging) {
        const currentY = e.touches[0].clientY
        const currentTime = Date.now()
        const deltaY = currentY - this.lastY
        const deltaTime = currentTime - this.lastMoveTime
        const speed = Math.abs(deltaY / deltaTime)
        this.accelerationFactor = 1 + speed * 0.5
        const adjustedDeltaY = deltaY * this.accelerationFactor
        let newTranslateY = this.panelTranslateY + adjustedDeltaY
        if (newTranslateY < 0) {
          newTranslateY = newTranslateY * this.elasticFactor
        } else if (newTranslateY > this.initPanelHeight) {
          newTranslateY =
            this.initPanelHeight +
            (newTranslateY - this.initPanelHeight) * this.elasticFactor
        }
        newTranslateY = Math.min(
          Math.max(newTranslateY, -this.initPanelHeight * this.elasticFactor),
          this.initPanelHeight + this.initPanelHeight * this.elasticFactor
        )
        this.panelTranslateY = newTranslateY
        this.lastY = currentY
        this.lastMoveTime = currentTime
      }
    },
    handleTouchEnd() {
      this.isDragging = false
      if (this.panelTranslateY < 0) {
        this.panelTranslateY = 0
      } else if (this.panelTranslateY > this.initPanelHeight) {
        this.panelTranslateY = this.initPanelHeight
      }
    }
  }
}
</script>

<style scoped lang="scss">
.panel {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  background: #fff;
  padding: 20rpx;
  border-radius: 24px 24px 0 0;
  box-shadow: 0 -4px 12px rgba(0, 0, 0, 0.1);
  z-index: 30;
  transition: transform 0.3s ease;
  will-change: transform;

  .panel-handle {
    width: 60rpx;
    height: 6rpx;
    border-radius: 3rpx;
    background: #f0f0f0;
    margin: 16rpx auto 24rpx;
  }
}
</style>

页面使用

<template>
  <view class="index">
    <map style="width: 100%; height: 100%"></map>
    <CustomSlider ref="slider">
      <view v-for="(item, index) in list" :key="index">
        <view class="item">
          <text>{{ item.name }}</text>
          <text>{{ item.value }}</text>
        </view>
      </view>
    </CustomSlider>
  </view>
</template>

<script>
import CustomSlider from '../../components/custom-slider/index.vue'
export default {
  components: {
    CustomSlider
  },
  data() {
    return {
      list: []
    }
  },
  onLoad() {
    this.getDetail()
  },
  methods: {
    getDetail() {
      setTimeout(() => {
        for (let i = 0; i < 100; i++) {
          this.list.push({
            name: '商品' + i,
            value: '价格' + i
          })
        }
        this.$refs.slider.initPanel()
      }, 2000)
    }
  }
}
</script>

<style>
page {
  height: 100%;
  width: 100%;
}
</style>
<style lang="scss">
.index {
  height: 100%;
  position: relative;
}
.item {
  height: 40px;
  line-height: 40px;
  text-align: center;
  border-bottom: 1px solid #eee;
}
</style>

效果展示
在这里插入图片描述
在这里插入图片描述

相关文章:

  • 2025 年广东水利水电安全员考试:理论与实践融合攻略​
  • 通过redis缓存获取菜品数据
  • Docker学习--容器操作相关命令--docker events 命令
  • 【mapreduce】工作原理
  • livekit ICE连接失败的一些总结
  • 鬼泣:升龙系统
  • Linux-线程概念与线程控制的常用操作
  • 通过动态获取项目的上下文路径来确保请求的 URL 兼容两种启动方式(IDEA 启动和 Tomcat 部署)下都能正确解析
  • 深度解析算法之滑动窗口
  • 清晰易懂的Rust安装与配置教程
  • 【区块链安全 | 第十五篇】类型之值类型(二)
  • 在 RK3588 多线程推理 YOLO 时,同时开启硬件解码和 RGA 加速的性能分析
  • 【JavaEE】UDP数据报套接字编程
  • 傅里叶变换C++实现方式
  • [C++]C++入门篇 2025最新版
  • Leetcode 寻找两个正序数组的中位数
  • CAM获取操作(程序组)的加工时间
  • 短视频团队架构工作流程---2025.3.30 李劭卓
  • python: DDD+ORM+pyQt6 using MySql
  • 用 Python 实现机器学习小项目:从入门到实战
  • 台州做网站的公司/网站内部seo
  • 怀柔网站整站优化公司/网站注册流程
  • 各网站封面尺寸/百度推广账户怎么开
  • 中 网站建设 扬州/seo体系百科
  • 清河网站建设/百度联系电话多少
  • 什么网站没人做/sem是什么测试