Skip to content
On this page

Classes ​

Coordinate Coordinate class CoordinateUtils 提供了百度坐标(BD09)、国测局坐标(火星坐标,GCJ02)、和WGS84坐标系之间的转换,GCJ02 可以通过反复迭代的形式降低精度损失,精度基本可以精确到米,迭代1次,可以达到 0.1 米的精度

Constants ​

DATE_FORMATTER 日期时间格式模板 asyncLoadJs 异步加载js文件 asyncLoadCss 异步加载css文件

Functions ​

requestAnimationFrame(fn) ⇒ number 执行帧动画 cancelAnimationFrame(id) 取消帧动画 sleep(ms) 休眠 ajax(requestParameters, callback) ⇒ Cancelable 请求函数 ajax(requestParameters, callback) ⇒ Cancelable 请求函数 requestAnimationFrame(fn) ⇒ number 执行帧动画 cancelAnimationFrame(id) 取消帧动画 sleep(ms) 休眠 getBrowserInfo() ⇒ Object 获取浏览器信息 getBrowserInfo() ⇒ Object 获取浏览器信息 isNotEmpty(value) ⇒ boolean 判断值是否为空 humpToLine(str) ⇒ string 驼峰转横线 lineToHump(str) ⇒ string 横线转驼峰 computedTextWidth(text, font) ⇒ number 计算文本宽度 getCookie(name) ⇒ string | undefined 读取cookie值 getAllCookies() ⇒ object 读取所有cookie setCookie(name, value, options) ⇒ string | undefined 设置cookie值 removeCookie(name, options) 删除cookie formatDatetime(v, format) ⇒ string 日期时间格式化为字符串 formatTimestamp(v) ⇒ number 日期时间格式化为13位timestamp getDocument() ⇒ Document 获取当前document对象 createDom(tagName, className, cssText) ⇒ HTMLElement 生成DOM元素对象 removeDom(el) 删除DOM元素对象 addClassName(el, className) 增加DOM对象的class removeClassName(el, className) 删除DOM对象的class removeClassNameByClassName(className, doc) ⇒ HTMLElement | null 根据class名称删除多个DOM对象的class injectStyle(style, doc) ⇒ HTMLStyleElement 增加document对象的内联样式 filterXSS(str) ⇒ string 生成过滤XSS的转义字符 getScreenScaleSize(defaultSize, fractionDigits) ⇒ number 获取屏幕缩放比例 getJson(json, cb) ⇒ Object 获取json文件 getJson(json, cb) ⇒ Object 获取json文件 getUrlParam(key, url) ⇒ string 获取url请求中的参数值 getUrlParam(targetUrl) ⇒ string | undefined 获取url请求的主机名 isSameDomain(targetUrl, source) ⇒ boolean 判断两个url顶级域名是否相等 getUuid8(options) ⇒ string 获取8位由数字和字符串组成的随机ID getUuid16(options) ⇒ string 获取16位由数字和字符串组成的随机ID getUuid(len, options) ⇒ string 获取自定义位数的由数字和字符串组成的随机ID,默认32位 getUuidDigit(len, options) ⇒ string 获取自定义位数的由数字组成的随机ID,默认4位

Coordinate ​

Coordinate class

Kind: global class

new Coordinate(lng, lat) ​

Coordinate class

ParamTypeDescription
lngnumberlongitude
latnumberlatitude

Example

javascript
const coordinate = new Coordinate(116.404, 39.915);

CoordinateUtils ​

提供了百度坐标(BD09)、国测局坐标(火星坐标,GCJ02)、和WGS84坐标系之间的转换,GCJ02 可以通过反复迭代的形式降低精度损失,精度基本可以精确到米,迭代1次,可以达到 0.1 米的精度

Kind: global class

CoordinateUtils.transformBD09ToGCJ02(coordinate) ⇒ Coordinate ​

百度坐标(BD09)转 火星坐标(GCJ02)

Kind: static method of CoordinateUtils
Returns: Coordinate -坐标:[经度,纬度]

ParamTypeDescription
coordinateCoordinate百度经纬度

Example

javascript
cnUtils.CoordinateUtils.transformBD09ToGCJ02(coordinate);

CoordinateUtils.transformGCJ02ToBD09(coordinate) ⇒ Coordinate ​

GCJ02 转百度坐标

