cssUnit

CSS 单位

Updated time: 14/06/2017

CSS color 单位

支持以下写法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
.classA {
/* 3-chars hex */
color: #0f0;
/* 6-chars hex */
color: #00ff00;
/* rgba */
color: rgb(255, 0, 0);
/* rgba */
color: rgba(255, 0, 0, 0.5);
/* transparent */
color: transparent;
/* Basic color keywords */
color: orange;
/* Extended color keywords */
color: darkgray;
}

注意

  • 不支持 hsl(), hsla(), currentColor, 8个字符的十六进制颜色。

  • rgb(a,b,c)rgba(a,b,c,d) 的性能比其他颜色格式差很多,请选择合适的颜色格式。

颜色名称可查看 颜色名称列表.

CSS length 单位

在 Weex 中,我们只支持 px 长度单位。并且它将在 JavaScript 运行时和本机渲染器中解析为数字类型。

下面这些不同的写法,解析的结果完全相同。

1
.classA { font-size: 48px; line-height: 64px; }

不支持类似 emrempt 这样的 CSS 标准中的其他长度单位。

CSS number 单位

仅仅一个数字。用于 opacitylines等。

有时值必须是整数,例如:lines

CSS percentage 单位 (暂不支持)

表示百分比值,如“50%”,“66.7%”等。

它是 CSS 标准的一部分,但 Weex 暂不支持。

globalEvent

globalEvent 0.8

Updated time: 14/06/2017

globalEvent 用于监听持久性事件,例如定位信息,陀螺仪等的变化。全局事件是需要额外 APIs 处理的次要 API。你能通过 addEventListener 注册事件监听,当你不再需要的时候,也可以通过 removeEventListener 取消事件监听。

提醒

  • 这是一个实例级别的事件,而非应用级别。

如何让你的模块支持全局事件

API 开发完成后,当需要发送事件时,需要通过以下方法:

1
2
3
4
5
6
/**
*
* @param eventName eventName
* @param params event params
*/
instance.fireGlobalEventCallback(eventName,params);

如何在 weex-html5 组件或模块中分发全局事件?只需在文档元素上分派事件:

1
2
3
var evt = new Event('some-type')
evt.data = { foo: 'bar' }
document.dispatchEvent(evt)

示例

Android
1
2
3
Map<String,Object> params=new HashMap<>();
params.put("key","value");
mWXSDKInstance.fireGlobalEventCallback("geolocation",params);
iOS
1
[weexInstance fireGlobalEvent:@"geolocation" params:@{@"key":@"value"}];

API

addEventListener(String eventName, String callback)

注册全局事件。

参数
  • eventName {string}:需要监听的事件名称。
  • callback {Function}:触发事件后的回调函数。
示例
1
2
3
4
var globalEvent = weex.requireModule('globalEvent');
globalEvent.addEventListener("geolocation", function (e) {
console.log("get geolocation")
});

removeEventListener(String eventName)

取消事件监听。

参数
  • eventName {string}:需要取消的事件名称。
示例
1
2
var globalEvent = weex.requireModule('globalEvent');
globalEvent.removeEventListener("geolocation");

webview

webview

Updated time: 14/06/2017

一系列的 <web> 组件操作接口。 比如 goBackgoForward、和 reloadwebview module 与 <web> 组件共用。

示例

查看 简单浏览器 ,一个结合 <web> 组件和 webview module 的示例。

API

goBack(webElement)

加载历史记录里的前一个资源地址。

参数
  • webElement {Element}<web> 组件对象。

goForward(webElement)

加载历史记录里的下一个资源地址。

参数
  • webElement {Element}<web> 组件对象。

reload(webElement)

刷新当前页面。

参数
  • webElement {Element}<web> 组件对象。
    注意事项:未来 <web> 组件的 Element 对象将会支持直接这些方法,届时 webview module 将不再需要

