原生小程序开发组件|视图容器组件汇总(三)

movable-area

movable-view 的可移动区域。

属性说明

属性类型默认值必填说明
scale-areabooleanfalse当里面的 movable-view 设置为支持双指缩放时,设置此值可将缩放手势生效区域修改为整个 movable-area

示例代码

TYML
<view class="page-section">
  <view class="page-section-title l-r-padding">movable移动功能</view>
  <view class="area-wrap">
    <movable-area class="area">
      <movable-view class="block" x="{{x}}" y="{{y}}" direction="all">
        text
      </movable-view>
    </movable-area>
  </view>
  <view class="btn-wrap">
    <view class="btn-line">
      <button type="primary" bind:tap="moveFn" class="btn">点击移动到(30px, 30px)</button>
    </view>
    <view class="btn-line">
      <button type="primary" bind:tap="moveStep" class="btn sec-btn">点击移动到({{x + 10}}px, {{y + 10}}px)</button>
    </view>
  </view>
</view>
 
<view class="page-section">
  <view class="page-section-title l-r-padding">只可以横向移动</view>
  <view class="area-wrap">
    <movable-area class="area">
      <movable-view class="block" direction="horizontal">text</movable-view>
    </movable-area>
  </view>
</view>
 
<view class="page-section">
  <view class="page-section-title l-r-padding">只可以纵向移动</view>
  <view class="area-wrap">
    <movable-area class="area">
      <movable-view class="block" direction="vertical">text</movable-view>
    </movable-area>
  </view>
</view>
 
<view class="page-section">
  <view class="page-section-title l-r-padding">可超出边界</view>
  <view class="area-wrap">
    <movable-area class="area">
      <movable-view class="block" direction="all" out-of-bounds="true">text</movable-view>
    </movable-area>
  </view>
</view>

JS
Page({
  data: {
    x: 0,
    y: 0,
  },
  // 移动到30px,30px
  moveFn() {
    this.setData({
      x: 30,
      y: 30,
    });
  },
  moveStep() {
    const x = this.data.x + 10;
    const y = this.data.y + 10;
    this.setData({
      x,
      y,
    });
  },
});

👉 立即免费领取开发资源,体验涂鸦 MiniApp 小程序开发。

TYSS
.page-section {
  width: 100%;
  margin-bottom: 30px;
}
 
.area-wrap {
  display: flex;
  justify-content: space-around;
}
 
.area {
  flex: none;
  height: 200px;
  width: 200px;
  margin: 10px;
  background-color: #ccc;
  overflow: hidden;
}
 
.block {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 50px;
  width: 50px;
  background: var(--ty-native-primary-color);
  color: #fff;
}
 
.btn-line {
  text-align: center;
}
 
.btn-line .btn {
  display: inline-block;
  width: 240px;
}
 
.btn-line .sec-btn {
  margin-top: 10px;
}

movable-view

可移动的视图容器,在页面中可以拖拽滑动。movable-view 必须在 movable-area 组件中,并且必须是直接子节点,否则不能移动。

属性说明

属性类型默认值必填说明
directionstringnonemovable-view 的移动方向,属性值有 all、vertical、horizontal、none
inertiabooleanfalsemovable-view 是否带有惯性
out-of-boundsbooleanfalse超过可移动区域后,movable-view 是否还可以移动
xnumber0定义 x 轴方向的偏移,如果 x 的值不在可移动范围内,会自动移动到可移动范围。改变 x 的值会触发动画
ynumber0定义 y 轴方向的偏移,如果 y 的值不在可移动范围内,会自动移动到可移动范围。改变 y 的值会触发动画
dampingnumber20阻尼系数,用于控制 x 或 y 改变时的动画和过界回弹的动画,值越大移动越快
frictionnumber2摩擦系数,用于控制惯性滑动的动画,值越大摩擦力越大,滑动越快停止;必须大于 0,否则会被设置成默认值
disabledbooleanfalse是否禁用
scalebooleanfalse是否支持双指缩放,默认缩放手势生效区域是在 movable-view 内
scale-minnumber0.5定义缩放倍数最小值
scale-maxnumber10定义缩放倍数最大值
scale-valuenumber1定义缩放倍数,取值范围为 0.5 - 10
animationbooleantrue是否使用动画
bind:changeeventhandle拖动过程中触发的事件,event.detail = {x, y, source}
bind:scaleeventhandle缩放过程中触发的事件,event.detail = {x, y, scale}

bind:change 返回的 source 表示产生移动的原因

说明
touch拖动
touch-out-of-bounds超出移动范围
out-of-bounds超出移动范围后的回弹
friction惯性
空字符串setData

注意事项

tip: movable-view 必须设置 width 和 height 属性,不设置默认为 10px tip: movable-view 默认为绝对定位,top 和 left 属性为 0px

👉 立即免费领取开发资源,体验涂鸦 MiniApp 小程序开发。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IoT砖家涂拉拉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值