Kind: static method of CoordinateUtils
Returns: Coordinate -百度坐标:[经度,纬度]

ParamTypeDescription
coordinateCoordinateGCJ02 经纬度

Example

javascript
cnUtils.CoordinateUtils.transformGCJ02ToBD09(coordinate);

CoordinateUtils.simpleTransformGCJ02ToWGS84(coordinate) ⇒ Coordinate ​

简单的GCJ02 转 WGS84

Kind: static method of CoordinateUtils
Returns: Coordinate -WGS84坐标:[经度,纬度]

ParamTypeDescription
coordinateCoordinateGCJ02经纬度

Example

javascript
cnUtils.CoordinateUtils.simpleTransformGCJ02ToWGS84(coordinate);

CoordinateUtils.transformGCJ02ToWGS84(gcj) ⇒ Coordinate ​

GCJ02 转 WGS84

Kind: static method of CoordinateUtils
Returns: Coordinate -WGS84坐标:[经度,纬度]

ParamTypeDescription
gcjCoordinateGCJ02经纬度

Example

javascript
cnUtils.CoordinateUtils.transformGCJ02ToWGS84(coordinate);

CoordinateUtils.transformWGS84ToGCJ02(coordinate) ⇒ Coordinate ​

WGS84 坐标 转 GCJ02

Kind: static method of CoordinateUtils
Returns: Coordinate -GCJ02 坐标:[经度,纬度]

ParamTypeDescription
coordinateCoordinateWGS84经纬度

Example

javascript
cnUtils.CoordinateUtils.transformWGS84ToGCJ02(coordinate);

CoordinateUtils.transformBD09ToWGS84(coordinate) ⇒ Coordinate ​

百度坐标BD09 转 WGS84

Kind: static method of CoordinateUtils
Returns: Coordinate -WGS84 坐标:[经度,纬度]

ParamTypeDescription
coordinateCoordinate经纬度

Example

javascript
cnUtils.CoordinateUtils.transformBD09ToWGS84(coordinate);

CoordinateUtils.transformWGS84ToBD09(coordinate) ⇒ Coordinate ​

WGS84 转 百度坐标BD09

Kind: static method of CoordinateUtils
Returns: Coordinate -BD09 坐标:[经度,纬度]

ParamTypeDescription
coordinateCoordinate经纬度

Example

javascript
cnUtils.CoordinateUtils.transformWGS84ToBD09(coordinate);

CoordinateUtils.transformBD09ToGCJ02(coordinate) ⇒ Coordinate ​

百度坐标(BD09)转 火星坐标(GCJ02)

Kind: static method of CoordinateUtils
Returns: Coordinate -坐标:[经度,纬度]

ParamTypeDescription
coordinateCoordinate百度经纬度

Example

javascript
cnUtils.CoordinateUtils.transformBD09ToGCJ02(coordinate);

CoordinateUtils.transformGCJ02ToBD09(coordinate) ⇒ Coordinate ​

GCJ02 转百度坐标

Kind: static method of CoordinateUtils
Returns: Coordinate -百度坐标:[经度,纬度]

ParamTypeDescription
coordinateCoordinateGCJ02 经纬度

Example

javascript
cnUtils.CoordinateUtils.transformGCJ02ToBD09(coordinate);

CoordinateUtils.simpleTransformGCJ02ToWGS84(coordinate) ⇒ Coordinate ​

简单的GCJ02 转 WGS84

Kind: static method of CoordinateUtils
Returns: Coordinate -WGS84坐标:[经度,纬度]

ParamTypeDescription
coordinateCoordinateGCJ02经纬度

Example

javascript
cnUtils.CoordinateUtils.simpleTransformGCJ02ToWGS84(coordinate);

CoordinateUtils.transformGCJ02ToWGS84(gcj) ⇒ Coordinate ​

GCJ02 转 WGS84

Kind: static method of CoordinateUtils
Returns: Coordinate -WGS84坐标:[经度,纬度]

ParamTypeDescription
gcjCoordinateGCJ02经纬度

Example

javascript
cnUtils.CoordinateUtils.transformGCJ02ToWGS84(coordinate);

CoordinateUtils.transformWGS84ToGCJ02(coordinate) ⇒ Coordinate ​

WGS84 坐标 转 GCJ02

