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

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

javascript 复制代码
<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>

页面使用

javascript 复制代码
<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>

效果展示

相关推荐
^Rocky8 小时前
uniapp 实现腾讯云IM群文件上传下载功能
uni-app·腾讯云
moxiaoran57539 小时前
uni-app学习笔记三十四--刷新和回到顶部的实现
笔记·学习·uni-app
y东施效颦9 小时前
uni-app页面发布测试环境出现连接服务器超时,点击屏幕重试解决方案
前端·javascript·vue.js·uni-app·vue
甜甜的资料库10 小时前
基于微信小程序的睡眠宝系统源码数据库文档
数据库·微信小程序·小程序
华子w90892585910 小时前
SpringBoot+uniapp 的 Champion 俱乐部微信小程序设计与实现,论文初版实现
spring boot·微信小程序·uni-app
恰薯条的屑海鸥12 小时前
关于我对各开发语言的看法与接下来的文章内容
开发语言·学习·微信小程序·网站开发·全栈开发
性野喜悲12 小时前
uniapp+<script setup lang=“ts“>解决有数据与暂无数据切换显示,有数据加载时暂无数据闪现(先加载空数据)问题
uni-app
Stanford_110613 小时前
关于大数据的基础知识(二)——国内大数据产业链分布结构
大数据·开发语言·物联网·微信小程序·微信公众平台·twitter·微信开放平台
假客套15 小时前
2025 后端自学UNIAPP【项目实战:旅游项目】6、我的收藏页面
微信·uni-app·旅游
邹荣乐16 小时前
uni-app开发微信小程序的报错[渲染层错误]排查及解决
前端·微信小程序·uni-app