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

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>
相关推荐
凯程序猿1号40 分钟前
Web墨卡托EPSG3857瓦片行列号XYZ与经纬度双向换算及地面分辨率推导
前端·电脑
qq_274475151 小时前
第 21 篇:Proxy 代理与 Reflect:数据劫持基础,Vue 响应式底层原理前置
前端·javascript·vue.js
寺中人1 小时前
从零做一个零依赖离线工具箱:34 个本地工具 + WebView2 打包单文件 EXE 实战
javascript·测试工具·webview2·winforms
qq_274475151 小时前
第 16 篇:类型检测方案汇总:typeof、instanceof、Object.prototype.toString、手写 isType
javascript·学习·原型模式
JudithHuang1 小时前
Angular 响应式表单:TS 建模型与校验规则
前端·javascript·angular·angular.js
拖孩2 小时前
群里有人开始比「次数」之后,我做了个排行榜小程序
前端·后端·微信小程序
陈随易2 小时前
CDN 自带安全防护,SQL 注入、XSS、命令攻击现原形
前端·后端
lgbisha3 小时前
银河麒麟入门:日志刷满终端,用 less 定位关键词并看前后几行
java·前端·less·银河麒麟
w-w0w-w4 小时前
CSRF 与 JWT 漏洞原理、攻击手法及防御机制
前端·网络·csrf
默 语4 小时前
HarmonyOS7 组件通用事件(一):四大事件总览——组件的生死、身位与存在感
服务器·前端·javascript