UniElement
UVUE DOM 元素对象,描述了 UVUE DOM 元素所普通具有的属性和方法。
UniElement 兼容性
| Web | 微信小程序 | Android | iOS | HarmonyOS | HarmonyOS uni-app x UTS 插件 |
| 4.0 | x | 4.0 | 4.11 | 4.61 | 4.61 |
UniElement 的属性值
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
| isConnected | boolean | 是 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
iOS uni-app x UTS 插件
|
HarmonyOS 系统版本
|
HarmonyOS
| | 4.0 | x | x | x | x | 12 | 4.61 |
| 只读属性 节点是否与 DOM 树连接 |
| uniPage | UniPage | 是 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
iOS uni-app x UTS 插件
|
HarmonyOS 系统版本
|
HarmonyOS
| | 4.0 | x | x | x | x | 12 | 4.61 |
| 元素所属的页面对象 |
| classList | Array<string> | 是 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
iOS uni-app x UTS 插件
|
HarmonyOS
| | 4.0 | x | 4.0 | 4.11 | x | - |
| 只读属性 获取当前元素的的 class 属性的动态集合。 |
| firstChild | UniElement | 否 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
iOS uni-app x UTS 插件
|
HarmonyOS
| | 4.0 | x | 4.0 | 4.11 | 4.25 | - |
| 只读属性 获取当前元素的的第一个子元素,如果元素是无子元素,则返回 null。 |
| lastChild | UniElement | 否 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
iOS uni-app x UTS 插件
|
HarmonyOS
| | 4.0 | x | 4.0 | 4.11 | 4.25 | - |
| 只读属性 获取当前元素的最后一个子元素,如果没有子元素,则返回 null。 |
| parentElement | UniElement | 否 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
iOS uni-app x UTS 插件
|
HarmonyOS
|
HarmonyOS uni-app x UTS 插件
| | 4.0 | x | 4.0 | 4.11 | 4.25 | 4.61 | 4.61 |
| 只读属性 获取当前元素在 DOM 树中的父元素,如果没有父元素(如未添加到DOM树中),则返回null。 |
| previousSibling | UniElement | 否 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
iOS uni-app x UTS 插件
|
HarmonyOS
|
HarmonyOS uni-app x UTS 插件
| | 4.0 | x | 4.0 | 4.11 | 4.25 | 4.61 | 4.61 |
| 只读属性 获取当前元素的前一个同级元素,没有则返回null。 |
| nextElementSibling | UniElement | 否 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
iOS uni-app x UTS 插件
|
HarmonyOS
|
HarmonyOS uni-app x UTS 插件
| | 4.0 | x | 4.0 | 4.11 | 4.25 | 4.61 | 4.61 |
| 只读属性 获取在 DOM 树中紧跟在其后面的同级元素,如果指定的元素为最后一个元素,则返回 null。 |
| children | Array<UniElement> | 是 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
iOS uni-app x UTS 插件
|
HarmonyOS
|
HarmonyOS uni-app x UTS 插件
| | 4.0 | x | 4.0 | 4.11 | 4.25 | 4.61 | 4.61 |
| 只读属性 获取当前元素包含的子元素的集合 |
| tagName | string | 是 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
iOS uni-app x UTS 插件
|
HarmonyOS
|
HarmonyOS uni-app x UTS 插件
| | 4.0 | 4.41 | 4.0 | 4.11 | 4.25 | 4.61 | 4.61 |
| 只读属性 获取当前元素的标签名 |
| nodeName | string | 是 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
iOS uni-app x UTS 插件
|
HarmonyOS
|
HarmonyOS uni-app x UTS 插件
| | 4.0 | x | 4.0 | 4.11 | 4.25 | 4.61 | 4.61 |
| 只读属性 获取当前元素的元素名称 |
| dataset | Map<string, any> | 是 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
iOS uni-app x UTS 插件
|
HarmonyOS
|
HarmonyOS uni-app x UTS 插件
| | 4.0 | 4.41 仅在event对象内的target上可用 | 4.0 | 4.11 | x | 4.61 | 4.61 |
| 只读属性 获取元素上自定义数据属性(data-*)的集合 |
| attributes | Map<string, any> | 是 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
iOS uni-app x UTS 插件
|
HarmonyOS
|
HarmonyOS uni-app x UTS 插件
| | 4.0 | x | 4.0 | 4.11 | 4.25 | 4.61 | 4.61 |
| 只读属性 获取元素上所有属性元素的集合 |
| style | CSSStyleDeclaration | 是 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
iOS uni-app x UTS 插件
|
HarmonyOS
|
HarmonyOS uni-app x UTS 插件
| | 4.0 | 4.41 | 4.0 | 4.11 | 4.25 | 4.61 | 4.61 |
| 只读属性 获取元素的CSS样式对象 |
| scrollWidth | number | 是 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
iOS uni-app x UTS 插件
|
HarmonyOS
|
HarmonyOS uni-app x UTS 插件
| | 4.0 | x | 4.0 | 4.11 | 4.25 | 4.61 | 4.61 |
| 只读属性 获取可滚动元素内容的总宽度,仅scroll-view、list-view组件支持,其他组件返回视图宽度 |
| scrollHeight | number | 是 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
iOS uni-app x UTS 插件
|
HarmonyOS
|
HarmonyOS uni-app x UTS 插件
| | 4.0 | x | 4.0 | 4.11 | 4.25 | 4.61 | 4.61 |
| 只读属性 获取可滚动元素内容的总高度,仅scroll-view、list-view组件支持,其他组件返回视图高度 |
| scrollLeft | number | 是 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
iOS uni-app x UTS 插件
|
HarmonyOS
|
HarmonyOS uni-app x UTS 插件
| | 4.0 | x | 4.0 | 4.11 | 4.25 | 4.61 | 4.61 |
| 获取或修改元素滚动条到元素左边的距离像素数,仅scroll-view、list-view组件支持。其他组件返回0 |
| scrollTop | number | 是 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
iOS uni-app x UTS 插件
|
HarmonyOS
|
HarmonyOS uni-app x UTS 插件
| | 4.0 | x | 4.0 | 4.11 | 4.25 | 4.61 | 4.61 |
| 获取或修改元素滚动条到元素顶部的距离像素数,仅scroll-view、list-view组件支持。其他组件返回0 |
| offsetLeft | number | 是 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
iOS uni-app x UTS 插件
|
HarmonyOS
|
HarmonyOS uni-app x UTS 插件
| | 4.0 | 4.41 仅在event对象内的target上可用 | 4.0 | 4.11 | 4.25 | 4.61 | 4.61 |
| 只读属性 元素的左边界偏移值 单位px |
| offsetTop | number | 是 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
iOS uni-app x UTS 插件
|
HarmonyOS
|
HarmonyOS uni-app x UTS 插件
| | 4.0 | 4.41 仅在event对象内的target上可用 | 4.0 | 4.11 | 4.25 | 4.61 | 4.61 |
| 只读属性 元素的顶部边界偏移值 单位px |
| offsetWidth | number | 是 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
iOS uni-app x UTS 插件
|
HarmonyOS
|
HarmonyOS uni-app x UTS 插件
| | 4.0 | x | 4.0 | 4.11 | 4.25 | 4.61 | 4.61 |
| 只读属性 元素的布局宽度,宽度包含border、padding的数据值 单位px |
| offsetHeight | number | 是 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
iOS uni-app x UTS 插件
|
HarmonyOS
|
HarmonyOS uni-app x UTS 插件
| | 4.0 | x | 4.0 | 4.11 | 4.25 | 4.61 | 4.61 |
| 只读属性 元素的布局高度,高度包含border、padding的数据值 单位px |
| ext | Map<string, any> | 是 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
iOS uni-app x UTS 插件
|
HarmonyOS
|
HarmonyOS uni-app x UTS 插件
| | 4.0 | x | 4.0 | 4.11 | x | 4.61 | 4.61 |
| 只读属性 扩展属性 |
style
App端
获取的是元素对象计算后的CSS样式集合对象,包括通过样式选择器设置的CSS样式。
Web端
获取的是元素对象style属性设置的CSS样式集合对象,不包括通过样式选择器设置的CSS样式。
小程序端
获取的是元素对象style属性设置的CSS样式集合对象(仅限通过 UniElement.style 的API设置的),不包括通过样式选择器设置的CSS样式,也不包含在模板上绑定的style属性。
UniElement 的方法
appendChild(aChild: UniElement): void
将一个元素添加到指定父元素的子元素列表的末尾处。如果将被插入的元素已经存在于当前文档的文档树中,那么将会它从原先的位置移动到新的位置。
appendChild 兼容性
| Web | 微信小程序 | Android | iOS | iOS uni-app x UTS 插件 | HarmonyOS | HarmonyOS uni-app x UTS 插件 |
| 4.0 | x | 4.0 | 4.11 | 4.25 | 4.61 | 4.61 |
参数
| 名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
| aChild | UniElement | 是 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
|
HarmonyOS uni-app x UTS 插件
| | 4.0 | x | 4.0 | 4.11 | 4.61 | 4.61 |
| 插入子元素对象 |
参见
insertBefore(newChild: UniElement, refChild?: UniElement | null): UniElement | null
在参考元素之前插入一个拥有指定父元素的子元素。如果给定的子元素是对文档中现有元素的引用,insertBefore() 会将其从当前位置移动到新位置。
insertBefore 兼容性
| Web | 微信小程序 | Android | iOS | iOS uni-app x UTS 插件 | HarmonyOS | HarmonyOS uni-app x UTS 插件 |
| 4.0 | x | 4.0 | 4.11 | 4.25 | 4.61 | 4.61 |
参数
| 名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
| newChild | UniElement | 是 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
|
HarmonyOS uni-app x UTS 插件
| | 4.0 | x | 4.0 | 4.11 | 4.61 | 4.61 |
| 插入子元素对象 |
| refChild | UniElement | 否 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
|
HarmonyOS uni-app x UTS 插件
| | 4.0 | x | 4.0 | 4.11 | 4.61 | 4.61 |
| 已存在父元素的子元素对象 |
返回值
参见
insertBefore(newChild: UniElement): UniElement | null
将一个元素添加到指定父元素的子元素列表的末尾处 功能等同于appendChild
insertBefore 兼容性
| Web | 微信小程序 | Android | iOS | iOS uni-app x UTS 插件 | HarmonyOS | HarmonyOS uni-app x UTS 插件 |
| 4.0 | x | 4.0 | 4.11 | 4.25 | 4.61 | 4.61 |
参数
| 名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
| newChild | UniElement | 是 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
|
HarmonyOS uni-app x UTS 插件
| | 4.0 | x | 4.0 | 4.11 | 4.61 | 4.61 |
| 插入子元素对象 |
返回值
参见
setAttribute(key: string, value: string): void
设置指定元素上的某个属性值。如果设置的属性已经存在,则更新该属性值;否则使用指定的名称和值添加一个新的属性。
setAttribute 兼容性
| Web | 微信小程序 | Android | iOS | iOS uni-app x UTS 插件 | HarmonyOS | HarmonyOS uni-app x UTS 插件 |
| 4.0 | x | 4.0 | 4.11 | 4.25 | 4.61 | 4.61 |
注意
- setAttribute从HBuilderX 3.93起,调整为只能保存string类型属性值,需要保存其它类型数据请使用dataset属性。
- 为保证多端一致setAttribute不推荐用于修改本文档中的UniElement属性,如有此类需求应使用element.xxx设置,如element.scrollTop。其余绑定到内置组件的属性也尽量使用数据驱动而不是绕过vue去设置。
参数
| 名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
| key | string | 是 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | - | x | - | - | - |
| 属性名称 |
| value | string | 是 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | - | x | - | - | - |
| 属性值域 |
App平台
app平台 setAttribute 不支持设置 class、style 属性,设置了也不会生效,class 属性需在 uvue/vue 页面中设置,style 信息可以通过 style 属性设置。
参见
setAnyAttribute(key: string, value: any): void
设置指定元素上的某个属性值。功能等同setAttribute value支持任意类型
setAnyAttribute 兼容性
| Web | 微信小程序 | Android | iOS | iOS uni-app x UTS 插件 | HarmonyOS | HarmonyOS uni-app x UTS 插件 |
| x | x | 4.0 | 4.11 | 4.25 | 4.61 | 4.61 |
参数
| 名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
| key | string | 是 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | - | x | - | - | - |
| 属性名称 |
| value | any | 是 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | - | x | - | - | - |
| 属性值域 |
参见
getAttribute(key: string): string | null
获取元素指定的属性值,如果指定的属性不存在则返回null。
getAttribute 兼容性
| Web | 微信小程序 | Android | iOS | iOS uni-app x UTS 插件 | HarmonyOS | HarmonyOS uni-app x UTS 插件 |
| 4.0 | x | 4.0 | 4.11 | 4.25 | 4.61 | 4.61 |
注意
- getAttribute返回值从HBuilderX 3.93起,调整为string类型,不要使用此方法获取非string类型的属性值。如有非string需求,请使用对象的点操作符直接访问dateset属性,不通过getAttribute方法。
- 为保证多端一致getAttribute不应用于获取本文档中的UniElement属性,如有此类需求应使用element.xxx获取,如element.scrollTop。
参数
| 名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
| key | string | 是 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | - | x | - | - | - |
| 属性名称 |
返回值
App平台
app平台 getAttribute 不支持获取 class、style 属性, uvue/vue 页面中设置的 class 属性暂不支持通过 UniElement 对象获取,style 信息可以通过 style 属性获取。
示例
示例为hello uni-app x alpha分支,与最新HBuilderX Alpha版同步。与最新正式版同步的master分支示例另见
扫码体验 示例源码如下,请查看 pre > code 标签中的内容
<template>
<!-- #ifdef APP -->
<scroll-view class="page-scroll-view">
<!-- #endif -->
<view style="padding: 15px;">
<view id="box" ref="box">
<text class="uni-title-text">元素的id:{{ attrId }}</text>
<!-- #ifndef APP -->
<text class="uni-title-text">元素的style:{{ attrStyle }}</text>
<!-- #endif -->
<text class="uni-title-text">元素的背景色样式值:{{ propertyValue }}</text>
<text class="uni-subtitle-text">小程序端:getAttribute 获取元素的属性值,目前仅支持id、style</text>
<text class="uni-subtitle-text">App端:getAttribute 不支持获取 class、style 属性</text>
</view>
<button @click="getAttributeId">getAttribute获取元素的id</button>
<button @click="setStyle">setProperty设置背景色</button>
<!-- #ifndef APP -->
<button @click="getAttributeStyle">getAttribute获取元素的style</button>
<!-- #endif -->
<button @click="getPropertyValue">getPropertyValue获取背景色值</button>
<child id="child" ref="child"></child>
<button @click="getBoundingClientRectAsyncChild">获取自定义组件child元素信息</button>
<view class="rect-info" v-if="rectInfo">
<view class="node-info-item">
<text class="node-info-item-k">x: </text>
<text class="node-info-item-v">{{rectInfo.x}}</text>
</view>
<view class="node-info-item">
<text class="node-info-item-k">y: </text>
<text class="node-info-item-v">{{rectInfo.y}}</text>
</view>
<view class="node-info-item">
<text class="node-info-item-k">width: </text>
<text class="node-info-item-v">{{rectInfo.width}}</text>
</view>
<view class="node-info-item">
<text class="node-info-item-k">height: </text>
<text class="node-info-item-v">{{rectInfo.height}}</text>
</view>
<view class="node-info-item">
<text class="node-info-item-k">left: </text>
<text class="node-info-item-v">{{rectInfo.left}}</text>
</view>
<view class="node-info-item">
<text class="node-info-item-k">top: </text>
<text class="node-info-item-v">{{rectInfo.top}}</text>
</view>
<view class="node-info-item">
<text class="node-info-item-k">right: </text>
<text class="node-info-item-v">{{rectInfo.right}}</text>
</view>
<view class="node-info-item">
<text class="node-info-item-k">bottom: </text>
<text class="node-info-item-v">{{rectInfo.bottom}}</text>
</view>
</view>
<scroll-view ref="scrollView" class="scroll-view_H" direction="horizontal">
<view class="scroll-view-item_H uni-bg-red"><text class="text">A</text></view>
<view class="scroll-view-item_H uni-bg-green"><text class="text">B</text></view>
<view class="scroll-view-item_H uni-bg-blue"><text class="text">C</text></view>
</scroll-view>
<!-- #ifndef WEB -->
<button @click="scrollTo">scrollTo设置left滚动200px</button>
<!-- #endif -->
</view>
<!-- #ifdef APP -->
</scroll-view>
<!-- #endif -->
</template>
<script lang="uts">
import child from './child.uvue'
type DomRectType = {
x : number,
y : number,
left : number,
top : number,
right : number,
bottom : number,
width : number,
height : number,
}
export default {
components: {
child
},
data() {
return {
boxTarget: null as null | UniElement,
scrollViewTarget: null as null | UniElement,
attrId: "",
attrStyle:"",
propertyValue:"",
rectInfo: {
x: 0,
y: 0,
width: 0,
height: 0,
left: 0,
top: 0,
right: 0,
bottom: 0,
} as DomRectType,
}
},
onReady() {
this.boxTarget = this.$refs['box'] as UniElement
this.scrollViewTarget = this.$refs['scrollView'] as UniElement;
},
methods: {
getBoundingClientRectAsyncChild(){
const childEl = uni.getElementById('child')!
childEl.getBoundingClientRectAsync()!.then((rect : DOMRect) => {
console.log('rect: ',rect);
this.rectInfo = {
x: rect.x,
y: rect.y,
width: rect.width,
height: rect.height,
left: rect.left,
top: rect.top,
right: rect.right,
bottom: rect.bottom,
} as DomRectType;;
})
},
getAttributeId() {
this.attrId = this.boxTarget!.getAttribute('id') ?? ""
},
setStyle() {
this.boxTarget!.style.setProperty("background-color", "#FFF000")
},
getPropertyValue() {
this.propertyValue = this.boxTarget!.style.getPropertyValue("background-color")
},
getAttributeStyle() {
this.attrStyle = this.boxTarget!.getAttribute('style')?? ""
},
scrollTo() {
// #ifdef MP-WEIXIN
this.scrollViewTarget!.scrollTo({
left: 200
})
// #endif
// #ifndef MP-WEIXIN
this.scrollViewTarget!.scrollTo(200,0)
// #endif
}
}
}
</script>
<style>
.scroll-view_H {
width: 100%;
flex-direction: row;
margin-top:15px;
}
.scroll-view-item_H {
width: 100%;
height: 150px;
justify-content: center;
align-items: center;
}
.text {
font-size: 18px;
color: #ffffff;
}
.rect-info {
margin-top: 15px;
padding: 10px;
flex-direction: column;
}
.node-info-item {
flex-direction: row;
}
.node-info-item-k {
width: 72px;
line-height: 2;
}
.node-info-item-v {
font-weight: bold;
line-height: 2;
}
</style>
参见
getAnyAttribute(key: string): any | null
返回元素上一个指定的属性值。如果指定的属性不存在,则返回 null
getAnyAttribute 兼容性
| Web | 微信小程序 | Android | iOS | iOS uni-app x UTS 插件 | HarmonyOS | HarmonyOS uni-app x UTS 插件 |
| x | x | 4.0 | 4.11 | 4.25 | 4.61 | 4.61 |
参数
| 名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
| key | string | 是 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | - | x | - | - | - |
| 属性名称 |
返回值
参见
hasAttribute(key: string): boolean
返回该元素是否包含有指定的属性,属性存在则返回true,否则返回false。
hasAttribute 兼容性
| Web | 微信小程序 | Android | iOS | iOS uni-app x UTS 插件 | HarmonyOS | HarmonyOS uni-app x UTS 插件 |
| 4.0 | x | 4.0 | 4.11 | 4.25 | 4.61 | 4.61 |
参数
| 名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
| key | string | 是 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | - | x | - | - | - |
| 属性名称 |
返回值
参见
removeAttribute(key: string): void
从元素中删除一个属性,如果指定的属性不存在,则不做任何操作,也不会产生错误。
removeAttribute 兼容性
| Web | 微信小程序 | Android | iOS | iOS uni-app x UTS 插件 | HarmonyOS | HarmonyOS uni-app x UTS 插件 |
| 4.0 | x | 4.0 | 4.11 | 4.25 | 4.61 | 4.61 |
参数
| 名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
| key | string | 是 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | - | x | - | - | - |
| 属性名称 |
参见
getAndroidView(): View | null
获取元素android原生view 可能返回null
getAndroidView 兼容性
| Web | 微信小程序 | Android | iOS | HarmonyOS | HarmonyOS uni-app x UTS 插件 |
| x | x | 4.25 | x | x | x |
返回值
参见
getAndroidView获取原生View:
示例源码如下,请查看 pre > code 标签中的内容
//通过组件定义的id属性值,获取到UniElement对象
const element = uni.getElementById(id)
//getAndroidView不设置泛型,获取到安卓View
if(element != null) {
const view = element.getAndroidView()
}
更多示例请参考 uts 插件 uts-get-native-view
注意事项:
- 安卓平台页面渲染时元素才会构建View,所以元素刚创建就获取View大概率是null,推荐页面onReady时获取。
- 安卓平台获取的原生View尽可能的避免设置View的background属性,会导致元素background、border、boxshadow css效果失效或设置的background不生效,与设置background时机有关。
getAndroidView<T>(): T | null
获取元素android原生view 通过泛型定义view类型 可能返回null
getAndroidView 兼容性
| Web | 微信小程序 | Android | iOS | HarmonyOS | HarmonyOS uni-app x UTS 插件 |
| x | x | 4.25 | x | x | x |
返回值
参见
getAndroidView通过泛型定义获取原生View:
示例源码如下,请查看 pre > code 标签中的内容
//导入安卓原生WebView对象
import WebView from "android.webkit.WebView"
//通过web-view组件定义的id属性值,获取web-view标签的UniElement对象
const webViewElement = uni.getElementById(id)
//getAndroidView设置泛型为安卓底层WebView对象, 直接获取WebView 如果泛型不匹配会返回null
if(webViewElement != null) {
const webview = webViewElement.getAndroidView<WebView>()
}
获取到原生的WebView对象后,可以直接使用其所有属性和方法。原生对象的方法非常多,远多于 uni-app x 封装的API。这些方法都可以直接调用了。
在 hello uni-app x 的 组件 -> web-view 示例中,获取原生WebView对象,然后进一步使用了可否前进后退的方法。
可通过getAndroidView泛型明确定义View类型的组件:
注意事项:
- 安卓平台页面渲染时元素才会构建View,所以元素刚创建就获取View大概率是null,推荐页面onReady时获取。
- 安卓平台获取的原生View尽可能的避免设置View的background属性,会导致元素background、border、boxshadow 失效或设置的background不生效,与设置background时机有关。
getAndroidActivity(): Activity | null
获取元素android原生activity 可能返回null
getAndroidActivity 兼容性
| Web | 微信小程序 | Android | iOS | HarmonyOS | HarmonyOS uni-app x UTS 插件 |
| x | x | 4.25 | x | x | x |
返回值
参见
getBoundingClientRect(): DOMRect
获取元素的大小及其相对于窗口的位置信息。
getBoundingClientRect 兼容性
| Web | 微信小程序 | Android | iOS | iOS uni-app x UTS 插件 | HarmonyOS | HarmonyOS uni-app x UTS 插件 |
| 4.0 | x | 4.0 | 4.11 | 4.25 | 4.61 | 4.61 |
返回值
参见
getBoundingClientRectAsync(options?: GetBoundingClientRectAsyncOptions | null): Promise<DOMRect> | null
获取元素的大小及其相对于窗口的位置信息 异步。
getBoundingClientRectAsync 兼容性
| Web | 微信小程序 | Android | iOS | iOS uni-app x UTS 插件 | HarmonyOS | HarmonyOS uni-app x UTS 插件 |
| 4.41 | x | 4.41 | 4.41 | x | 4.61 | 4.61 |
参数
| 名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
| options | GetBoundingClientRectAsyncOptions | 否 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | - | x | - | - | - |
| |
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 | | success | (rect: DOMRect) => void | 否 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | - | x | - | - | - |
| | | fail | () => void | 否 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | - | x | - | - | - |
| | | complete | (rect?: any) => void | 否 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | - | x | - | - | - |
| |
|
返回值
示例
示例为hello uni-app x alpha分支,与最新HBuilderX Alpha版同步。与最新正式版同步的master分支示例另见
扫码体验 示例源码如下,请查看 pre > code 标签中的内容
<template>
<!-- #ifdef APP -->
<scroll-view class="page-scroll-view">
<!-- #endif -->
<view class="page" id="page">
<page-head :title="title"></page-head>
<button class="btn btn-get-all-node-info" @click="getBoundingClientRectAsync">getBoundingClientRectAsync</button>
<view id="rect-test" ref="rect-test" class="rect-test"></view>
<view class="rect-info" v-if="rectInfo">
<view class="node-info-item">
<text class="node-info-item-k">x: </text>
<text class="node-info-item-v">{{rectInfo.x}}</text>
</view>
<view class="node-info-item">
<text class="node-info-item-k">y: </text>
<text class="node-info-item-v">{{rectInfo.y}}</text>
</view>
<view class="node-info-item">
<text class="node-info-item-k">width: </text>
<text class="node-info-item-v">{{rectInfo.width}}</text>
</view>
<view class="node-info-item">
<text class="node-info-item-k">height: </text>
<text class="node-info-item-v">{{rectInfo.height}}</text>
</view>
<view class="node-info-item">
<text class="node-info-item-k">left: </text>
<text class="node-info-item-v">{{rectInfo.left}}</text>
</view>
<view class="node-info-item">
<text class="node-info-item-k">top: </text>
<text class="node-info-item-v">{{rectInfo.top}}</text>
</view>
<view class="node-info-item">
<text class="node-info-item-k">right: </text>
<text class="node-info-item-v">{{rectInfo.right}}</text>
</view>
<view class="node-info-item">
<text class="node-info-item-k">bottom: </text>
<text class="node-info-item-v">{{rectInfo.bottom}}</text>
</view>
</view>
</view>
<!-- #ifdef APP -->
</scroll-view>
<!-- #endif -->
</template>
<script>
type DomRectType = {
x : number,
y : number,
left : number,
top : number,
right : number,
bottom : number,
width : number,
height : number,
}
export default {
data() {
return {
title: 'getBoundingClientRectAsync',
rectInfo: {
x: 0,
y: 0,
width: 0,
height: 0,
left: 0,
top: 0,
right: 0,
bottom: 0,
} as DomRectType,
}
},
methods: {
getBoundingClientRectAsync() {
(this.$refs["rect-test"] as UniElement).getBoundingClientRectAsync()!.then((rect : DOMRect) => {
this.rectInfo = {
x: rect.x,
y: rect.y,
width: rect.width,
height: rect.height,
left: rect.left,
top: rect.top,
right: rect.right,
bottom: rect.bottom,
} as DomRectType;;
})
}
}
}
</script>
<style>
.page {
padding: 15px;
}
.btn {
margin-top: 15px;
}
.rect-test {
margin-top: 15px;
height: 100px;
background-color: dodgerblue;
}
.rect-info {
margin-top: 15px;
flex-direction: column;
}
.node-info-item {
flex-direction: row;
}
.node-info-item-k {
width: 72px;
line-height: 2;
}
.node-info-item-v {
font-weight: bold;
line-height: 2;
}
</style>
参见
getDrawableContext(): DrawableContext | null
获取组件的绘制对象,仅uvue页面中的 view 组件支持,其它组件不支持则返回null。
getDrawableContext 兼容性
| Web | 微信小程序 | Android | iOS | iOS uni-app x UTS 插件 | HarmonyOS | HarmonyOS uni-app x UTS 插件 |
| x | x | 3.9 | 4.11 | 4.25 | 4.61 | 4.61 |
返回值
参见
getIOSView(): UIView | null
获取元素ios原生view
getIOSView 兼容性
| Web | 微信小程序 | Android | Android uni-app x UTS 插件 | iOS | iOS uni-app x UTS 插件 | HarmonyOS | HarmonyOS uni-app x UTS 插件 |
| x | x | x | x | x | 4.25 | x | x |
返回值
参见
getIOSView 获取原生 View:
示例源码如下,请查看 pre > code 标签中的内容
//通过 webViewElementId 获取 web-view 标签的 UniElement 对象
const webViewElement = uni.getElementById(webViewElementId)
//获取原生 view
const view = webViewElement?.getIOSView();
//判断 view 是否存在,类型是否为 WKWebView
if (view != null && view instanceof WKWebView) {
//将 view 转换为 WKWebView 类型
const webView = view! as WKWebView;
}
获取到原生的WebView对象后,可以直接使用其所有属性和方法。原生对象的方法非常多,远多于 uni-app x 封装的API。这些方法都可以直接调用了。
在 hello uni-app x 的 组件 -> web-view 示例中,获取原生WebView对象,然后进一步使用了可否前进后退的方法。
组件对应原生 View 类型:
注意事项:
- iOS平台 uvue 环境使用 js 驱动无法处理原生类型,getIOSView 方法需要在 uts 插件中使用。
- iOS平台页面渲染时元素才会构建View,所以元素刚创建就获取 View 大概率是 null,推荐页面 onReady 时获取。
addEventListener<T extends UniEvent, R>(type: string, callback: (event: T) => R): UniCallbackWrapper
将指定的监听器注册到元素对象上,当该对象触发指定的事件时,指定的回调函数就会被执行。
addEventListener 兼容性
| Web | 微信小程序 | Android | iOS | iOS uni-app x UTS 插件 | HarmonyOS | HarmonyOS uni-app x UTS 插件 |
| 4.0 | x | x | x | x | x | x |
参数
| 名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
| type | string | 是 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | - | x | - | - | - |
| 事件类型 |
| callback | (event: T) => R | 是 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | - | x | - | - | - |
| 事件监听器 T表示event类型,R表示返回值类型 |
返回值
| 类型 | 描述 |
| UniCallbackWrapper | 事件回调封装类,用于注销监听函数的形参 |
参见
removeEventListener(type: string, callbackWrapper: UniCallbackWrapper): void
删除使用 addEventListener 方法添加的事件监听器。
removeEventListener 兼容性
| Web | 微信小程序 | Android | iOS | iOS uni-app x UTS 插件 | HarmonyOS | HarmonyOS uni-app x UTS 插件 |
| 4.0 | x | x | x | x | x | x |
参数
| 名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
| type | string | 是 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | - | x | - | - | - |
| 事件类型 |
| callbackWrapper | UniCallbackWrapper | 是 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | - | x | - | - | - |
| 事件监听回调封装类 |
参见
removeChild(aChild: UniElement): UniElement | null
从元素中删除一个子元素,返回删除的元素。
removeChild 兼容性
| Web | 微信小程序 | Android | iOS | iOS uni-app x UTS 插件 | HarmonyOS | HarmonyOS uni-app x UTS 插件 |
| 4.0 | x | 4.0 | 4.11 | 4.25 | 4.61 | 4.61 |
参数
| 名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
| aChild | UniElement | 是 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
|
HarmonyOS uni-app x UTS 插件
| | 4.0 | x | 4.0 | 4.11 | 4.61 | 4.61 |
| 被删除子元素对象 |
返回值
参见
remove(): void
把元素对象从它所属的 DOM 树中删除。
remove 兼容性
| Web | 微信小程序 | Android | iOS | iOS uni-app x UTS 插件 | HarmonyOS | HarmonyOS uni-app x UTS 插件 |
| 4.0 | x | 4.0 | 4.11 | 4.25 | 4.61 | 4.61 |
参见
使界面滚动到给定元素的指定坐标位置 仅scroll-view、list-view组件支持
| Web | 微信小程序 | Android | iOS | iOS uni-app x UTS 插件 | HarmonyOS | HarmonyOS uni-app x UTS 插件 |
| 4.0 | x | 4.0 | 4.11 | 4.25 | 4.61 | 4.61 |
参数
| 名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
| x | number | 是 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | - | x | - | - | - |
| x轴要滚动到坐标位置(单位px) |
| y | number | 是 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | - | x | - | - | - |
| y轴要滚动到坐标位置(单位px) |
参见
使得元素滚动一段特定距离 仅scroll-view、list-view组件支持
| Web | 微信小程序 | Android | iOS | iOS uni-app x UTS 插件 | HarmonyOS | HarmonyOS uni-app x UTS 插件 |
| 4.0 | x | 4.0 | 4.11 | 4.25 | 4.61 | 4.61 |
参数
| 名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
| x | number | 是 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | - | x | - | - | - |
| x轴要滚动的距离(单位px) |
| y | number | 是 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | - | x | - | - | - |
| y轴要滚动的距离(单位px) |
参见
querySelector(selector: string.cssSelectorString): UniElement | null
返回文档中与指定选择器或选择器组匹配的第一个 Element对象。如果找不到匹配项,则返回null
querySelector 兼容性
| Web | 微信小程序 | Android | iOS | iOS uni-app x UTS 插件 | HarmonyOS | HarmonyOS uni-app x UTS 插件 |
| 4.0 | x | 4.0 | 4.11 | 4.25 | 4.61 | 4.61 |
参数
返回值
参见
querySelectorAll(selector: string.cssSelectorString): UniElement[] | null
返回与指定的选择器组匹配的文档中的元素列表
querySelectorAll 兼容性
| Web | 微信小程序 | Android | iOS | iOS uni-app x UTS 插件 | HarmonyOS | HarmonyOS uni-app x UTS 插件 |
| 4.0 | x | 4.0 | 4.11 | 4.25 | 4.61 | 4.61 |
参数
返回值
参见
focus(): void
使元素获取焦点 仅input、Textarea组件支持
focus 兼容性
| Web | 微信小程序 | Android | iOS | iOS uni-app x UTS 插件 | HarmonyOS | HarmonyOS uni-app x UTS 插件 |
| 4.0 | x | 4.0 | 4.11 | 4.25 | 4.81 | 4.81 |
参见
blur(): void
使元素丢失焦点 仅input、Textarea组件支持
blur 兼容性
| Web | 微信小程序 | Android | iOS | iOS uni-app x UTS 插件 | HarmonyOS | HarmonyOS uni-app x UTS 插件 |
| 4.0 | x | 4.0 | 4.11 | 4.25 | 4.81 | 4.81 |
参见
takeSnapshot(options: TakeSnapshotOptions): void
对当前组件进行截图,调用此方法会将当前组件(包含子节点)渲染结果导出成图片。
成功会返回图片对应的临时文件路径,目前默认png格式
takeSnapshot 兼容性
| Web | 微信小程序 | Android | iOS | iOS uni-app x UTS 插件 | HarmonyOS | HarmonyOS uni-app x UTS 插件 |
| x | x | 3.93 | 4.11 | 4.25 | 4.61 | 4.61 |
参数
| 名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
| options | TakeSnapshotOptions | 是 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | x | x | - | - | - |
| 组件截图的参数对象 |
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 | | type | string | 否 | "file" | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | x | x | - | - | - |
| 截图导出类型,目前仅支持 'file' 保存到临时文件目录 | | format | string | 否 | "png" | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | x | x | - | - | - |
| 截图文件格式,目前仅支持 'png' | | success | (res: TakeSnapshotSuccess) => void | 否 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | x | x | - | - | - |
| 接口调用成功的回调函数 | | fail | (res: TakeSnapshotFail) => void | 否 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | x | x | - | - | - |
| 接口调用失败的回调函数 | | complete | (res: any) => void | 否 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | x | x | - | - | - |
| 接口调用结束的回调函数(调用成功、失败都会执行) |
|
TakeSnapshotSuccess 的属性值
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
| tempFilePath | string | 是 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | x | x | - | - | - |
| 截图保存的临时文件路径 |
TakeSnapshotFail 的属性值
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
| errMsg | string | 是 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | x | x | - | - | - |
| |
示例
示例为hello uni-app x alpha分支,与最新HBuilderX Alpha版同步。与最新正式版同步的master分支示例另见
该 API 不支持 Web,请运行 hello uni-app x 到 App 平台体验
扫码体验 示例源码如下,请查看 pre > code 标签中的内容
<template>
<view id="snapshot-content">
<page-head title="对本页面根view截图"></page-head>
<view class="uni-padding-wrap">
<text>this is text</text>
</view>
<button class="uni-btn btn-TakeSnapshot" type="primary" @tap="takeSnapshotClick">
点击截图并替换显示下方图片
</button>
<image style="margin-left:auto;margin-right:auto;margin-top:20px;width:90%;" :src="snapImage" :mode="mode"
@longpress="saveToAlbum"></image>
</view>
</template>
<script lang="uts">
export default {
data() {
return {
mode: "center",//aspectFit
snapImage: "/static/uni.png",
// for testing
completeTriggered: false
}
},
methods: {
takeSnapshotClick() {
const view = uni.getElementById('snapshot-content')!
view.takeSnapshot({
success: (res) => {
console.log('takeSnapshot success', res.tempFilePath)
this.snapImage = res.tempFilePath
this.mode = 'widthFix'
uni.showToast({
title: '截图成功,路径:' + res.tempFilePath,
icon: "none"
})
},
fail: (res) => {
console.log('takeSnapshot fail', res)
uni.showToast({
icon: 'error',
title: '截图失败'
})
},
complete: (res) => {
this.completeTriggered = true
console.log('takeSnapshot complete', res)
}
})
},
saveToAlbum(e : TouchEvent) {
// console.log(e.currentTarget!.getAttribute("src"));
let filePath : string = e.currentTarget!.getAttribute("src") as string
uni.showActionSheet({
itemList: ["保存"],
success: res => {
// console.log(res.tapIndex);
if (res.tapIndex == 0) {
uni.saveImageToPhotosAlbum({
filePath: filePath,
success() {
uni.showToast({
position: "center",
icon: "none",
title: "图片保存成功,请到手机相册查看"
})
},
fail(e) {
uni.showModal({
content: "保存相册失败,errCode:" + e.errCode + ",errMsg:" + e.errMsg + ",errSubject:" + e.errSubject,
showCancel: false
});
}
})
}
},
fail: () => { },
complete: () => { }
});
}
}
}
</script>
参见
完整示例代码参考hello uni-app x
- 截图会在应用沙盒目录的cache目录产生临时文件,位置详见。
- app端如需主动删除临时文件,使用uni.getFileSystemManager。
- app端list-view、web-view组件性能优化仅渲染屏幕显示的内容,所以截图仅能截取到当前屏幕展示的内容。
- app端scroll-view组件设置padding后,截图内容不会包含padding区域。
- app端由于组件背景默认透明,所以截图图片默认底色为白色。
getPage(): UniPage | null
获取元素所属的页面对象 请使用 uniPage 属性
getPage 兼容性
| Web | 微信小程序 | Android | iOS | HarmonyOS |
| 4.31 | x | 4.31 | 4.31 | - |
返回值
参见
animate(keyframes : UniAnimationKeyframe|UniAnimationKeyframe[], options : UniAnimationOption|number) : UniAnimation | null
创建一个新的动画并应用于元素,然后立即执行动画。
animate 兼容性
| Web | 微信小程序 | Android | iOS | HarmonyOS |
| √ | x | 4.51 | 4.53 | - |
参数
| 名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
| keyframes | string | 是 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | - | x | - | - | - |
| 关键帧对象数组或一个关键帧对象。 |
| 合法值 | 兼容性 | 描述 | | interface | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | - | x | - | - | - |
| - | | Array | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | - | x | - | - | - |
| - |
|
| options | UniAnimationOption | number | 是 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | - | x | - | - | - |
| 动画属性配置。 |
UniAnimationOption 的属性值
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
| delay | number | 否 | 0 | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | √ | x | 4.51 | 4.53 | - |
| 动画延迟的毫秒数 |
| direction | string | 否 | normal | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | √ | x | 4.51 | 4.53 | - |
| 动画运行方向 |
| 合法值 | 兼容性 | 描述 | | normal | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | √ | x | 4.51 | 4.53 | - |
| 正向运行 | | reverse | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | √ | x | x | x | - |
| 反向运行 | | alternate | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | √ | x | 4.51 | 4.53 | - |
| 每次迭代后切换方向 | | alternate-reverse | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | √ | x | x | x | - |
| 反向运行并在每次迭代后切换方向 |
|
| duration | number | 否 | 0 | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | √ | x | 4.51 | 4.53 | - |
| 动画时长 |
| easing | string | 否 | linear | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | √ | x | 4.51 | 4.53 | - |
| 动画曲线。 |
| 合法值 | 兼容性 | 描述 | | ease | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | - | x | - | - | - |
| - | | ease-in | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | - | x | - | - | - |
| - | | ease-out | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | - | x | - | - | - |
| - | | ease-in-out | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | - | x | - | - | - |
| - | | linear | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | - | x | - | - | - |
| - | | cubic-bezier | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | - | x | - | - | - |
| - |
|
| fill | string | 否 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | √ | x | 4.51 | 4.53 | - |
| 决定动画效果是否应在播放前反映在元素中 |
| 合法值 | 兼容性 | 描述 | | backwards | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | √ | x | x | x | - |
| 动画播放完毕后恢复初始状态 | | forwards | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | √ | x | 4.51 | 4.53 | - |
| 动画播放完毕后保留状态 | | both | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | √ | x | x | x | - |
| 动画播放完毕后保留状态 | | none | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | √ | x | x | x | - |
| 动画播放完毕后恢复初始状态 |
|
| iterations | number | 否 | 1 | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | √ | x | 4.51 | 4.53 | - |
| 动画重复的次数。当设置为Infinity时,动画将一直重复执行。 |
| 名称 | 描述 |
| width | 控制宽度属性的过渡效果 |
| height | 控制高度属性的过渡效果 |
| margin | 控制外边距属性的过渡效果 |
| marginTop | 控制上外边距属性的过渡效果 |
| marginBottom | 控制下外边距属性的过渡效果 |
| marginLeft | 控制左外边距属性的过渡效果 |
| marginRight | 控制右外边距属性的过渡效果 |
| left | 控制左侧位置属性的过渡效果 |
| right | 控制右侧位置属性的过渡效果 |
| top | 控制顶部位置属性的过渡效果 |
| bottom | 控制底部位置属性的过渡效果 |
| padding | 控制内边距属性的过渡效果 |
| paddingLeft | 控制左内边距属性的过渡效果 |
| paddingRight | 控制右内边距属性的过渡效果 |
| paddingTop | 控制上内边距属性的过渡效果 |
| paddingBottom | 控制下内边距属性的过渡效果 |
| opacity | 控制透明度属性的过渡效果 |
| backgroundColor | 控制背景颜色属性的过渡效果 |
| borderColor | 控制边框颜色属性的过渡效果 |
| borderTopColor | 控制上边框颜色属性的过渡效果 |
| borderBottomColor | 控制下边框颜色属性的过渡效果 |
| borderLeftColor | 控制左边框颜色属性的过渡效果 |
| borderRightColor | 控制右边框颜色属性的过渡效果 |
| transform | 控制变换属性的过渡效果 |
| transformOrigin | 控制元素变形的原点的过渡效果 |
| offset | 关键帧的偏移量。为0.0和1.0之间的数字。如果此值缺失,则关键帧将在相邻关键帧之间均匀分布。 |
Tips
- App平台keyframes支持的属性值不支持设置百分比。
- 微信小程序平台 keyframes 使用 wxs + transition 方案实现,不支持多个 keyframes 之间使用缺省属性。微信内置的
this.animate 方案支持 css 属性有限,可以配合使用。
| 名称 | 描述 |
| delay | 动画延迟的毫秒数 。默认值为0 |
| direction | 动画运行方向。Android/微信小程序平台仅支持alternate和normal,默认为normal |
| duration | 动画时长。默认值为0 |
| easing | 动画曲线。支持ease、ease-in、ease-out、ease-in-out、linear和贝塞尔函数。默认值为linear(微信小程序平台支持 linear) |
| fill | 决定动画效果是否应在播放前反映在元素中(Android仅支持forwards) |
| iterations | 动画应重复的次数。默认值为1。 当设置为Infinity时,动画将一直重复执行。 |
返回值
| 类型 | 必备 |
| UniAnimation | 否 |
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 | | id | string | 是 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | √ | x | 4.51 | 4.53 | - |
| 获取或设置用于标识动画的字符串 | | playState | string | 是 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | √ | x | 4.51 | 4.53 | - |
| 返回动画播放状态。可选值:running动画正在运行;paused动画暂停;finished动画播放完成;idle动画取消或者失败 |
|
UniAnimation 的方法
oncancel: (event : UniAnimationPlaybackEvent) => void
oncancel
监听动画取消事件
oncancel 兼容性
| Web | 微信小程序 | Android | iOS | HarmonyOS |
| √ | x | 4.51 | 4.53 | - |
参数
| 名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
| event | UniAnimationPlaybackEvent | 是 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | - | x | - | - | - |
| |
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 | | type | string | 是 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | - | x | - | - | - |
| 动画的事件类型 |
|
onfinish: (event : UniAnimationPlaybackEvent) => void
onfinish
监听动画完成事件
onfinish 兼容性
| Web | 微信小程序 | Android | iOS | HarmonyOS |
| √ | x | 4.51 | 4.53 | - |
参数
cancel() : void
cancel
终止并取消所有动画
cancel 兼容性
| Web | 微信小程序 | Android | iOS | HarmonyOS |
| √ | x | 4.51 | 4.53 | - |
finish() : void
finish
动画跳转到最后一毫秒并立即播放完成
finish 兼容性
| Web | 微信小程序 | Android | iOS | HarmonyOS |
| √ | x | 4.51 | 4.53 | - |
pause() : void
pause
暂停动画播放
pause 兼容性
| Web | 微信小程序 | Android | iOS | HarmonyOS |
| √ | x | 4.51 | 4.53 | - |
play() : void
play
开始或恢复动画播放
play 兼容性
| Web | 微信小程序 | Android | iOS | HarmonyOS |
| √ | x | 4.51 | 4.53 | - |
参见
getHarmonyController(): any | null
鸿蒙原生组件控制器,可以控制组件的状态。可能返回null
getHarmonyController 兼容性
| Web | 微信小程序 | Android | iOS | HarmonyOS |
| x | x | x | x | 4.61 |
返回值
参见
getHarmonyController<T>(): T | null
鸿蒙原生组件控制器,可以控制组件的状态。通过泛型定义view类型,可能返回null
getHarmonyController 兼容性
| Web | 微信小程序 | Android | iOS | HarmonyOS |
| x | x | x | x | 4.61 |
返回值
参见
requestFullscreen(options?: RequestFullscreenOptions|null) : void
控制元素进入全屏状态
requestFullscreen 兼容性
| Web | 微信小程序 | Android | iOS | HarmonyOS |
| x | x | 4.61 | 4.61 | 4.61 |
参数
| 名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
| options | RequestFullscreenOptions | 否 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | - | x | - | - | - |
| |
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 | | navigationUI | string | 否 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | - | x | - | - | - |
| 全屏模式时导航栏状态 | | 合法值 | 兼容性 | 描述 | | hide | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | - | x | - | - | - |
| 隐藏所有系统状态栏和底部导航栏 | | show | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | - | x | - | - | - |
| 隐藏顶部系统状态栏,显示底部系统导航栏 | | auto | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | - | x | - | - | - |
| 系统默认行为 |
| | orientation | string | 否 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | - | x | - | - | - |
| 全屏显示方向 | | 合法值 | 兼容性 | 描述 | | auto | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | - | x | - | - | - |
| 根据重力感应自动调整 | | landscape | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | - | x | - | - | - |
| 固定为横屏,会根据重力调整方向 | | landscape-secondary | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | - | x | - | - | - |
| 固定为反向横屏 | | landscape-primary | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | - | x | - | - | - |
| 固定为正向横屏 | | portrait | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | - | x | - | - | - |
| 固定为竖屏 |
| | success | () => void | 否 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | - | x | - | - | - |
| 成功回调 | | fail | (error: IFullscreenError) => void | 否 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | - | x | - | - | - |
| 失败回调 | | complete | (result?: any) => void | 否 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | - | x | - | - | - |
| 完成回调 |
|
IFullscreenError 的属性值
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
| errCode | number | 是 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | - | x | - | - | - |
| 错误码 |
| 合法值 | 兼容性 | 描述 | | 106600 | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | - | x | - | - | - |
| 当前页面已经有element处于全屏状态 | | 106601 | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | - | x | - | - | - |
| 当前element不支持全屏 | | 106602 | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | - | x | - | - | - |
| 当前页面没有element处于全屏状态 | | 106603 | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | - | x | - | - | - |
| 页面已销毁或者尚未就绪 | | 106604 | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | - | x | - | - | - |
| 组件未就绪 |
|
| errSubject | string | 是 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | - | x | - | - | - |
| 统一错误主题(模块)名称 |
| data | any | 否 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | - | x | - | - | - |
| 错误信息中包含的数据 |
| cause | Error | 否 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | - | x | - | - | - |
| 源错误信息,可以包含多个错误,详见SourceError |
| errMsg | string | 是 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | - | x | - | - | - |
| |
示例
示例为hello uni-app x alpha分支,与最新HBuilderX Alpha版同步。与最新正式版同步的master分支示例另见
该 API 不支持 Web,请运行 hello uni-app x 到 App 平台体验
扫码体验 示例源码如下,请查看 pre > code 标签中的内容
<template>
<scroll-view class="content" direction="vertical">
<view id="fullscreen" class="view1" @click="fullscreen" @fullscreenchange="fullscreenchange" @fullscreenerror="fullscreenerror">
<text style="color: white;">{{ text }}</text>
</view>
<enum-data :items="orientation_enum" title="orientation" @change="radio_change_orientation"></enum-data>
<enum-data :items="navigationUI_enum" title="navigationUI" @change="radio_change_ui"></enum-data>
</scroll-view>
</template>
<script>
import { ItemType } from '@/components/enum-data/enum-data-types';
export default {
data() {
return {
orientation_enum: [{ "value": 0, "name": "auto" }, { "value": 1, "name": "landscape" }, { "value": 2, "name": "landscape-primary" }, { "value": 3, "name": "landscape-secondary" }, { "value": 4, "name": "portrait" }] as ItemType[],
navigationUI_enum: [{ "value": 0, "name": "auto" }, { "value": 1, "name": "hide" }, { "value": 2, "name": "show" }] as ItemType[],
text: "点击进入全屏",
fullscreenElement: null as UniElement | null,
isFullscreen: false,
orientation: "landscape",
navigationUI: "hide",
fullscreenchangeCount: 0
}
},
onReady() {
this.fullscreenElement = uni.getElementById('fullscreen') as UniElement
},
methods: {
getCurrentPage() : UniPage {
const pages = getCurrentPages()
return pages[pages.length - 1]
},
fullscreen() {
if (this.isFullscreen) {
const page = this.getCurrentPage()
page.exitFullscreen({
success: () => {
this.text = "点击进入全屏"
},
fail: (err) => {
console.log('fail', err)
},
complete: () => {
console.log('complete')
}
})
} else {
this.fullscreenElement?.requestFullscreen({
navigationUI: this.navigationUI,
orientation: this.orientation,
success: () => {
this.text = "点击退出全屏"
},
fail: (err) => {
console.log('fail', err)
},
complete: () => {
console.log('complete')
}
})
}
this.isFullscreen = !this.isFullscreen
},
fullscreenchange(e : UniEvent) {
console.log(e.type)
this.fullscreenchangeCount++
console.log(this.fullscreenchangeCount)
},
fullscreenerror(e : UniEvent) {
console.log(e.type)
},
radio_change_orientation(checked : number) {
console.log(checked)
this.orientation = this.orientation_enum[checked]['name'] as string
},
radio_change_ui(checked : number) {
console.log(checked)
this.navigationUI = this.navigationUI_enum[checked]['name'] as string
}
}
}
</script>
<style>
.content {
flex: 1;
background-color: #f0f0f0;
}
.view1 {
width: 100%;
height: 150px;
align-items: center;
justify-content: center;
background-color: black;
}
</style>
参见
Tips
- requestFullscreen仅支持
view组件。其他组件调用会触发失败回调。 - app-ios平台,横屏时系统会自动隐藏状态栏。