# movable-view

可移动的视图容器,在页面中可以拖拽滑动或双指缩放。

movable-view必须在movable-area组件中,并且必须是直接子节点,否则不能移动。

# 使用示例


扫码预览
用BoosHi扫码或PC端点击
<template>
  <view class="demo-ui-page">
    <view class="demo-title">movable-view 可拖动视图</view>
    <demo-cell title="movable-view 必须在 movable-area 组件内"  noPadding></demo-cell>
    <demo-cell title="movable-view 区域小于 movable-area">
      <movable-area>
				<movable-view :x="x" :y="y" direction="all" @change="onChange">text</movable-view>
			</movable-area>
      <bz-button @click="tap">点这里移动至(30px,30px)</bz-button>
    </demo-cell>
    <demo-cell title="movable-view区域大于movable-area">
      <movable-area>
				<movable-view class="max" direction="all">text</movable-view>
			</movable-area>
    </demo-cell>
    <demo-cell title="只可以横向移动" subTitle="direction=horizontal">
      <movable-area>
				<movable-view direction="horizontal">text</movable-view>
			</movable-area>
    </demo-cell>
    <demo-cell title="可超出边界" subTitle="out-of-bounds">
      <movable-area>
				<movable-view direction="all" out-of-bounds>text</movable-view>
			</movable-area>
    </demo-cell>
    <demo-cell title="带有惯性" subTitle="inertia">
      <movable-area>
				<movable-view direction="all" inertia>text</movable-view>
			</movable-area>
    </demo-cell>
    <demo-cell title="可放缩">
      <movable-area scale-area>
				<movable-view direction="all" @scale="onScale" scale scale-min="0.5" scale-max="4" :scale-value="scale">text</movable-view>
			</movable-area>
      <bz-button @click="tap2">点击这里放大3倍</bz-button>
    </demo-cell>
  </view>
</template>

<script>
	export default {
    data() {
			return {
				x: 0,
				y: 0,
				scale: 1.5,
				old: {
					x: 0,
					y: 0,
					scale: 2
				}
			}
		},
		methods: {
			tap(e) {
				// 解决view层不同步的问题
				this.x = this.old.x
				this.y = this.old.y
				this.$nextTick(function() {
					this.x = 30
					this.y = 30
				})
			},
			tap2() {
				// 解决view层不同步的问题
				this.scale = this.old.scale
				this.scale = this.old.scale
				this.$nextTick(function() {
					this.scale = 3
				})
			},
			onChange(e) {
				this.old.x = e.detail.x
				this.old.y = e.detail.y
			},
			onScale(e) {
				this.old.scale = e.detail.scale
			}
		}
	}
</script>

<style>
	movable-view {
		display: flex;
		align-items: center;
		justify-content: center;
		height: 150rpx;
		width: 150rpx;
		background-color: #007AFF;
		color: #fff;
	}
	movable-area {
		height: 300rpx;
		width: 100%;
		background-color: #D8D8D8;
		overflow: hidden;
	}
	.max {
		width:500rpx;
		height: 500rpx;
	}
</style>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
展开 刷新 关闭

# 属性说明

属性名 类型 默认值 说明
direction String none movable-view的移动方向,属性值有all、vertical、horizontal、none
inertia Boolean false movable-view是否带有惯性
out-of-bounds Boolean false 超过可移动区域后,movable-view是否还可以移动
x Number / String 定义x轴方向的偏移,如果x的值不在可移动范围内,会自动移动到可移动范围;改变x的值会触发动画
y Number / String 定义y轴方向的偏移,如果y的值不在可移动范围内,会自动移动到可移动范围;改变y的值会触发动画
damping Number 20 阻尼系数,用于控制x或y改变时的动画和过界回弹的动画,值越大移动越快
friction Number 2 摩擦系数,用于控制惯性滑动的动画,值越大摩擦力越大,滑动越快停止;必须大于0,否则会被设置成默认值
disabled Boolean false 是否禁用
scale Boolean false 是否支持双指缩放,默认缩放手势生效区域是在movable-view内
scale-min Number 0.5 定义缩放倍数最小值
scale-max Number 10 定义缩放倍数最大值
scale-value Number 1 定义缩放倍数,取值范围为 0.5 - 10
animation Boolean true 是否使用动画
@change EventHandle 拖动过程中触发的事件,event.detail = {x: x, y: y, source: source},其中source表示产生移动的原因,值可为touch(拖动)、touch-out-of-bounds(超出移动范围)、out-of-bounds(超出移动范围后的回弹)、friction(惯性)和空字符串(setData)
@scale EventHandle 缩放过程中触发的事件,event.detail = {x: x, y: y, scale: scale},

Tips:

  • movable-view 必须设置width和height属性,不设置默认为 10px
  • movable-view 默认为绝对定位,top 和 left 属性为 0px
  • 当 movable-view 小于 movable-area 时,movable-view的移动范围是在 movable-area 内;当 movable-view 大于 movable-area 时,movable-view 的移动范围必须包含 movable-area(x轴方向和y轴方向分开考虑)
  • movable-view必须在<movable-area/>组件中,并且必须是直接子节点,否则不能移动。
最后更新于 : 12/15/2022, 2:18:25 PM