Blame view

pages/API/get-node-info/get-node-info.vue 2.32 KB
4b045f7c   刘淇   江阴初始化项目
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
  <template>
  	<view>
  		<page-head :title="title"></page-head>
  		<view class="uni-padding-wrap uni-common-mt">
  			<view class="movable block">
  				<!-- #ifndef MP-TOUTIAO -->
  				<movable-area>
  					<movable-view class="target" direction="all" @change="getNodeInfo">Drag</movable-view>
  				</movable-area>
  				<!-- #endif -->
  				<!-- #ifdef MP-TOUTIAO -->
  				<view class="target" @click="setPosition" :style="{top:top,left:left}">Click</view>
  				<!-- #endif -->
  			</view>
  			<view class="movable">
  				<view class="info">
  					<view v-for="(item,index) in info" :key="index">
  						<text class="b">{{item.key}}</text>
  						<text class="span">{{item.val}}</text>
  					</view>
  				</view>
  			</view>
  		</view>
  	</view>
  </template>
  
  <script>
  	export default {
  		data() {
  			return {
  				title: 'createSelectorQuery',
  				top: 0,
  				left: '0px',
  				info: []
  			}
  		},
  		onReady() {
  			this.getNodeInfo()
  		},
  		methods: {
  			setPosition() {
  				this.left = Math.random() * uni.upx2px(320) + 'px'
  				this.top = Math.random() * uni.upx2px(320) + 'px'
  				this.getNodeInfo()
  			},
  			getNodeInfo() {
  				uni.createSelectorQuery().select('.target').boundingClientRect().exec((ret) => {
  					const rect = ret[0]
  					if (rect) {
                          const sort = ['left','right','top','bottom','width','height']
  						const info = []
  						for (let i in sort) {
                              let key = sort[i]
                              info.push({
                                  'key': key,
                                  'val': rect[key]
                              })
  						}
  						this.info = info
  					}
  				});
  			}
  		},
  	}
  </script>
  
  <style>
  	.movable {
  		display: flex;
  		justify-content: center;
  	}
  
  	.block {
  		height: 400rpx;
  		width: 400rpx;
  		background-color: #FFFFFF;
  		border: 1px solid #ccc;
  		position: relative;
  		margin: 0 auto;
  		margin-bottom: 30rpx;
  	}
  
  	movable-area {
  		height: 400rpx;
  		width: 400rpx;
  		position: relative;
  	}
  
  	.target {
  		height: 80rpx;
  		width: 80rpx;
  		line-height: 80rpx;
  		text-align: center;
  		color: #FFFFFF;
  		background-color: #4cd964;
  		font-size: 28rpx;
  		position: absolute;
  	}
  
  	.info {
  		display: flex;
  		flex-direction: column;
  		justify-content: center;
  		align-items: center;
  	}
  
  	.b {
  		font-weight: bold;
  		width: 150rpx;
  		display: inline-block;
  	}
  
  	.span {
  		width: 100rpx;
  		display: inline-block;
  	}
  </style>