Appearance
super-tools-lib - v1.74.0 / Modules / bom
Module: bom
Table of contents
Variables
Functions
- copyText
- disableContext
- exitFullscreen
- getLocalStorageSize
- getPosition
- getUrlParam
- getWindowCenter
- getWindowCorners
- mBroadcastChannel
- newWin
- noRefdelUrlParam
- onVisibilityChange
- scrollToTop
- titleTex
- toFullScreen
- userBrowser
Variables
LocalStorage
• Const
LocalStorage: Object
LocalStorage
- 存储的数据的生命周期是永久,除非主动删除数据,否则永远不会过期
Since
1.0.0
Example
ts
import { LocalStorage } from 'super-tools-lib'
// 设置值
LocalStorage.set('container', 'data', {name: '张三', age: 12})
// 查询值
LocalStorage.get('container', 'data') // {name: '张三', age: 12}
// 遍历LocalStorage
LocalStorage.forEach((value, key)=>{
console.log(value, key) // {data:{name: '张三', age: 12}} container
})
// 删除
LocalStorage.remove('container', 'data')
// 清空LocalStorage
LocalStorage.clear()
Type declaration
Name | Type |
---|---|
clear | () => void |
forEach | (iteratee : (value : any , key : string ) => void ) => void |
get | (container : string , key : string ) => string |
remove | (container : string , key : string ) => void |
set | (container : string , key : string , value : any , date? : string | number | Date ) => void |
SessionStorage
• Const
SessionStorage: Object
SessionStorage
- 存储的数据的生命周期是一个会话
Since
1.60.0
Example
ts
import { SessionStorage } from 'super-tools-lib'
// 设置值
SessionStorage.set('container', 'data', {name: '张三', age: 12})
// 查询值
SessionStorage.get('container', 'data') // {name: '张三', age: 12}
// 遍历SessionStorage
SessionStorage.forEach((value, key)=>{
console.log(value, key) // {data:{name: '张三', age: 12}} container
})
// 删除
SessionStorage.remove('container', 'data')
// 清空SessionStorage
SessionStorage.clear()
Type declaration
Name | Type |
---|---|
clear | () => void |
forEach | (iteratee : (value : any , key : string ) => void ) => void |
get | (container : string , key : string ) => string |
remove | (container : string , key : string ) => void |
set | (container : string , key : string , value : any ) => void |
cookie
• Const
cookie: Object
cookie增删改查
Since
1.62.0
Example
ts
import { cookie } from 'super-tools-lib'
cookie.set('token', 'NSBSNAKSA2771HS11212', 1680078662360)
cookie.get('token') // NSBSNAKSA2771HS11212
cookie.delete('token')
Type declaration
Name | Type |
---|---|
delete | (key : string ) => void |
get | (key : string ) => string |
set | (key : string , value : string , expTime? : number ) => void |
Functions
copyText
▸ copyText(text
): void
复制文本
Since
1.62.0
Example
ts
import { copyText } from 'super-tools-lib'
copyText('复制的内容')
Parameters
Name | Type | Description |
---|---|---|
text | string | 文本 |
Returns
void
disableContext
▸ disableContext(flag
): void
禁止/开启:右键、选择、复制
Since
1.62.0
Example
ts
import { disableContext } from 'super-tools-lib'
// 禁止浏览器右键、选择、复制
disableContext(true)
// 开启浏览器右键、选择、复制
disableContext(false)
Parameters
Name | Type | Description |
---|---|---|
flag | boolean | true禁止 false开启 |
Returns
void
exitFullscreen
▸ exitFullscreen(): void
浏览器退出全屏
Since
1.0.0
Example
ts
import { exitFullscreen } from 'super-tools-lib'
// 退出全屏显示
exitFullscreen()
Returns
void
getLocalStorageSize
▸ getLocalStorageSize(): string
获取localStorage使用容量
Since
1.62.0
Example
ts
import { getLocalStorageSize } from 'super-tools-lib'
getLocalStorageSize() // 11KB
Returns
string
返回localStorage的使用空间大小 (KB)
getPosition
▸ getPosition(ops?
): Promise
<unknown
>
获取地理位置
- 方法用来获取设备当前位置因浏览器安全策略需要https环境中使用
Since
1.60.0
Example
ts
import { getPosition } from 'super-tools-lib'
getPosition().then((res)=>{
console.log(res) // {lat: 123.2131231, lon: 232.3e13123, accuracy: 20, timestamp: 1680059936948}
}).catch(err => console.log(err))
Parameters
Name | Type | Description |
---|---|---|
ops | Object | 参数 |
ops.enableHighAccuracy? | boolean | 指示浏览器获取高精度的位置,默认为false |
ops.maximumAge? | number | 最长有效期,在重复获取地理位置时,此参数指定多久再次获取位置。 |
ops.timeout? | number | 指定获取地理位置的超时时间,默认不限时,单位为毫秒 |
Returns
Promise
<unknown
>
getUrlParam
▸ getUrlParam(): any
获取url?后参数
Since
1.62.0
Example
ts
import { getUrlParam } from 'super-tools-lib'
getUrlParam() // { type: 1 }
Returns
any
返回?后的参数 //
getWindowCenter
▸ getWindowCenter(): Object
获取窗口中心点
Since
1.64.0
Example
ts
import { getWindowCenter } from 'super-tools-lib'
getWindowCenter()
// => {x: 960, y: 540}
Returns
Object
返回一个对象,包含 x 和 y 坐标
Name | Type |
---|---|
x | number |
y | number |
getWindowCorners
▸ getWindowCorners(): { x
: number
= 0; y
: number
= 0 }[]
获取窗口四个角的坐标
Since
1.64.0
Example
ts
import { getWindowCorners } from 'super-tools-lib'
getWindowCorners()
// => [{x: 0, y: 0}, {x: 1920, y: 0}, {x: 1920, y: 1080}, {x: 0, y: 1080}]
Returns
{ x
: number
= 0; y
: number
= 0 }[]
返回一个数组,包含四个角的坐标
mBroadcastChannel
▸ mBroadcastChannel(name
): BroadcastChannel
创建或加入某个频道
- Broadcast Channel API 可以实现同 源 下浏览器不同窗口,Tab 页,frame 或者 iframe 下的 浏览器上下文 (通常是同一个网站下不同的页面) 之间的简单通讯。
Since
1.67.0
Example
ts
import { mBroadcastChannel } from 'super-tools-lib'
const channel = mBroadcastChannel('channel')
channel.postMessage('hello')
channel.onmessage = (message) => {
console.log(message)
}
channel.close()
Parameters
Name | Type | Description |
---|---|---|
name | string | 频道名称 |
Returns
BroadcastChannel
返回一个频道
newWin
▸ newWin(url
): void
打开一个新的窗口
- 会自动创建一个a标签,然后点击a标签,完成跳转
- 规避了在异步中window.open浏览器的拦截
Since
1.65.0
Example
ts
import { newWin } from 'super-tools-lib'
newWin('https://www.baidu.com')
Parameters
Name | Type |
---|---|
url | any |
Returns
void
noRefdelUrlParam
▸ noRefdelUrlParam(ref
): string
无刷新去除url参数
- 去除url后不会刷新浏览器
Since
1.62.0
Example
ts
import { noRefdelUrlParam } from 'super-tools-lib'
// 当前url http://localhost:8888/?id=2
noRefdelUrlParam('id')
// 去除后的url http://localhost:8888/?
Parameters
Name | Type |
---|---|
ref | string |
Returns
string
返回最新的url
onVisibilityChange
▸ onVisibilityChange(callback
): void
监听浏览器窗口是否可见
Since
1.72.0
Example
ts
import { onVisibilityChange } from 'super-tools-lib'
onVisibilityChange((flag) => {
if (flag) {
console.log('可见')
} else {
console.log('不可见')
}
})
Parameters
Name | Type | Description |
---|---|---|
callback | (flag : boolean ) => void | 回调函数 true: 可见 false: 不可见 |
Returns
void
scrollToTop
▸ scrollToTop(): void
平滑滚动到页面顶部
Since
1.62.0
Example
ts
import { scrollToTop } from 'super-tools-lib'
scrollToTop()
Returns
void
titleTex
▸ titleTex(title
): Object
滚动页面title
Since
1.67.0
Example
ts
import { titleTex } from 'super-tools-lib'
const tex = titleTex('我是标题')
tex.start()
tex.end()
Parameters
Name | Type | Description |
---|---|---|
title | string | 标题 |
Returns
Object
- 返回一个对象,包含start和end方法
Name | Type |
---|---|
end | () => void |
start | (time? : number ) => void |
toFullScreen
▸ toFullScreen(): void
浏览器全屏
Since
1.0.0
Example
ts
import { toFullScreen } from 'super-tools-lib'
// 浏览器全屏显示
toFullScreen()
Returns
void
userBrowser
▸ userBrowser(): string
返回当前浏览器是什么类型的浏览器
Since
1.62.0
Example
ts
import { userBrowser } from 'super-tools-lib'
userBrowser() // Chrome
Returns
string
返回当前所在的浏览器标识