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>

效果展示

相关推荐
moxiaoran57532 小时前
uni-app萌宠案例学习笔记--页面布局和CSS样式设置
前端·css·uni-app
moxiaoran57533 小时前
uni-app学习笔记二十九--数据缓存
笔记·学习·uni-app
moxiaoran57538 小时前
uni-app学习笔记三十--request网络请求传参
笔记·学习·uni-app
疯狂的沙粒10 小时前
在uni-app中如何从Options API迁移到Composition API?
javascript·vue.js·uni-app
chaosama10 小时前
微信小程序带参分享、链接功能
微信小程序·小程序
胡西风_foxww11 小时前
微信小程序动态组件加载的应用场景与实现方式
微信小程序·应用·加载·动态组件
anyup13 小时前
快崩溃了!华为应用商店已经 4 次驳回我的应用上线
前端·华为·uni-app
甜甜的资料库15 小时前
基于小程序老人监护管理系统源码数据库文档
微信小程序
alphaair21 小时前
【一步步开发AI运动APP】十、微调优化内置运动分析器,灵活适配不同的应用场景
uni-app·ai运动·ai运动识别·ai健身·ai体测·ai运动app·ai运动检测·工会云上运动会·ai人体检测·ai姿态识别
moxiaoran57531 天前
uni-app学习笔记二十三--交互反馈showToast用法
笔记·学习·uni-app