common utils of mobile html5 development
npm i mobile-utils --save
//in requirejs config:
require.config({
paths: {
"mobile-utils": "YOUR_PATH/mobile-utils.min",
...
},
...
});
//in page
import mUtils from 'mobile-utils';
...
mUtils.format.limit_decimal(someNum)
the entry point
Kind: global namespace
-
mUtils :
object
-
.dom :
object
-
.has_class(ele, clsName) ⇒
Boolean
-
.remove_class(ele, clsName) ⇒
void
-
.add_class(ele, clsName) ⇒
void
-
.real_style([ele], [styleName]) ⇒
String
-
.append_HTML(dom, html) ⇒
void
-
.prepend_HTML(dom, html) ⇒
void
-
.tag_range_from_HTML(p_featureStr, p_html) ⇒
Object
-
.transformXY(dom, x, y, [duration]) ⇒
void
-
.has_class(ele, clsName) ⇒
-
.env :
object
-
.event :
object
-
.on_tweened(dom, callback) ⇒
void
-
.on_page_loaded(callback) ⇒
void
-
.on_page_rotated(callback) ⇒
void
-
.fake_click(callback, [domId]) ⇒
void
-
.listen_select_change(sel, callback) ⇒
void
-
.is_event_on_target(targetDom) ⇒
Boolean
-
.on_tweened(dom, callback) ⇒
-
.format :
object
-
.reverse_str(str) ⇒
String
-
.step_str(target, [step], [needReverse], [div]) ⇒
String
-
.knot_num(num, [fix]) ⇒
String
-
.limit_decimal(num, [fix]) ⇒
String
-
.num_pad_left(num, [leng]) ⇒
String
-
.num_limit_leng(value, leng) ⇒
Number
-
.trim(str) ⇒
String
-
.reverse_str(str) ⇒
-
.lang :
object
-
.read_i18n(tmpl, ...args) ⇒
String
-
.read_i18n(tmpl, ...args) ⇒
-
.time :
object
-
.date_to_YMD(date, [div]) ⇒
String
-
.getTime([setting]) ⇒
Date
-
.today([zeroTime]) ⇒
Date
-
.tomorrow([zeroTime]) ⇒
Date
-
.yesterday([zeroTime]) ⇒
Date
-
.get_clean_date([p_month], [p_year]) ⇒
Date
-
.calender(p_year, [p_month]) ⇒
Array
-
.date_range(rangeNums, [from]) ⇒
Object
-
.is_leap_year([year]) ⇒
Boolean
-
.date_to_YMD(date, [div]) ⇒
-
.url :
object
-
.URLHash
- new URLHash([href], [hashChar], [separator])
-
.get(key) ⇒
String
-
.put(key, value) ⇒
void
-
.putAll(keyValues) ⇒
void
-
.remove(key) ⇒
void
-
.toString() ⇒
String
-
.clone() ⇒
URLHash
-
.query_params([query]) ⇒
Object
-
.params_query(obj, [separator]) ⇒
String
-
.URLHash
-
.utils :
object
-
.dom :
Kind: static namespace of mUtils
-
.dom :
object
-
.has_class(ele, clsName) ⇒
Boolean
-
.remove_class(ele, clsName) ⇒
void
-
.add_class(ele, clsName) ⇒
void
-
.real_style([ele], [styleName]) ⇒
String
-
.append_HTML(dom, html) ⇒
void
-
.prepend_HTML(dom, html) ⇒
void
-
.tag_range_from_HTML(p_featureStr, p_html) ⇒
Object
-
.transformXY(dom, x, y, [duration]) ⇒
void
-
.has_class(ele, clsName) ⇒
判断元素是否有指定className
Kind: static method of dom
Param | Type |
---|---|
ele | Element |
clsName | String |
删除className
Kind: static method of dom
Param | Type |
---|---|
ele | Element |
clsName | String |
添加className
Kind: static method of dom
Param | Type |
---|---|
ele | Element |
clsName | String |
获取计算样式
Kind: static method of dom
Param | Type | Default | Description |
---|---|---|---|
[ele] | Element |
|
目标元素 |
[styleName] | String |
|
样式名 |
在指定元素内的最末插入html
Kind: static method of dom
Param | Type |
---|---|
dom | Element |
html | String |
在指定元素内的开始插入html
Kind: static method of dom
Param | Type |
---|---|
dom | Element |
html | String |
根据特征值找到一段html中其所在的tag范围
Kind: static method of dom
Returns: Object
- {start, end}
Param | Type | Description |
---|---|---|
p_featureStr | String |
特征值 |
p_html | String |
作为查找范围的html |
为元素附加2d移动的css3样式
Kind: static method of dom
Param | Type | Default | Description |
---|---|---|---|
dom | Element |
目标元素 | |
x | Number |
x轴上移动的px值 | |
y | Number |
y轴上移动的px值 | |
[duration] | Number |
|
持续的毫秒数 |
获取移动设备型号版本等环境信息
Kind: static namespace of mUtils
Default: {
ua,
version,
ios,
android,
windows,
blackberry,
weixin,
wVersion,
qq,
qVersion,
touchSupport,
hashSupport
}
Kind: static namespace of mUtils
-
.event :
object
-
.on_tweened(dom, callback) ⇒
void
-
.on_page_loaded(callback) ⇒
void
-
.on_page_rotated(callback) ⇒
void
-
.fake_click(callback, [domId]) ⇒
void
-
.listen_select_change(sel, callback) ⇒
void
-
.is_event_on_target(targetDom) ⇒
Boolean
-
.on_tweened(dom, callback) ⇒
监听CSS3 transition动画结束
Kind: static method of event
Param | Type |
---|---|
dom | Element |
callback | function |
如果监听时已加载则直接执行回调
Kind: static method of event
Param | Type |
---|---|
callback | function |
监听设备方向改变
Kind: static method of event
Param | Type |
---|---|
callback | function |
模拟点击事件
Kind: static method of event
Param | Type | Default | Description |
---|---|---|---|
callback | function |
回调函数 | |
[domId] | String |
'fakeClick' |
模拟载体元素的id |
兼容性的监听select更改事件
Kind: static method of event
Param | Type | Description |
---|---|---|
sel | Element |
目标select元素 |
callback | function |
回调函数 |
判断时间是否发生在指定元素范围内
Kind: static method of event
Param | Type | Description |
---|---|---|
targetDom | Element |
指定元素 |
Kind: static namespace of mUtils
-
.format :
object
-
.reverse_str(str) ⇒
String
-
.step_str(target, [step], [needReverse], [div]) ⇒
String
-
.knot_num(num, [fix]) ⇒
String
-
.limit_decimal(num, [fix]) ⇒
String
-
.num_pad_left(num, [leng]) ⇒
String
-
.num_limit_leng(value, leng) ⇒
Number
-
.trim(str) ⇒
String
-
.reverse_str(str) ⇒
字符串反转
Kind: static method of format
Param | Type |
---|---|
str | String |
格式化手机号等,按位插入分隔符
Kind: static method of format
Param | Type | Default | Description |
---|---|---|---|
target |
Number | String
|
目标数字或字符串 | |
[step] | Number |
4 |
插入分隔符的相隔位数 |
[needReverse] | Boolean |
true |
是否要从右向左进行插入 |
[div] | String |
" " |
分隔符 |
添加千分位的数字分结号
Kind: static method of format
Param | Type | Default | Description |
---|---|---|---|
num | Number |
目标数字 | |
[fix] | Number |
|
如果同时要对小数限制位数则指定其位数 |
限制小数位
Kind: static method of format
Param | Type | Default |
---|---|---|
num | Number |
|
[fix] | Number |
2 |
补全数字左侧的0
Kind: static method of format
Param | Type | Default | Description |
---|---|---|---|
num | Number |
目标数字 | |
[leng] | Number |
2 |
最终位数 |
只允许数字并限制位数
Kind: static method of format
Param | Type | Description |
---|---|---|
value | String |
原始输入值 |
leng | Number |
最大长度 |
去首尾空格
Kind: static method of format
Param | Type |
---|---|
str | String |
Kind: static namespace of mUtils
获取基于模板的文本值
Kind: static method of lang
Param | Type | Description |
---|---|---|
tmpl | String |
文本模板,格式为 'hello{0},world{1}' |
...args | String |
用于替换的若干参数 |
Kind: static namespace of mUtils
-
.time :
object
-
.date_to_YMD(date, [div]) ⇒
String
-
.getTime([setting]) ⇒
Date
-
.today([zeroTime]) ⇒
Date
-
.tomorrow([zeroTime]) ⇒
Date
-
.yesterday([zeroTime]) ⇒
Date
-
.get_clean_date([p_month], [p_year]) ⇒
Date
-
.calender(p_year, [p_month]) ⇒
Array
-
.date_range(rangeNums, [from]) ⇒
Object
-
.is_leap_year([year]) ⇒
Boolean
-
.date_to_YMD(date, [div]) ⇒
将字符串转化为yyyymmdd格式
Kind: static method of time
Param | Type | Default | Description |
---|---|---|---|
date | Date |
目标时间 | |
[div] | String |
'-' |
分隔符 |
取得某个时间
Kind: static method of time
Param | Type | Description |
---|---|---|
[setting] | Object |
{偏移值 offset, 基准时间 from, 是否置为0点 zeroTime, 是否置为1日 clean} |
取得当天
Kind: static method of time
Param | Type | Default | Description |
---|---|---|---|
[zeroTime] | Boolean |
true |
是否置为0点 |
取得明天
Kind: static method of time
Param | Type | Default | Description |
---|---|---|---|
[zeroTime] | Boolean |
true |
是否置为0点 |
取得昨天
Kind: static method of time
Param | Type | Default | Description |
---|---|---|---|
[zeroTime] | Boolean |
true |
是否置为0点 |
取得一个某年某月1号0时的干净日期
Kind: static method of time
Param | Type | Default |
---|---|---|
[p_month] | Number |
1 |
[p_year] | Number |
今年 |
取得某月的日历
Kind: static method of time
Returns: Array
- 星期-日期 的二维数组: 周日0,周一1...
Param | Type | Default |
---|---|---|
p_year | Number |
|
[p_month] | Number |
1 |
取得开始-结束的日期范围
Kind: static method of time
Param | Type | Default | Description |
---|---|---|---|
rangeNums | Number |
开始到结束的天数 | |
[from] | Date |
今天 |
基准日期 |
是否闰年
Kind: static method of time
Param | Type | Default | Description |
---|---|---|---|
[year] | Number |
|
年份 |
Kind: static namespace of mUtils
-
.url :
object
-
.URLHash
- new URLHash([href], [hashChar], [separator])
-
.get(key) ⇒
String
-
.put(key, value) ⇒
void
-
.putAll(keyValues) ⇒
void
-
.remove(key) ⇒
void
-
.toString() ⇒
String
-
.clone() ⇒
URLHash
-
.query_params([query]) ⇒
Object
-
.params_query(obj, [separator]) ⇒
String
-
.URLHash
将hash部分转化为object
Kind: static class of url
-
.URLHash
- new URLHash([href], [hashChar], [separator])
-
.get(key) ⇒
String
-
.put(key, value) ⇒
void
-
.putAll(keyValues) ⇒
void
-
.remove(key) ⇒
void
-
.toString() ⇒
String
-
.clone() ⇒
URLHash
构造方法
Param | Type | Default |
---|---|---|
[href] | String |
当前URL的hash |
[hashChar] | String |
'#' |
[separator] | String |
"&" |
取得某个值
Kind: instance method of URLHash
Param | Type |
---|---|
key | String |
放置新值
Kind: instance method of URLHash
Param | Type |
---|---|
key | String |
value | Any |
一次性设置多个值
Kind: instance method of URLHash
Param | Type |
---|---|
keyValues | Object |
删除某个值
Kind: instance method of URLHash
Param | Type |
---|---|
key | String |
转换为字符串
Kind: instance method of URLHash
克隆一个实例
Kind: instance method of URLHash
将query字符串转换为object
Kind: static method of url
Param | Type | Default | Description |
---|---|---|---|
[query] | String |
当前url的query |
给定的query字符串 |
将object转换为query字符串
Kind: static method of url
Param | Type | Default | Description |
---|---|---|---|
obj | Object |
键值对的对象 | |
[separator] | Stribng |
'&' |
分隔符 |
Kind: static namespace of mUtils
-
.utils :
object
锁定屏幕,限制其响应划动事件
Kind: static class of utils
构造方法
Param | Type | Description |
---|---|---|
settings | Object |
构造参数对象,可以设置ignores忽略对象列表等 |
执行锁定
Kind: instance method of ScrollLocker
解除锁定
Kind: instance method of ScrollLocker
确保老旧的设备可以使用html5的新元素
Kind: static method of utils
检查并监听applicationCache的更新情况
Kind: static method of utils
将页面移动至指定位置
Kind: static method of utils
Param | Type |
---|---|
[top] | Number |
在页面中动态写入css
Kind: static method of utils
Param | Type | Description |
---|---|---|
css | String |
css字符串 |
utils.form_primary_valid(form, [submitCallback], [noticeFunction], [submitTriggerEvent], [submitTriggerTarget], [mismatchNoticeName]) ⇒ void
利用html5的原生pattern校验,对自定义了规则的表单项提示错误信息
Kind: static method of utils
Param | Type | Default | Description |
---|---|---|---|
form | Element |
||
[submitCallback] | function |
|
校验成功后的回掉函数,参数为 (form, fieldsObj) |
[noticeFunction] | function |
|
IOS only 提示错误的方法 |
[submitTriggerEvent] | String |
'click' |
触发校验的事件 |
[submitTriggerTarget] | String |
'.submit' |
触发校验的目标元素上下文 |
[mismatchNoticeName] | String |
'mismatch' |
与pattern属性匹配的自定义提示语data-属性 |