Kind: static method of CoordinateUtils
Returns: Coordinate -GCJ02 坐标:[经度,纬度]

ParamTypeDescription
coordinateCoordinateWGS84经纬度

Example

javascript
cnUtils.CoordinateUtils.transformWGS84ToGCJ02(coordinate);

CoordinateUtils.transformBD09ToWGS84(coordinate) ⇒ Coordinate ​

百度坐标BD09 转 WGS84

Kind: static method of CoordinateUtils
Returns: Coordinate -WGS84 坐标:[经度,纬度]

ParamTypeDescription
coordinateCoordinate经纬度

Example

javascript
cnUtils.CoordinateUtils.transformBD09ToWGS84(coordinate);

CoordinateUtils.transformWGS84ToBD09(coordinate) ⇒ Coordinate ​

WGS84 转 百度坐标BD09

Kind: static method of CoordinateUtils
Returns: Coordinate -BD09 坐标:[经度,纬度]

ParamTypeDescription
coordinateCoordinate经纬度

Example

javascript
cnUtils.CoordinateUtils.transformWGS84ToBD09(coordinate);

DATE_FORMATTER ​

日期时间格式模板

Kind: global constant

asyncLoadJs ​

异步加载js文件

Kind: global constant

asyncLoadCss ​

异步加载css文件

Kind: global constant

requestAnimationFrame(fn) ⇒ number ​

执行帧动画

Kind: global function
Returns: number -动画id

ParamTypeDescription
fnFrameRequestCallback动画函数

Example

javascript
var progress = 0;
function render() {
     progress += 1; // 修改图像的位置
     if (progress < 100) {  // 在动画没有结束前,递归渲染
         requestAnimationFrame(render);
     }
}
const handlerId = requestAnimationFrame(render);
console.log('动画id', handlerId);

cancelAnimationFrame(id) ​

取消帧动画

Kind: global function

ParamTypeDescription
idnumber | undefined | null动画id

Example

javascript
const handlerId = requestAnimationFrame(render);
cancelAnimationFrame(handlerId);

sleep(ms) ​

休眠

Kind: global function

ParamTypeDescription
msnumber休眠时间(单位毫秒)

Example

javascript
sleep(1000);

ajax(requestParameters, callback) ⇒ Cancelable ​

请求函数

Kind: global function
Returns: Cancelable -{ cancel: () => { ... } }

ParamTypeDescription
requestParametersRequestParameters请求参数
requestParameters.urlstring要请求的URL。
requestParameters.headersObject要与请求一起发送的标头。
requestParameters.methodstring请求方法“GET”
requestParameters.bodystring请求正文。
requestParameters.typestring要返回的响应主体类型为“string”、“json”、“arrayBuffer”。
requestParameters.credentialsstring`'same-origin'
requestParameters.collectResourceTimingboolean如果为true,将为这些转换后的请求收集 Resource Timing API 信息,并在相关数据事件的resourceTiming属性中返回。
requestParameters.referrerPolicystring表示请求的referrerPolicy的字符串。有关更多信息和可能的值,请参阅Referrer Policy HTTP头页.
callbackCallback回调函数

Example

javascript
ajax({url: 'data.json', type: "json", method: "GET"}, (res) => {})

ajax(requestParameters, callback) ⇒ Cancelable ​

请求函数

Kind: global function
Returns: Cancelable -{ cancel: () => { ... } }

ParamTypeDescription
requestParametersRequestParameters请求参数
requestParameters.urlstring要请求的URL。
requestParameters.headersObject要与请求一起发送的标头。
requestParameters.methodstring请求方法“GET”
requestParameters.bodystring请求正文。
requestParameters.typestring要返回的响应主体类型为“string”、“json”、“arrayBuffer”。
requestParameters.credentialsstring`'same-origin'
requestParameters.collectResourceTimingboolean如果为true,将为这些转换后的请求收集 Resource Timing API 信息,并在相关数据事件的resourceTiming属性中返回。
requestParameters.referrerPolicystring表示请求的referrerPolicy的字符串。有关更多信息和可能的值,请参阅Referrer Policy HTTP头页.
callbackCallback回调函数

Example

javascript
ajax({url: 'data.json', type: "json", method: "GET"}, (res) => {})

requestAnimationFrame(fn) ⇒ number ​

执行帧动画

Kind: global function
Returns: number -动画id

