common.md 7.5 KB
Newer Older
D
DCloud_LXH 已提交
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 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
# 组件的公共属性和事件

每个组件都有属性和事件。有些属性和事件,是所有组件都支持的。

## 组件公共属性

- id
- ref
- style
- class
- data-

<!-- CUSTOMTYPEJSON.general-attribute.example -->

## 组件公共事件

- @touchstart
- @touchmove
- @touchend
- @touchcancel
- @tap
- @click //与tap等价
- @longpress
- @transitionend

在多点触摸的屏幕上,touch事件返回数组,包含了每个touch点对应的x、y坐标。

### transition 事件

- @transitionend

	transition 效果结束时触发
	
	#### 兼容性
	
	安卓 3.93+ 版本开始支持
	
```vue
<template>
  <image class="transition-transform" id="transition-transform" @transitionend="onEnd" src="/static/uni.png"></image>
</template>
<script>
  export default {
    data() {
      return {}
    },
    onReady() {
      var element = uni.getElementById('transition-transform')
      element!.style.setProperty('transform', 'rotate(360deg)')
    },
    methods: {
      onEnd() {
        console.log("transition效果结束")
      }
    }
  }
</script>

<style>
  .transition-transform {
    transition-duration: 2000;
    transition-property: transform;
    transform: rotate(0deg);
  }
</style>
```

### 冒泡事件系统

DOM事件主要有三个阶段:捕获阶段、目标阶段和冒泡阶段。

以点击事件为例,当触发点击时,
1. 首先从根节点逐级向下分发,直到监听点击事件的节点为止(捕获阶段);
2. 然后事件到达当前节点并触发点击事件(目标阶段);
3. 接着继续向上逐级触发父节点的点击事件,直到根节点为止(冒泡阶段)。

注意,虽然有3个阶段,但第2个阶段(“目标阶段”:事件到达了元素)并没有单独处理:捕获和冒泡阶段的处理程序都会在该阶段触发。

我们一般使用默认的事件注册机制,将事件注册到冒泡阶段,相对来说,大多数处理情况都在冒泡阶段。

uvue 目前暂不支持事件的捕获阶段。

#### 阻止冒泡

在事件回调中,可以通过调用`event.stopPropagation`方法阻止事件冒泡。

```ts
handleClick (event : MouseEvent) {
    // 阻止继续冒泡.
    event.stopPropagation();
}
```

#### 阻止默认行为

在事件回调中,可以通过调用`event.preventDefault`方法阻止默认行为。`event.preventDefault`仅处理默认行为,事件冒泡不会被阻止。

```vue
<template>
	<scroll-view style="flex: 1;">
		<view style="width: 750rpx;height: 1750rpx;background-color: bisque;">
			滑动框中区域修改进度并阻止滚动,滑动其余空白区域触发滚动
			<view style="width: 750rpx;height: 40rpx; margin-top: 100rpx;border:5rpx;" @touchmove="slider">
				<view ref="view1" style="background-color: chocolate;width: 0rpx;height: 30rpx;"></view>
			</view>
		</view>
	</scroll-view>
</template>
<script>
	export default {
		data() {
			return {
				$view1Element: null as UniElement | null
			}
		},
    onReady() {
      this.$view1Element = this.$refs['view1'] as UniElement
    },
		methods: {
			slider(e : TouchEvent) {
				e.preventDefault() // 阻止外层scroll-view滚动行为
				this.$view1Element!.style?.setProperty('width', e.touches[0].screenX);
			}
		}
	}
</script>
```

## Event

<!-- CUSTOMTYPEJSON.Event.description -->

<!-- CUSTOMTYPEJSON.Event.extends -->

<!-- CUSTOMTYPEJSON.Event.param -->



### Event 方法 @event-methods
<!-- CUSTOMTYPEJSON.Event.methods.stopPropagation.name -->

<!-- CUSTOMTYPEJSON.Event.methods.stopPropagation.description -->

<!-- CUSTOMTYPEJSON.Event.methods.stopPropagation.param -->

<!-- CUSTOMTYPEJSON.Event.methods.stopPropagation.returnValue -->

<!-- CUSTOMTYPEJSON.Event.methods.stopPropagation.compatibility -->

<!-- CUSTOMTYPEJSON.Event.methods.stopPropagation.tutorial -->

<!-- CUSTOMTYPEJSON.Event.methods.preventDefault.name -->

<!-- CUSTOMTYPEJSON.Event.methods.preventDefault.description -->

<!-- CUSTOMTYPEJSON.Event.methods.preventDefault.param -->

<!-- CUSTOMTYPEJSON.Event.methods.preventDefault.returnValue -->

<!-- CUSTOMTYPEJSON.Event.methods.preventDefault.compatibility -->

<!-- CUSTOMTYPEJSON.Event.methods.preventDefault.tutorial -->

## CustomEvent

<!-- CUSTOMTYPEJSON.CustomEvent.description -->

