Taro微信小程序底部动画弹窗

本文探讨了在前端开发中实现弹窗动画遇到的问题及解决方案。当使用`maskVisible`控制内容挂载时,弹回动画无法显示。作者提出了两种策略:一是通过改变内容的显示模式,二是利用动画时间来延迟卸载内容。这两种方法旨在确保在内容隐藏时仍能执行动画,从而实现完整的弹窗动画效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

// 蒙层部分
@keyframes slideBgUp {
  from {
    visibility: visible;
    background: transparent;
  }
  to {
    background: rgba(0,0,0,.7);
  }
}

@keyframes slideBgDown {
  from {
    background: rgba(0, 0 ,0 , .7);
  }

  to {
    background: transparent;
    visibility: hidden;
  }
}

.mask, .mask_down {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%; height: 100%;
  background-color: rgba(0, 0, 0, 0.7);
  z-index: 1000;
  animation: slideBgUp 0.3s ease-in both;
}

.mask_down {
  animation: slideBgDown 0.3s ease-in both;
}
// 内容部分
// 浏览器坐标系是y轴向下为正,x轴向右为正,z轴是屏幕到用户的眼睛的方向为正
@keyframes slideContentUp {
  from {
    // max-height: 0; 实现方法1,把内容最大高度设为0
    transform: translate3d(0, 100%, 0);  // 实现方法2,把内容整体向下移动
    // 此时其他的css中已经把内容容器定位到了底部
  }
  to {
    // max-height: 1000px;  设置足够的最大高度
    transform: translate3d(0, 0, 0);  // 把内容整体回到原来的状态
  }
}

@keyframes slideContentDown {
  from {
    transform: translate3d(0, 0, 0);
    // max-height: 1000px;
  }
  to {
    // max-height: 0;
    transform: translate3d(0, 100%, 0);
  }
}

.pop{
  animation: slideContentUp 0.3s ease-in both;
  position: absolute;
  bottom: 0;
  border-radius: 24px 24px 0 0;
  overflow: hidden;
}

.pop_down {
  animation: slideContentDown 0.3s ease-in both;
  position: absolute;
  bottom: 0;
  border-radius: 24px 24px 0 0;
  overflow: hidden;
}
示例1
{
	maskVisible ?
  (
	<View className={maskVisible ? styles.mask : styles.mask_down} 
	 onClick={this.changeVisible}>
     <View className={maskVisible ? styles.pop : styles.pop_down }>
       <ComonentContent onclick={this.changeVisible}/>
     </View>
    </View>
  ) : null
}
// 思考
// 上面的形式弹出时没有问题,但是弹回时就不会出现动画
// 因为maskVisible 控制着内容的挂载,为false时内容被直接卸载
// 所以不会出现弹回的动画
// 总述上面的是不完整的弹窗动画实现
// 知道了原因,笔者暂时想到有两种方法实现
// 第一种改变内容的显示模式
// 做出修改的关键部分
{
  <View className={maskVisible ? styles.mask : styles.mask_down} 
   // style={animaFlag ? {visibility: "hidden"} : ''}
   // 为了避免第一次加载时会出现动画效果
  style={animaFlag ? {visibility: "hidden"} : ''}
  onClick={this.changeVisible}>
    <View className={maskVisible ? styles.pop : styles.pop_down }>
      <ComonentContent onclick={this.changeVisible}/>
    </View>
  </View>
}

// 第二种
// 在示例1中通过动画时间判断是否把maskVisible 设置为false
// 比如弹回的动画时间为0.3s
// 可以通过setTimeout 的回调中把maskVisible 设置为false
// 由于比较复杂大家可以去试一下
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值