ParamTypeDescription
fnFrameRequestCallback动画函数

Example

javascript
var progress = 0;
function render() {
     progress += 1; // 修改图像的位置
     if (progress < 100) {  // 在动画没有结束前,递归渲染
         requestAnimationFrame(render);
     }
}
const handlerId = requestAnimationFrame(render);
console.log('动画id', handlerId);

cancelAnimationFrame(id) ​

取消帧动画

Kind: global function

ParamTypeDescription
idnumber | undefined | null动画id

Example

javascript
const handlerId = requestAnimationFrame(render);
cancelAnimationFrame(handlerId);

sleep(ms) ​

休眠

Kind: global function

ParamTypeDescription
msnumber休眠时间(单位毫秒)

Example

javascript
sleep(1000);

getBrowserInfo() ⇒ Object ​

获取浏览器信息

Kind: global function
Returns: Object -{ Actual_Name, Actual_Version }
Example

javascript
const result = getBrowserInfo()
console.log('浏览器' + navigator.userAgent)
console.log('浏览器' + navigator.Actual_Name, '版本号' + navigator.Actual_Version)
console.log(result.Actual_Name, result.Actual_Version)

getBrowserInfo() ⇒ Object ​

获取浏览器信息

Kind: global function
Returns: Object -{ Actual_Name, Actual_Version }
Example

javascript
const result = getBrowserInfo()
console.log('浏览器' + navigator.userAgent)
console.log('浏览器' + navigator.Actual_Name, '版本号' + navigator.Actual_Version)
console.log(result.Actual_Name, result.Actual_Version)

isNotEmpty(value) ⇒ boolean ​

判断值是否为空

Kind: global function
Returns: boolean -是否为空

ParamTypeDescription
valueany值

Example

javascript
const str = null;
const isNull = isNotEmpty(str);
console.log('是否不为空', isNull);

humpToLine(str) ⇒ string ​

驼峰转横线

Kind: global function
Returns: string -转换后的字符

ParamTypeDescription
strstring字符,默认为""

Example

javascript
const str = "helloWorld";
cosnt newStr = humpToLine(str);
console.log('转换后', newStr);

lineToHump(str) ⇒ string ​

横线转驼峰

Kind: global function
Returns: string -转换后的字符

ParamTypeDescription
strstring字符,默认为""

Example

javascript
const str = "hello-world";
cosnt newStr = lineToHump(str);
console.log('转换后', newStr);

computedTextWidth(text, font) ⇒ number ​

计算文本宽度

Kind: global function
Returns: number - 宽度,单位 px

ParamTypeDescription
textstring文本
fontstring字体

getCookie(name) ⇒ string | undefined ​

读取cookie值

Kind: global function
Returns: string | undefined -cookie值

ParamTypeDescription
namestringcookie名称

Example

javascript
const cookie = getCookie('user');
console.log('cookie值', cookie);

getAllCookies() ⇒ object ​

读取所有cookie

Kind: global function
Returns: object -cookie信息,例:{userName: 'admin'}
Example

javascript
const cookie = getAllCookies();
console.log('所有cookie信息', cookie);

setCookie(name, value, options) ⇒ string | undefined ​

设置cookie值

Kind: global function

ParamTypeDescription
namestringcookie名称
valuestringcookie值
optionsCookieAttributes | undefinedcookie属性

Example

javascript
setCookie('userName', 'admin');

removeCookie(name, options) ​

删除cookie

Kind: global function

ParamTypeDescription
namestringcookie名称
optionsCookieAttributescookie属性

Example

javascript
removeCookie('userName');

formatDatetime(v, format) ⇒ string ​

日期时间格式化为字符串

Kind: global function
Returns: string -日期时间字符串

ParamTypeDescription
vstring | Date日期时间
formatstring日期时间格式

Example

javascript
const date = new Date();
const strDate = formatDatetime(date, 'YYYY-MM-DD HH:mm:ss');
console.log('日期时间', strDate);

formatTimestamp(v) ⇒ number ​

日期时间格式化为13位timestamp

Kind: global function
Returns: number -13位timestamp

ParamTypeDescription
vstring | Date日期时间

Example

javascript
const date = new Date();
const numDate = formatTimestamp(date);
console.log('日期时间', numDate);

getDocument() ⇒ Document ​

获取当前document对象