<!-- CUSTOMTYPEJSON.CustomEvent.extends -->

<!-- CUSTOMTYPEJSON.CustomEvent.param -->



### CustomEvent 方法 @customevent-methods
<!-- CUSTOMTYPEJSON.CustomEvent.methods.stopPropagation.name -->

<!-- CUSTOMTYPEJSON.CustomEvent.methods.stopPropagation.description -->

<!-- CUSTOMTYPEJSON.CustomEvent.methods.stopPropagation.param -->

<!-- CUSTOMTYPEJSON.CustomEvent.methods.stopPropagation.returnValue -->

<!-- CUSTOMTYPEJSON.CustomEvent.methods.stopPropagation.compatibility -->

<!-- CUSTOMTYPEJSON.CustomEvent.methods.stopPropagation.tutorial -->

<!-- CUSTOMTYPEJSON.CustomEvent.methods.preventDefault.name -->

<!-- CUSTOMTYPEJSON.CustomEvent.methods.preventDefault.description -->

<!-- CUSTOMTYPEJSON.CustomEvent.methods.preventDefault.param -->

<!-- CUSTOMTYPEJSON.CustomEvent.methods.preventDefault.returnValue -->

<!-- CUSTOMTYPEJSON.CustomEvent.methods.preventDefault.compatibility -->

<!-- CUSTOMTYPEJSON.CustomEvent.methods.preventDefault.tutorial -->

## CustomEventOptions

<!-- CUSTOMTYPEJSON.CustomEventOptions.description -->

<!-- CUSTOMTYPEJSON.CustomEventOptions.extends -->

<!-- CUSTOMTYPEJSON.CustomEventOptions.param -->

## MouseEvent

<!-- CUSTOMTYPEJSON.MouseEvent.description -->

<!-- CUSTOMTYPEJSON.MouseEvent.extends -->

<!-- CUSTOMTYPEJSON.MouseEvent.param -->


### MouseEvent 方法 @mouseevent-methods
<!-- CUSTOMTYPEJSON.MouseEvent.methods.stopPropagation.name -->

<!-- CUSTOMTYPEJSON.MouseEvent.methods.stopPropagation.description -->

<!-- CUSTOMTYPEJSON.MouseEvent.methods.stopPropagation.param -->

<!-- CUSTOMTYPEJSON.MouseEvent.methods.stopPropagation.returnValue -->

<!-- CUSTOMTYPEJSON.MouseEvent.methods.stopPropagation.compatibility -->

<!-- CUSTOMTYPEJSON.MouseEvent.methods.stopPropagation.tutorial -->

<!-- CUSTOMTYPEJSON.MouseEvent.methods.preventDefault.name -->

<!-- CUSTOMTYPEJSON.MouseEvent.methods.preventDefault.description -->

<!-- CUSTOMTYPEJSON.MouseEvent.methods.preventDefault.param -->

<!-- CUSTOMTYPEJSON.MouseEvent.methods.preventDefault.returnValue -->

<!-- CUSTOMTYPEJSON.MouseEvent.methods.preventDefault.compatibility -->

<!-- CUSTOMTYPEJSON.MouseEvent.methods.preventDefault.tutorial -->

## TouchEvent

<!-- CUSTOMTYPEJSON.TouchEvent.description -->

<!-- CUSTOMTYPEJSON.TouchEvent.extends -->

<!-- CUSTOMTYPEJSON.TouchEvent.param -->

### TouchEvent 方法 @touchevent-methods
<!-- CUSTOMTYPEJSON.TouchEvent.methods.stopPropagation.name -->

<!-- CUSTOMTYPEJSON.TouchEvent.methods.stopPropagation.description -->

<!-- CUSTOMTYPEJSON.TouchEvent.methods.stopPropagation.param -->

<!-- CUSTOMTYPEJSON.TouchEvent.methods.stopPropagation.returnValue -->

<!-- CUSTOMTYPEJSON.TouchEvent.methods.stopPropagation.compatibility -->

<!-- CUSTOMTYPEJSON.TouchEvent.methods.stopPropagation.tutorial -->

<!-- CUSTOMTYPEJSON.TouchEvent.methods.preventDefault.name -->

<!-- CUSTOMTYPEJSON.TouchEvent.methods.preventDefault.description -->

<!-- CUSTOMTYPEJSON.TouchEvent.methods.preventDefault.param -->

<!-- CUSTOMTYPEJSON.TouchEvent.methods.preventDefault.returnValue -->

<!-- CUSTOMTYPEJSON.TouchEvent.methods.preventDefault.compatibility -->

<!-- CUSTOMTYPEJSON.TouchEvent.methods.preventDefault.tutorial -->

## Touch

<!-- CUSTOMTYPEJSON.Touch.description -->

<!-- CUSTOMTYPEJSON.Touch.extends -->

<!-- CUSTOMTYPEJSON.Touch.param -->

<!-- CUSTOMTYPEJSON.general-event.example -->