Example

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
<template>
<div class="wrapper">
<div class="group">
<input class="input" v-model="value" ref="input" type="url" autofocus="false"></input>
</div>
<div class="group">
<text class="button" @click="loadURL">LoadURL</text>
<text class="button" @click="reload">reload</text>
</div>
<web ref="webview" :src="url" class="webview" @pagestart="start" @pagefinish="finish" @error="error"></web>
</div>
</template>
<script>
const webview = weex.requireModule('webview')
const modal = weex.requireModule('modal')
export default {
data () {
return {
url : 'https://m.alibaba.com',
value: 'https://m.alibaba.com'
}
},
methods: {
loadURL (event) {
this.url = this.value
modal.toast({ message: 'load url:' + this.url })
setTimeout(() => {
console.log('will go back.')
modal.toast({ message: 'will go back' })
webview.goBack(this.$refs.webview)
}, 10000)
},
reload (event) {
console.log('will reload webview')
modal.toast({ message: 'reload' })
webview.reload(this.$refs.webview)
},
start (event) {
console.log('pagestart', event)
modal.toast({ message: 'pagestart' })
},
finish (event) {
console.log('pagefinish', event)
modal.toast({ message: 'pagefinish' })
},
error (event) {
console.log('error', event)
modal.toast({ message: 'error' })
}
}
}
</script>
<style scoped>
.group {
flex-direction: row;
justify-content: space-around;
margin-top: 20px;
}
.input {
width: 600px;
font-size: 36px;
padding-top: 15px;
padding-bottom: 15px;
border-width: 2px;
border-style: solid;
border-color: #BBBBBB;
}
.button {
width: 225px;
text-align: center;
background-color: #D3D3D3;
padding-top: 15px;
padding-bottom: 15px;
margin-bottom: 30px;
font-size: 30px;
}
.webview {
margin-left: 75px;
width: 600px;
height: 750px;
border-width: 2px;
border-style: solid;
border-color: #41B883;
}
</style>

try it

stream

stream

Updated time: 14/06/2017

概述

以下为 stream 相关的 API,用于实现网络请求。

API

fetch(options, callback[,progressCallback])

发起网络请求

参数
  • options {Object}:请求的一些选项

    • method {string}:HTTP 方法 GET 或是 POST
    • url {string}:请求的 URL
    • headers {Object}:HTTP 请求头
    • type {string}:响应类型, json, text 或是 jsonp {在原生实现中其实与 json 相同)
    • body {string}:HTTP 请求体。
      注意:
      • body 参数仅支持 string 类型的参数,请勿直接传递 JSON,必须先将其转为字符串。
      • GET 请求不支持 body 方式传递参数,请使用 url 传参。
  • callback {Function}:响应结果回调,回调函数将收到如下的 response 对象:

    • status {number}:返回的状态码
    • ok {boolean}:如果状态码在 200~299 之间就为真。
    • statusText {string}:状态描述文本
    • data {Object | string}: 返回的数据,如果请求类型是 jsonjsonp,则它就是一个 object ,如果不是,则它就是一个 string。
    • headers {Object}:响应头
  • progressCallback {Function}:关于请求状态的回调。 这个回调函数将在请求完成后就被调用:

    • readyState {number}:当前状态
      state:’1’: 请求连接中
      opened:’2’: 返回响应头中
      received:’3’: 正在加载返回数据
    • status {number}:响应状态码.
    • length {number}:已经接受到的数据长度. 你可以从响应头中获取总长度
    • statusText {string}:状态文本
    • headers {Object}:响应头

注意

  • 默认 Content-Type 是 ‘application/x-www-form-urlencoded’。
  • 如果你需要通过 POST json , 你需要将 Content-Type 设为 ‘application/json’。

Example

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
<template>
<div class="wrapper">
<div class="group">
<text class="title">Weex Star :</text>
<text class="count">{{weexStar}}</text>
</div>
<div class="group">
<text class="title">Vue Star :</text>
<text class="count">{{vueStar}}</text>
</div>
</div>
</template>
<script>
var stream = weex.requireModule('stream')
export default {
data () {
return {
weexStar: 'unknown',
vueStar: 'unknown'
}
},
methods: {
getStarCount (repo, callback) {
return stream.fetch({
method: 'GET',
type: 'json',
url: 'https://api.github.com/repos/' + repo
}, callback)
}
},
created () {
this.getStarCount('alibaba/weex', res => {
this.weexStar = res.ok ? res.data.stargazers_count : '(network error)'
})
this.getStarCount('vuejs/vue', res => {
this.vueStar = res.ok ? res.data.stargazers_count : '(network error)'
})
}
}
</script>
<style scoped>
.wrapper {
flex-direction: column;
justify-content: center;
}
.group {
flex-direction: row;
justify-content: center;
margin-bottom: 40px;
}
.title {
font-size: 45px;
color: #888888;
}
.count {
font-size: 45px;
font-weight: bold;
margin-left: 12px;
color: #41B883;
}
</style>

try it

storage

storage v0.7+

Updated time: 14/06/2017

备注:0.7及以上版本可用

storage 是一个在前端比较常用的模块,可以对本地数据进行存储、修改、删除,并且该数据是永久保存的,除非手动清除或者代码清除。但是,storage 模块有一个限制就是浏览器端(H5)只能存储小于5M的数据,因为在 H5/Web 端的实现是采用 HTML5 LocalStorage API。而 Android 和 iOS 这块是没什么限制的。
storage 常用在一些被用户经常查询,但是又不频繁更新的数据,比如搜索历史、用户的订单列表等。搜索历史一般情况都是作为本地数据存储的,因此使用 storage 比较合适。而用户订单列表是需要本地存储和服务端器检索配合的场景。当一个用户下单后,会经常查阅个人的订单列表。但是,订单的列表数据不是频繁更新的,往往只有在收到货品时,才更新“已签收”,其余平时的状态是“已发货”。因此,可以使用 storage 存储订单列表,可以减少服务器的压力,例如减少 SQL 查询或者缓存的压力。当用户查看订单详情的时候,再更新数据状态。

API

storage 提供了一系列的 API 供我们调用。我们只需要引入该模块,然后调用对应的 API 即可。

setItem(key, value, callback)

该方法可以通过键值对的形式将数据存储到本地。同时可以通过该方法,更新已有的数据。

参数
  • key {string}:要存储的键,不允许是 ""null
  • value {string}:要存储的值,不允许是 ""null
  • callback {function (e)}:执行操作成功后的回调
    • e.result:表示设置是否成功,如果成功返回 "success"
    • e.dataundefined 表示设置成功,invalid_param 表示 key/value"" 或者 null

这里,对返回值做一个简单的介绍:

e 包含两个属性:e.resulte.data。如果 e.result 返回值是 “success”,则说明成功。e.data 返回 undefined 表示设置成功,返回 invalid_param 表示key/value 为 “” 或者 null。因此,你可以判断两个返回判断是否插入成功。

getItem(key, callback)

传入键名返回对应的键值

参数
  • key {string}:要获取的值的名称,不允许是 ""null
  • callback {function (e)}:执行操作成功后的回调
    • e.result:表示设置是否成功,如果成功返回 "success"
    • e.data:获取对应的键值字符串,如果没有找到则返回 undefined

removeItem(key, callback)

传入一个键名将会删除本地存储中对应的键值

参数
  • key {string}:要删除的值的名称,不允许是 ""null
  • callback {function (e)}:执行操作成功后的回调.
    • e.result:表示删除是否成功,如果成功返回 "success"
    • e.dataundefined 表示删除成功

length(callback)

返回本地存储的数据中所有存储项数量的整数

参数
  • callback {function (e)}:执行操作成功后的回调
    • e.result:表示设置是否成功,如果成功返回 "success"
    • e.data:当前已存储项的数量

getAllKeys(callback)

返回一个包含全部已存储项键名的数组

参数
  • callback {function (e)}:执行操作成功后的回调。
    • e.result:表示设置是否成功,如果成功返回 "success"
    • e.data:所有键名组成的数组

示例

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
<template>
<div class="list">
<div class="group center">
<div class="panel"><text class="text">{{state}}</text></div>
</div>
<div class="group">
<div class="panel"><text class="text" @click="setItem">set</text></div>
<div class="panel"><text class="text" @click="getItem">get</text></div>
<div class="panel"><text class="text" @click="removeItem">remove</text></div>
<div class="panel"><text class="text" @click="getAll">all</text></div>
</div>
</div>
</template>
<script>
const storage = weex.requireModule('storage')
const modal = weex.requireModule('modal')
export default {
data () {
return {
keys: '[]',
length: 0,
state: '----'
}
},
methods: {
setItem () {
storage.setItem('name', 'Hanks', event => {
this.state = 'set success'
console.log('set success')
})
},
getItem () {
storage.getItem('name', event => {
console.log('get value:', event.data)
this.state = 'value: ' + event.data
})
},
removeItem () {
storage.removeItem('name', event => {
console.log('delete value:', event.data)
this.state = 'deleted'
})
},
getAll () {
storage.getAllKeys(event => {
// modal.toast({ message: event.result })
if (event.result === 'success') {
modal.toast({
message: 'props: ' + event.data.join(', ')
})
}
})
}
}
}
</script>
<style scoped>
.panel {
height: 100px;
flex-direction: column;
justify-content: center;
border-width: 2px;
border-style: solid;
border-color: rgb(162, 217, 192);
background-color: rgba(162, 217, 192, 0.2);
}
.group {
flex-direction: row;
justify-content: space-between;
width: 650px;
margin-left: 50px;
margin-top: 50px;
margin-bottom: 50px;
}
.center {
justify-content: center;
}
.text {
font-size: 50px;
text-align: center;
padding-left: 25px;
padding-right: 25px;
color: #41B883;
}
.small {
font-size: 32px;
padding-left: 35px;
padding-right: 35px;
color: #41B883;
}
</style>

try it

其它参考

navigator

Updated time: 14/06/2017

众所周知,在浏览器里,我们可以通过前进或者回退按钮来切换页面,iOS/Android 的 navigator 模块就是用来实现类似的效果的。除了前进、回退功能,该模块还允许我们指定在切换页面的时候是否应用动画效果。

API

push(options, callback)

把一个weex页面URL压入导航堆栈中,可指定在页面跳转时是否需要动画,以及操作完成后需要执行的回调函数

参数
  • options {Object}:选项参数
    • url {stirng}:要压入的 Weex 页面的 URL
    • animated {string}"true" 示意为页面压入时需要动画效果,"false" 则不需要,默认值为 "true"
  • callback {Function}:执行完该操作后的回调函数

pop(options, callback)

把一个 Weex 页面 URL 弹出导航堆栈中,可指定在页面弹出时是否需要动画,以及操作完成后需要执行的回调函数。

参数
  • options {object}:选项参数对象
    • animated {string}"true" 示意为弹出页面时需要动画效果,"false" 则不需要,默认值为 "true"
  • callback {function}:执行完该操作后的回调函数

注意事项:animated 二级参数目前仅支持字符串的 "true""false",传入布尔值类型会导致程序崩溃,未来版本会修复这个问题

Example

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
<template>
<div class="wrapper">
<text class="button" @click="jump">Jump</text>
</div>
</template>
<script>
var navigator = weex.requireModule('navigator')
var modal = weex.requireModule('modal')
export default {
methods: {
jump (event) {
console.log('will jump')
navigator.push({
url: 'http://dotwe.org/raw/dist/519962541fcf6acd911986357ad9c2ed.js',
animated: "true"
}, event => {
modal.toast({ message: 'callback: ' + event })
})
}
}
};
</script>
<style scoped>
.wrapper {
flex-direction: column;
justify-content: center;
}
.button {
font-size: 60px;
width: 450px;
text-align: center;
margin-top: 30px;
margin-left: 150px;
padding-top: 20px;
padding-bottom: 20px;
border-width: 2px;
border-style: solid;
color: #666666;
border-color: #DDDDDD;
background-color: #F5F5F5
}
</style>

try it

modal

Updated time: 14/06/2017

modal 模块提供了以下展示消息框的 API:toastalertconfirmprompt

API

toast(options)

toast() 会在一个小浮层里展示关于某个操作的简单反馈。例如,在邮件发送前离开邮编编辑界面,可以触发一个“草稿已保存”的 toast,告知用户以后可以继续编辑。toast 会在显示一段时间之后自动消失。

参数
  • options {Object}:相关选项
    • message {string}:展示的内容
    • duration {number}:展示的持续时间(以秒为单位)
      • Android: 如果时间长度大于3s,将使用一个被称为LONG的系统变量, 否则使用SHORT这个系统变量
      • iOS: 持续的时间同Duration相同。

alert(options, callback)

警告框经常用于确保用户可以得到某些信息。当警告框出现后,用户需要点击确定按钮才能继续进行操作。

参数
  • options {Object}:alert选项
    • message {string}:警告框内显示的文字信息
    • okTitle {string}:确定按钮上显示的文字信息,默认是“OK”
    • callback {Function}:用户操作完成后的回调

confirm(options, callback)