Kind: global function
Returns: Document -DOM对象

createDom(tagName, className, cssText) ⇒ HTMLElement ​

生成DOM元素对象

Kind: global function
Returns: HTMLElement -DOM元素对象

ParamTypeDescription
tagNamestring标签名称
classNamestringclass名称
cssTextstringcss样式

removeDom(el) ​

删除DOM元素对象

Kind: global function

ParamTypeDescription
elElement要素

addClassName(el, className) ​

增加DOM对象的class

Kind: global function

ParamTypeDescription
elElement要素
classNameArray.&lt;string&gt;class名称

removeClassName(el, className) ​

删除DOM对象的class

Kind: global function

ParamTypeDescription
elElement要素
classNameArray.&lt;string&gt;class名称

removeClassNameByClassName(className, doc) ⇒ HTMLElement | null ​

根据class名称删除多个DOM对象的class

Kind: global function
Returns: HTMLElement | null -DOM元素对象

ParamTypeDescription
classNamestringclass名称
docDocumentDOM对象

injectStyle(style, doc) ⇒ HTMLStyleElement ​

增加document对象的内联样式

Kind: global function
Returns: HTMLStyleElement -DOM对象的内联样式

ParamTypeDescription
stylestringcss样式
docDocumentDOM对象

filterXSS(str) ⇒ string ​

生成过滤XSS的转义字符

Kind: global function
Returns: string -转义后字符

ParamTypeDescription
strstring字符

getScreenScaleSize(defaultSize, fractionDigits) ⇒ number ​

获取屏幕缩放比例

Kind: global function
Returns: number -屏幕缩放比例

ParamTypeDescription
defaultSizenumber默认尺寸
fractionDigitsnumber | undefined小数位数

Example

javascript
const screenScaleSize = getScreenScaleSize(1920, 2);
console.log('屏幕缩放比例', screenScaleSize);

getJson(json, cb) ⇒ Object ​

获取json文件

Kind: global function
Returns: Object -{ cancel: () => controller.abort() }

ParamTypeDescription
jsonStringjson文件地址
cbfunction回调函数

Example

javascript
getJson('data.json',(res) => {
   console.log(res)
})

getJson(json, cb) ⇒ Object ​

获取json文件

Kind: global function
Returns: Object -{ cancel: () => controller.abort() }

ParamTypeDescription
jsonStringjson文件地址
cbfunction回调函数

Example

javascript
getJson('data.json',(res) => {
   console.log(res)
})

getUrlParam(key, url) ⇒ string ​

获取url请求中的参数值

Kind: global function
Returns: string -参数值

ParamTypeDescription
keystring参数名称
urlstring请求url

Example

javascript
const url = 'http://localhost:8080/getList?id=12';
const id = getUrlParam('id', url);
console.log('参数值', id);

getUrlParam(targetUrl) ⇒ string | undefined ​

获取url请求的主机名

Kind: global function
Returns: string | undefined -主机名

ParamTypeDescription
targetUrlstring请求url

Example

javascript
const url = 'http://localhost:8080/getList?id=12';
const host = getHost(url);
console.log('主机名', host);

isSameDomain(targetUrl, source) ⇒ boolean ​

判断两个url顶级域名是否相等

Kind: global function
Returns: boolean -是否相等

ParamTypeDescription
targetUrlstring请求url,默认为""
sourcestring请求url,默认为本机名

Example

javascript
const url = 'http://localhost:8080/getList?id=12';
const isSome = isSameDomain(url);
console.log('是否相等', isSome);

getUuid8(options) ⇒ string ​

获取8位由数字和字符串组成的随机ID

Kind: global function
Returns: string -uuid

ParamTypeDescription
optionsany规则参数

getUuid16(options) ⇒ string ​

获取16位由数字和字符串组成的随机ID

Kind: global function
Returns: string -uuid

ParamTypeDescription
optionsany规则参数

getUuid(len, options) ⇒ string ​

获取自定义位数的由数字和字符串组成的随机ID,默认32位

Kind: global function
Returns: string -uuid

ParamTypeDescription
lennumber位数
optionsany规则参数

getUuidDigit(len, options) ⇒ string ​

获取自定义位数的由数字组成的随机ID,默认4位

Kind: global function
Returns: string -uuid

ParamTypeDescription
lennumber位数
optionsany规则参数