【快应用】如何避免过渡动画设置不生效

 【关键词】

过渡动画、transition

【问题背景】

快应用组件位置变更时,使用过渡动画时,组件位置直接切换了,设置的过渡动画效果没有生效,该如何处理?

代码如下:

<template>
  <!-- Only one root node is allowed in template. -->
  <div class="container">
    <text class="title" onclick="startTransform">transition</text>
    <div class="item" style="left: {{pointerLeft}}px;"></div>
  </div>
</template>

<style>
  .container {
    flex-direction: column;
    justify-content: center;
    align-items: center;
  }
  .item {
    width: 150px;
    height: 80px;
    background-color: yellow;
    transition: left 1s ease;
  }
  .title {
    font-size: 60px;
  }
</style>

<script>
  module.exports = {
    data: {
      pointerLeft: 0
    },
    startTransform() {
      this.pointerLeft = 100
    }
  }

</script>

【问题分析】

上述代码实现了一个item的横向移动的动画,通过left属性来显示的一个动画效果。但是在点击触发过渡动画时,是直接在对应位置显示的,动画效果并未展现。这是因为快应用中transition-property支持的通用样式属性如下:width,height,background-color,background-position,opacity,transform,暂不支持left属性来实现过渡动画。

【解决方案】

虽然快应用暂不支持left/right/top/bottom来实现过渡动画,我们仍然是可以实现这样的效果的,通过transform来控制就可以。

代码如下:

<template>
  <!-- Only one root node is allowed in template. -->
  <div class="container">
    <text class="title" onclick="startTransform">transition</text>
    <div class="item" style="transform: translateX({{pointerLeft}}px);"></div>
  </div>
</template>

<style>
  .container {
    flex-direction: column;
    justify-content: center;
    align-items: center;
  }
  .item {
    width: 150px;
    height: 80px;
    background-color: yellow;
    transform: translateX(0px);
    transition: transform 1s ease;
  }

  .title {
    font-size: 60px;
  }
</style>

 

<script>
  module.exports = {
    data: {
      pointerLeft: 0
    },
    startTransform() {
      this.pointerLeft = 100
    }
  }
</script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值