确认框用于使用户可以验证或者接受某些信息。当确认框出现后,用户需要点击确定或者取消按钮才能继续进行操作。

参数
  • options {object}:confirm 选项
    • message {string}:确认框内显示的文字信息
    • okTitle {string}:确认按钮上显示的文字信息,默认是 OK
    • cancelTitle {string}:取消按钮上显示的文字信息,默认是 Cancel
  • callback {function (result)}:用户操作完成后的回调,回调函数的参数 result 是确定按钮上的文字信息字符串

prompt(options, callback)

提示框经常用于提示用户在进入页面前输入某个值。当提示框出现后,用户需要输入某个值,然后点击确认或取消按钮才能继续操作。

参数
  • options {object}:prompt 选项
    • message {string}:提示框内要显示的文字信息
    • okTitle {string}:确认按钮上显示的文字信息,默认是 OK
    • cancelTitle {string}:取消按钮上显示的文字信息,默认是 Cancel
  • callback {function (ret)}:用户操作完成后的回调,回调函数的参数 ret 格式形如 { result: 'OK', data: 'hello world' },如下
    • result {string}:用户按下的按钮上的文字信息
    • data {string}:用户输入的文字信息

Example

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
<template>
<div class="wrapper">
<text class="button" @click="showToast">Toast</text>
<text class="button" @click="showAlert">Alert</text>
<text class="button" @click="showConfirm">Confirm</text>
<text class="button" @click="showPrompt">Prompt</text>
</div>
</template>
<script>
var modal = weex.requireModule('modal')
export default {
methods: {
showToast (event) {
console.log('will show toast')
modal.toast({
message: 'This is a toast',
duration: 0.3
})
},
showAlert (event) {
console.log('will show alert')
modal.alert({
message: 'This is a alert',
duration: 0.3
}, function (value) {
console.log('alert callback', value)
})
},
showConfirm (event) {
console.log('will show confirm')
modal.confirm({
message: 'Do you confirm ?',
duration: 0.3
}, function (value) {
console.log('confirm callback', value)
})
},
showPrompt (event) {
console.log('will show prompt')
modal.prompt({
message: 'This is a prompt',
duration: 0.3
}, function (value) {
console.log('prompt callback', value)
})
}
}
};
</script>
<style scoped>
.wrapper {
flex-direction: column;
justify-content: center;
}
.button {
font-size: 60px;
width: 450px;
text-align: center;
margin-top: 30px;
margin-left: 150px;
padding-top: 20px;
padding-bottom: 20px;
border-width: 2px;
border-style: solid;
color: #666666;
border-color: #DDDDDD;
background-color: #F5F5F5
}
</style>

try it

dom

dom

Updated time: 12/09/2017

包含如下可以更新 dom 树的 dom API。

这部分API是通过把 virtual-dom 的消息发送到 native 渲染器来做到的。

开发者在日常开发中,唯一可在 .vue 文件中使用的是 scrollToElement
你也可以调用 $scrollTo 方法来使用它

这个页面提及的其他的 API,只在 callNative 进程中的 native 渲染器用。
(关于 callNative 进程的进一步介绍,可以在 How it works中的 JS Framework 部分看到 )

API

scrollToElement(node, options)

让页面滚动到那个对应的节点,这个API只能在 <scroller><list> 组件中用。

这个API也能通过调用VM的方法 $scrollTo 来使用(已弃用)

要在你的 .vue 文件中使用这个 API,可以使用 weex.requireModule('dom').scrollToElement

参数
  • node {Node}:你要滚动到的那个节点
  • options {Object}:如下选项
  • offset {number}:一个到其可见位置的偏移距离,默认是 0
  • animated {boolean} 0.10+:是否需要附带滚动动画,默认是true
