小程序的右侧抽屉开关动画手写效果

javascript 复制代码
<template>
  <view>
    <button @click="openDrawer">打开抽屉</button>
    
    <view v-if="showDrawer" class="drawer" :style="{ backgroundColor: bgColor }" @click="closeDrawer">
      <view class="drawerContent" :animation="animationData" @click.stop>
        <view class="drawerName">
          <view style="font-weight: bold;" class="drawerName_title">价格区间(元)</view>
          <view style="position: relative;">
            <text @click="closeDrawer" class="drawerName_icon qh-rt-single qh-rt-a-zu4416 choose-sku-close-icon"></text>
          </view>
        </view>
        <!-- ... 其余内容保持不变 ... -->
      </view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      showDrawer: false,
      animationData: {},
      bgColor: 'rgba(0, 0, 0, 0)',
      animation: null,
      // ... 其他数据
    }
  },
  methods: {
    openDrawer() {
      this.showDrawer = true;
      this.bgColor = 'rgba(0, 0, 0, 0)';
      this.$nextTick(() => {
        this.animation = uni.createAnimation({
          duration: 300,
          timingFunction: 'ease-out',
        });
        this.animation.translateX(0).step();
        this.animationData = this.animation.export();
        
        // 背景颜色渐变
        let opacity = 0;
        const bgInterval = setInterval(() => {
          opacity += 0.1;
          this.bgColor = `rgba(0, 0, 0, ${opacity})`;
          if (opacity >= 0.5) {
            clearInterval(bgInterval);
          }
        }, 30);
      });
    },
    closeDrawer() {
      this.animation.translateX('100%').step();
      this.animationData = this.animation.export();
      
      // 背景颜色渐变
      let opacity = 0.5;
      const bgInterval = setInterval(() => {
        opacity -= 0.1;
        this.bgColor = `rgba(0, 0, 0, ${opacity})`;
        if (opacity <= 0) {
          clearInterval(bgInterval);
          this.showDrawer = false;
        }
      }, 30);
    }
  }
}
</script>

<style lang="scss">
.drawer {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: flex-end;
  z-index: 999;
}

.drawerContent {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  height: 100%;
  padding-top: 140rpx;
  background: #fff;
  width: 512rpx;
  transform: translateX(100%); // 初始位置在屏幕右侧
}

// ... 其他样式保持不变 ...
</style>
相关推荐
2501_9445264213 小时前
Flutter for OpenHarmony 万能游戏库App实战 - 笑话生成器实现
android·javascript·python·flutter·游戏
请叫我聪明鸭13 小时前
基于 marked.js 的扩展机制,创建一个自定义的块级容器扩展,让内容渲染为<div>标签而非默认的<p>标签
开发语言·前端·javascript·vue.js·ecmascript·marked·marked.js插件
悟能不能悟13 小时前
Gson bean getxxx,怎么才能返回给前端
java·前端
2501_9447114313 小时前
前端向架构突围系列 - 工程化(五):企业级脚手架的设计与落地
前端·架构
Apex Predator13 小时前
本地库导入到nexus
java·服务器·前端
趁着年轻吃点苦13 小时前
宝塔面板部署指南
前端
2501_9445264213 小时前
Flutter for OpenHarmony 万能游戏库App实战 - 21点游戏实现
android·javascript·flutter·游戏·harmonyos
0思必得013 小时前
[Web自动化] Selenium中Select元素操作方法
前端·python·selenium·自动化·html
Duang007_13 小时前
【万字学习总结】API设计与接口开发实战指南
开发语言·javascript·人工智能·python·学习
一叶星殇13 小时前
C# .NET 如何解决跨域(CORS)
开发语言·前端·c#·.net