1.0.20 • Published 7 years ago

mobile-utils v1.0.20

Weekly downloads
5
License
ISC
Repository
github
Last release
7 years ago

mobile-utils

common utils of mobile html5 development

1. How to install

npm i mobile-utils --save

2. How to use

//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)

3. API

mUtils : object

the entry point

Kind: global namespace

mUtils.dom : object

Kind: static namespace of mUtils

dom.has_class(ele, clsName) ⇒ Boolean

判断元素是否有指定className

Kind: static method of dom

ParamType
eleElement
clsNameString

dom.remove_class(ele, clsName) ⇒ void

删除className

Kind: static method of dom

ParamType
eleElement
clsNameString

dom.add_class(ele, clsName) ⇒ void

添加className

Kind: static method of dom

ParamType
eleElement
clsNameString

dom.real_style(ele, styleName) ⇒ String

获取计算样式

Kind: static method of dom

ParamTypeDefaultDescription
eleElement目标元素
styleNameString样式名

dom.append_HTML(dom, html) ⇒ void

在指定元素内的最末插入html

Kind: static method of dom

ParamType
domElement
htmlString

dom.prepend_HTML(dom, html) ⇒ void

在指定元素内的开始插入html

Kind: static method of dom

ParamType
domElement
htmlString

dom.tag_range_from_HTML(p_featureStr, p_html) ⇒ Object

根据特征值找到一段html中其所在的tag范围

Kind: static method of dom
Returns: Object - {start, end}

ParamTypeDescription
p_featureStrString特征值
p_htmlString作为查找范围的html

dom.transformXY(dom, x, y, duration) ⇒ void

为元素附加2d移动的css3样式

Kind: static method of dom

ParamTypeDefaultDescription
domElement目标元素
xNumberx轴上移动的px值
yNumbery轴上移动的px值
durationNumber持续的毫秒数

mUtils.env : object

获取移动设备型号版本等环境信息

Kind: static namespace of mUtils
Default: { ua, version, ios, android, windows, blackberry, weixin, wVersion, qq, qVersion, touchSupport, hashSupport }

mUtils.event : object

Kind: static namespace of mUtils

event.on_tweened(dom, callback) ⇒ void

监听CSS3 transition动画结束

Kind: static method of event

ParamType
domElement
callbackfunction

event.on_page_loaded(callback) ⇒ void

如果监听时已加载则直接执行回调

Kind: static method of event

ParamType
callbackfunction

event.on_page_rotated(callback) ⇒ void

监听设备方向改变

Kind: static method of event

ParamType
callbackfunction

event.fake_click(callback, domId) ⇒ void

模拟点击事件

Kind: static method of event

ParamTypeDefaultDescription
callbackfunction回调函数
domIdString'fakeClick'模拟载体元素的id

event.listen_select_change(sel, callback) ⇒ void

兼容性的监听select更改事件

Kind: static method of event

ParamTypeDescription
selElement目标select元素
callbackfunction回调函数

event.is_event_on_target(targetDom) ⇒ Boolean

判断时间是否发生在指定元素范围内

Kind: static method of event

ParamTypeDescription
targetDomElement指定元素

mUtils.format : object

Kind: static namespace of mUtils

format.reverse_str(str) ⇒ String

字符串反转

Kind: static method of format

ParamType
strString

format.step_str(target, step, needReverse, div) ⇒ String

格式化手机号等,按位插入分隔符

Kind: static method of format

ParamTypeDefaultDescription
targetNumber | String目标数字或字符串
stepNumber4插入分隔符的相隔位数
needReverseBooleantrue是否要从右向左进行插入
divString" "分隔符

format.knot_num(num, fix) ⇒ String

添加千分位的数字分结号

Kind: static method of format

ParamTypeDefaultDescription
numNumber目标数字
fixNumber如果同时要对小数限制位数则指定其位数

format.limit_decimal(num, fix) ⇒ String

限制小数位

Kind: static method of format

ParamTypeDefault
numNumber
fixNumber2

format.num_pad_left(num, leng) ⇒ String

补全数字左侧的0

Kind: static method of format

ParamTypeDefaultDescription
numNumber目标数字
lengNumber2最终位数

format.num_limit_leng(value, leng) ⇒ Number

只允许数字并限制位数

Kind: static method of format

ParamTypeDescription
valueString原始输入值
lengNumber最大长度

format.trim(str) ⇒ String

去首尾空格

Kind: static method of format

ParamType
strString

mUtils.lang : object

Kind: static namespace of mUtils

lang.read_i18n(tmpl, ...args) ⇒ String

获取基于模板的文本值

Kind: static method of lang

ParamTypeDescription
tmplString文本模板,格式为 'hello{0},world{1}'
...argsString用于替换的若干参数

mUtils.time : object

Kind: static namespace of mUtils

time.date_to_YMD(date, div) ⇒ String

将字符串转化为yyyymmdd格式

Kind: static method of time

ParamTypeDefaultDescription
dateDate目标时间
divString'-'分隔符