示例
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
<template>
<div class="wrapper">
<scroller class="scroller">
<div class="row" v-for="(name, index) in rows" :ref="'item'+index">
<text class="text" :ref="'text'+index">{{name}}</text>
</div>
</scroller>
<div class="group">
<text @click="goto10" class="button">Go to 10</text>
<text @click="goto20" class="button">Go to 20</text>
</div>
</div>
</template>
<script>
const dom = weex.requireModule('dom')
export default {
data () {
return {
rows: []
}
},
created () {
for (let i = 0; i < 30; i++) {
this.rows.push('row ' + i)
}
},
methods: {
goto10 (count) {
const el = this.$refs.item10[0]
dom.scrollToElement(el, {})
},
goto20 (count) {
const el = this.$refs.item20[0]
dom.scrollToElement(el, { offset: 0 })
}
}
}
</script>
<style scoped>
.scroller {
width: 700px;
height: 700px;
border-width: 3px;
border-style: solid;
border-color: rgb(162, 217, 192);
margin-left: 25px;
}
.row {
height: 100px;
flex-direction: column;
justify-content: center;
padding-left: 30px;
border-bottom-width: 2px;
border-bottom-style: solid;
border-bottom-color: #DDDDDD;
}
.text {
font-size: 45px;
color: #666666;
}
.group {
flex-direction: row;
/*justify-content: space-around;*/
justify-content: center;
margin-top: 60px;
}
.button {
width: 200px;
padding-top: 20px;
padding-bottom: 20px;
font-size: 40px;
margin-left: 30px;
margin-right: 30px;
text-align: center;
color: #41B883;
border-width: 2px;
border-style: solid;
border-color: rgb(162, 217, 192);
background-color: rgba(162, 217, 192, 0.2);
}
</style>

try it

getComponentRect(ref, callback) v0.9.4+

通过标签的 ref 获得其布局信息,返回的信息在 callBack 中,格式参考如下:

1
2
3
4
5
6
7
8
9
10
11
{
result: true,
size: {
bottom: 60,
height: 15,
left: 0,
right: 353,
top: 45,
width: 353
}
}

如果想要获取到 Weex 容器的布局信息,可以指定 ref='viewport',调用例子如下:

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
<template>
<div class="wrapper" style='margin-top:200px'>
<div ref="box" class="box">
<text class="info">Width: {{size.width}}</text>
<text class="info">Height: {{size.height}}</text>
<text class="info">Top: {{size.top}}</text>
<text class="info">Bottom: {{size.bottom}}</text>
<text class="info">Left: {{size.left}}</text>
<text class="info">Right: {{size.right}}</text>
</div>

<text class="info btn" @click='click()'>{{this.tip}}</text>

</div>
</template>
<script>
const dom = weex.requireModule('dom')

function round(size) {
var roundSize = {
'width': Math.round(size.width),
'height': Math.round(size.height),
'top': Math.round(size.top),
'bottom': Math.round(size.bottom),
'left': Math.round(size.left),
'right': Math.round(size.right)
}
return roundSize
}
export default {
data () {
return {
size: {
width: 0,
height: 0,
top: 0,
bottom: 0,
left: 0,
right: 0
},
ref:"viewport",
tip:"get box rect"
}
},
mounted () {
const result = dom.getComponentRect(this.ref, option => {
console.log('getComponentRect:', option)
this.size = round.call(this,option.size);
})
},

methods:{
click:function() {
if (this.ref === 'viewport') {
this.ref = this.$refs.box;
this.tip = "get viewport rect"
} else {
this.ref = 'viewport'
this.tip = "get box rect"
}

const result = dom.getComponentRect(this.ref, option => {
console.log('getComponentRect:', option)
this.size = round.call(this,option.size);
})
}
}

}
</script>
<style scoped>
.btn {
margin-top:20px;
border-width:2px;
border-style: solid;
border-radius:10px;
width:300px;
margin-left:170px;
padding-left:35px;
border-color: rgb(162, 217, 192);

}
.btn:active {
background-color: #8fbc8f;
22border-color: gray;
}

.box {
align-items:center;
margin-left: 150px;
width: 350px;
height: 400px;
background-color: #DDD;
border-width: 2px;
border-style: solid;
border-color: rgb(162, 217, 192);
background-color: rgba(162, 217, 192, 0.2);
}
.info {
font-size: 40px;
top:30px;
margin-left:20px;
font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace;
color: #41B883;
}
</style>

try it

addRule

支持版本:v0.12.0

addRule是可以为dom 添加一条规则,目前支持自定义字体fontFace规则,构建自定义的font-family,可以在text使用

fontFace
1
2
3
4
5
var domModule = weex.requireModule('dom');
domModule.addRule('fontFace', {
'fontFamily': "iconfont2",
'src': "url('http://at.alicdn.com/t/font_1469606063_76593.ttf')"
});

try it

其他

dom 还有一些底层接口用于创建 Weex 实例时调用,比如 createBodyupdateAttrs 等,但并未开放供外部使用。

cipboard

clipboard v0.8+

Updated time: 14/06/2017

我们可以通过 clipboard 模块的 getString()setString() 接口从系统的粘贴板获取内容或者设置内容。

以前当我们收到一条短信验证码信息时,除了人肉拷贝,我们无法获取拷贝短信的内容。这是非常苦恼的。但是现在我们可以通过简单的调用 clipboard.getString() 接口来获取短信内容了。

注意

  • 仅支持文本拷贝
  • 出于安全考虑和平台限制,只支持 Android 和 iOS,不支持 html5。

API

getString(callback)

从系统粘贴板读取内容。

参数
  • callback {function (ret)}:执行完读取操作后的回调函数。ret {Object}callback 函数的参数,有两个属性:
    • ret.data:获取到的文本内容;
    • ret.result:返回状态,可能为 successfail

setString(text)

将一段文本复制到剪切板,相当于手动复制文本。

参数
  • text {string}:要复制到剪切板的字符串。

Example

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
<template>
<div>
<div class="div">
<text class="text" @click="onItemClick">{{message}}</text>
</div>
<div class="div">
<text class="text" @click="setContent">Click to copy: {{tobecopied}}</text>
</div>
</div>
</template>
<script>
const clipboard = weex.requireModule('clipboard')
export default {
data () {
return {
tobecopied: 'yay!',
message: 'nothing.'
}
},
methods: {
setContent () {
clipboard.setString(this.tobecopied)
},
onItemClick () {
this.message = 'clicked! '
clipboard.getString(ret => {
this.message = 'text from clipboard:' + ret.data
})
}
}
}
</script>
<style scoped>
.div {
flex-direction: row;
justify-content: space-between;
align-items: center;
width: 750px;
height: 90px;
padding-left: 30px;
padding-right: 30px;
border-bottom-width: 1px;
border-style: solid;
border-color: #DDDDDD;
}
.text {
width: 750px;
height: 90px;
}
</style>

try it

meta

meta 0.10.0+

Updated time: 01/08/2017

meta 模块可用于声明单个页面的元信息,通常是一些页面的配置,如容器的显示宽度 (viewport) 等。

API

setViewport(options)

Weex 容器默认的宽度 (viewport) 是 750px,通过 setViewport 方法可以改变页面的显示宽度,仅对当前页面生效。

需要注意的是:只有在页面渲染开始之前设置 viewport 才会生效。 也就是说,setViewport 方法只能在入口文件中使用,而且要在 new Vue(...)之前调用;如果是在组件中使用,就只有在渲染到该组件的时候才会执行相应的代码,此时页面已经处于渲染过程中,设置 viewport 将不会再生效。

参数

参数配置借鉴了 W3C 标准中的 CSS Device Adaptation,目前支持如下属性:

  • options: viewport 的配置项
    • width: 数值,或者 "device-width""device-height" 之一。
    • height: 数值,或者 "device-width""device-height" 之一。
      宽度和高度的单位默认是 px,暂不支持其他单位。

例子

入口文件:

1
2
3
4
5
6
7
8
9
// entry.js
import App from './app.vue'
const meta = weex.requireModule('meta')
// 配置 viewport 的宽度为 640px
meta.setViewport({
width: 640
})
App.el = '#root'
new Vue(App)

在入口文件中配置了 viewport 的宽度为 640 之后,当前页面中的所有组件都会以 640px 作为满屏宽度。

组件文件:

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
<!-- app.vue -->
<template>
<div>
<div class="box750">
<text class="text">750</text>
<div class="box640">
<text class="text">640</text>
<div class="box480">
<text class="text">480</text>
</div>
</div>
</div>
</div>
</template>
<style scoped>
.box750 {
width: 750px;
height: 750px;
background-color: #EEEEEE;
}
.box640 {
width: 640px;
height: 640px;
background-color: #CCCCCC;
}
.box480 {
width: 480px;
height: 480px;
background-color: #AAAAAA;
}
.text {
font-size: 50px;
}
</style>

试试看。(由于 http://dotwe.org 目前还不支持配置入口文件,例子中的效果暂时无法在线查看。)

本地开发环境的搭建可以参考:《搭建开发环境》