uniapp 顶部通知 上滑隐藏

javascript 复制代码
<template>
  <view
    v-if="visible"
    class="top-notice-wrapper"
    :style="{ opacity: opacity, transform: `translateY(${translateY}px)` }"
    @touchstart="handleTouchStart"
    @touchmove="handleTouchMove"
    @touchend="handleTouchEnd"
  >
    <text class="notice-text">{{ message }}</text>
  </view>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'

const message = ref('这是顶部通知消息')
const opacity = ref(0)
const translateY = ref(-50)
const visible = ref(true) 

let startY = 0
const maxHideDistance = 50

onMounted(() => {
  setTimeout(() => {
    opacity.value = 1
    translateY.value = 0
  }, 50)
})

const handleTouchStart = (e: UniApp.TouchEvent) => {
  startY = e.touches[0].clientY
}

const handleTouchMove = (e: UniApp.TouchEvent) => {
  const currentY = e.touches[0].clientY
  let deltaY = currentY - startY

  if (deltaY < 0) {
    deltaY = Math.abs(deltaY)
    if (deltaY > maxHideDistance) deltaY = maxHideDistance

    translateY.value = -deltaY
    opacity.value = 1 - deltaY / maxHideDistance
  }
}

const handleTouchEnd = () => {
  if (translateY.value <= -maxHideDistance * 0.5) {
    opacity.value = 0
    translateY.value = -maxHideDistance
    setTimeout(() => {
      visible.value = false
    }, 300)
  } else {
    opacity.value = 1
    translateY.value = 0
  }
}
</script>

<style lang="scss" scoped>
.top-notice-wrapper {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  background-color: #ffd700;
  padding: 50rpx;
  text-align: center;
  z-index: 1000;
  transition: opacity 0.4s ease, transform 0.3s ease;
  font-size: 28rpx;
}

.notice-text {
  color: #000;
}
</style>
相关推荐
qq_54702617920 小时前
Flowable 工作流引擎
java·服务器·前端
刘逸潇200520 小时前
CSS基础语法
前端·css
吃饺子不吃馅21 小时前
[开源] 从零到一打造在线 PPT 编辑器:React + Zustand + Zundo
前端·svg·图形学
小马哥编程1 天前
【软考架构】案例分析-Web应用设计(应用服务器概念)
前端·架构
鱼与宇1 天前
苍穹外卖-VUE
前端·javascript·vue.js
啃火龙果的兔子1 天前
前端直接渲染Markdown
前端
z-robot1 天前
Nginx 配置代理
前端
用户47949283569151 天前
Safari 中文输入法的诡异 Bug:为什么输入 @ 会变成 @@? ## 开头 做 @ 提及功能的时候,测试同学用 Safari 测出了个奇怪的问题
前端·javascript·浏览器
没有故事、有酒1 天前
Ajax介绍
前端·ajax·okhttp
朝新_1 天前
【SpringMVC】详解用户登录前后端交互流程:AJAX 异步通信与 Session 机制实战
前端·笔记·spring·ajax·交互·javaee