time.getTime(setting) ⇒ Date

取得某个时间

Kind: static method of time

ParamTypeDescription
settingObject{偏移值 offset, 基准时间 from, 是否置为0点 zeroTime, 是否置为1日 clean}

time.today(zeroTime) ⇒ Date

取得当天

Kind: static method of time

ParamTypeDefaultDescription
zeroTimeBooleantrue是否置为0点

time.tomorrow(zeroTime) ⇒ Date

取得明天

Kind: static method of time

ParamTypeDefaultDescription
zeroTimeBooleantrue是否置为0点

time.yesterday(zeroTime) ⇒ Date

取得昨天

Kind: static method of time

ParamTypeDefaultDescription
zeroTimeBooleantrue是否置为0点

time.get_clean_date(p_month, p_year) ⇒ Date

取得一个某年某月1号0时的干净日期

Kind: static method of time

ParamTypeDefault
p_monthNumber1
p_yearNumber今年

time.calender(p_year, p_month) ⇒ Array

取得某月的日历

Kind: static method of time
Returns: Array - 星期-日期 的二维数组: 周日0,周一1...

ParamTypeDefault
p_yearNumber
p_monthNumber1

time.date_range(rangeNums, from) ⇒ Object

取得开始-结束的日期范围

Kind: static method of time

ParamTypeDefaultDescription
rangeNumsNumber开始到结束的天数
fromDate今天基准日期

time.is_leap_year(year) ⇒ Boolean

是否闰年

Kind: static method of time

ParamTypeDefaultDescription
yearNumber年份

mUtils.url : object

Kind: static namespace of mUtils

url.URLHash

将hash部分转化为object

Kind: static class of url

new URLHash(href, hashChar, separator)

构造方法

ParamTypeDefault
hrefString当前URL的hash
hashCharString'#'
separatorString"&"

urlHash.get(key) ⇒ String

取得某个值

Kind: instance method of URLHash

ParamType
keyString

urlHash.put(key, value) ⇒ void

放置新值

Kind: instance method of URLHash

ParamType
keyString
valueAny

urlHash.putAll(keyValues) ⇒ void

一次性设置多个值

Kind: instance method of URLHash

ParamType
keyValuesObject

urlHash.remove(key) ⇒ void

删除某个值

Kind: instance method of URLHash

ParamType
keyString

urlHash.toString() ⇒ String

转换为字符串

Kind: instance method of URLHash

urlHash.clone() ⇒ URLHash

克隆一个实例

Kind: instance method of URLHash

url.query_params(query) ⇒ Object

将query字符串转换为object

Kind: static method of url

ParamTypeDefaultDescription
queryString当前url的query给定的query字符串

url.params_query(obj, separator) ⇒ String

将object转换为query字符串

Kind: static method of url

ParamTypeDefaultDescription
objObject键值对的对象
separatorStribng'&'分隔符

mUtils.utils : object

Kind: static namespace of mUtils

utils.ScrollLocker

锁定屏幕,限制其响应划动事件

Kind: static class of utils

new ScrollLocker(settings)

构造方法

ParamTypeDescription
settingsObject构造参数对象,可以设置ignores忽略对象列表等

scrollLocker.lock()

执行锁定

Kind: instance method of ScrollLocker

scrollLocker.unlock()

解除锁定

Kind: instance method of ScrollLocker

utils.enable_html5_for_old() ⇒ void

确保老旧的设备可以使用html5的新元素

Kind: static method of utils

utils.check_appcache() ⇒ void

检查并监听applicationCache的更新情况

Kind: static method of utils

utils.page_to_top(top) ⇒ void

将页面移动至指定位置

Kind: static method of utils

ParamType
topNumber

utils.write_CSS(css) ⇒ void

在页面中动态写入css

Kind: static method of utils

ParamTypeDescription
cssStringcss字符串

utils.form_primary_valid(form, submitCallback, noticeFunction, submitTriggerEvent, submitTriggerTarget, mismatchNoticeName) ⇒ void

利用html5的原生pattern校验,对自定义了规则的表单项提示错误信息

Kind: static method of utils

ParamTypeDefaultDescription
formElement
submitCallbackfunction校验成功后的回掉函数,参数为 (form, fieldsObj)
noticeFunctionfunctionIOS only 提示错误的方法
submitTriggerEventString'click'触发校验的事件
submitTriggerTargetString'.submit'触发校验的目标元素上下文
mismatchNoticeNameString'mismatch'与pattern属性匹配的自定义提示语data-属性
1.0.20

7 years ago

1.0.19

7 years ago

1.0.18

7 years ago

1.0.17

7 years ago

1.0.16

7 years ago

1.0.15

7 years ago

1.0.14

8 years ago

1.0.12

8 years ago

1.0.11

8 years ago

1.0.10

8 years ago

1.0.9

8 years ago

1.0.8

8 years ago

1.0.7

8 years ago

1.0.6

8 years ago

1.0.5

8 years ago

1.0.4

8 years ago

1.0.3

8 years ago

1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago