Appearance
super-tools-lib - v1.74.0 / Modules / dom
Module: dom
Table of contents
Functions
- addClass
- classnames
- eleReplace
- getAttr
- getDomBound
- getOffset
- getSelectedText
- getTransformMatrix
- hasClass
- insertAfter
- insertBefore
- insertHtmlAfter
- insertHtmlBefore
- removeClass
- replaceClass
- scrollToTheBottom
- setAttr
- shaking
- stopPropagation
- textVisibilityChange
Functions
addClass
▸ addClass(ele
, className
): void
添加类名
Since
1.0.0
Example
ts
import { addClass } from 'super-tools-lib'
addClass('dom', 'active')
Parameters
Name | Type | Description |
---|---|---|
ele | HTMLElement | 元素 |
className | string | 类名 |
Returns
void
classnames
▸ classnames(...args
): string
返回一个字符串,其中包含所有非空参数的类名,这些参数可以是字符串或对象。
Example
ts
import { classnames } from 'super-tools-lib'
classnames('foo', 'bar'); // 'foo bar'
classnames('foo', { bar: true }); // 'foo bar'
#### Parameters
| Name | Type | Description |
| :------ | :------ | :------ |
| `...args` | `any`[] | 一个包含类名的数组 |
#### Returns
`string`
返回一个类名字符串
___
### eleReplace
▸ **eleReplace**(`oldEle`, `newEle`): `void`
替换dom元素
**`Since`**
1.62.0
**`Example`**
```ts
import { eleReplace } from 'super-tools-lib'
eleReplace(dom1, dom2)
Parameters
Name | Type | Description |
---|---|---|
oldEle | Element | 旧元素 |
newEle | Element | 新元素 |
Returns
void
getAttr
▸ getAttr(element
, attrName
): string
用于获取元素的属性值
Since
1.62.0
Example
ts
import { getAttr } from 'super-tools-lib'
getAttr(dom1, 'id')
Parameters
Name | Type | Description |
---|---|---|
element | HTMLElement | 需要获取属性值的元素 |
attrName | string | 属性名称 |
Returns
string
返回属性值
getDomBound
▸ getDomBound(elem
, flag?
): { bottom
: number
= box.bottom; left
: number
= box.left; right
: number
= box.right; top
: number
= box.top } | { x
: number
= box.left; y
: number
= box.top }[]
获取dom四个角的坐标 返回数组[{x,y}] | {top, left, right, bottom}格式
Since
1.62.0
Example
ts
import { getDomBound } from 'super-tools-lib'
const dom = document.getElementById('test')
getDomBound(dom)
// => [{x: 0, y: 0}, {x: 100, y: 0}, {x: 100, y: 100}, {x: 0, y: 100}}]
Parameters
Name | Type | Description |
---|---|---|
elem | HTMLElement | dom元素 |
flag? | boolean | 返回格式 true: {top, left, right, bottom} false: [{x,y}] |
Returns
{ bottom
: number
= box.bottom; left
: number
= box.left; right
: number
= box.right; top
: number
= box.top } | { x
: number
= box.left; y
: number
= box.top }[]
返回数组[{x,y}] | {top, left, right, bottom}格式
getOffset
▸ getOffset(ele
): Object
获取一个元素距离浏览器左上角的偏移量
Since
1.0.0
Example
ts
import { getOffset } from 'super-tools-lib'
getOffset(ele) // { left: 0, top: 20 }
Parameters
Name | Type | Description |
---|---|---|
ele | HTMLElement | 元素 |
Returns
Object
返回元素距离浏览器左上角的偏移量
Name | Type |
---|---|
left | number |
top | number |
getSelectedText
▸ getSelectedText(): string
获取鼠标所选文本
Since
1.62.0
Example
ts
import { getSelectedText } from 'super-tools-lib'
getSelectedText() // 1111
Returns
string
返回鼠标选择的文本
getTransformMatrix
▸ getTransformMatrix(transform
): Object
获取transform translate矩阵中x,y坐标
Since
1.62.0
Example
ts
import { getTransformMatrix } from 'super-tools-lib'
getTransformMatrix('translate(10px, 10px)') // { x: 10, y: 10 }
Parameters
Name | Type | Description |
---|---|---|
transform | string | css transform |
Returns
Object
返回x,y坐标
Name | Type |
---|---|
x | number |
y | number |
hasClass
▸ hasClass(element
, className
): boolean
判断元素是否包含某个class
Since
1.0.0
Example
ts
import { hasClass } from 'super-tools-lib'
hasClass(dom, 'active') // false
Parameters
Name | Type | Description |
---|---|---|
element | HTMLElement | 需要判断的元素 |
className | string | class名称 |
Returns
boolean
返回true / false
insertAfter
▸ insertAfter(anotherEle
, newEle
): void
在目标元素之后插入一个新元素
Since
1.62.0
Example
ts
import { insertAfter } from 'super-tools-lib'
insertAfter(dom1, dom2)
Parameters
Name | Type | Description |
---|---|---|
anotherEle | Element | 目标元素 |
newEle | Element | 新元素 |
Returns
void
insertBefore
▸ insertBefore(anotherEle
, newEle
): void
在目标元素之前插入一个新元素
Since
1.62.0
Example
ts
import { insertAfter } from 'super-tools-lib'
insertAfter(dom1, dom2)
Parameters
Name | Type | Description |
---|---|---|
anotherEle | Element | 目标元素 |
newEle | Element | 新元素 |
Returns
void
insertHtmlAfter
▸ insertHtmlAfter(ele
, html
): void
在元素后插入给定的HTML
Since
1.62.0
Example
ts
import { insertHtmlAfter } from 'super-tools-lib'
insertHtmlAfter(dom1, '<div></div>')
Parameters
Name | Type | Description |
---|---|---|
ele | Element | 元素 |
html | string | html字符串 |
Returns
void
insertHtmlBefore
▸ insertHtmlBefore(ele
, html
): void
在元素前插入给定的HTML
Since
1.62.0
Example
ts
import { insertHtmlBefore } from 'super-tools-lib'
insertHtmlBefore(dom1, '<div></div>')
Parameters
Name | Type | Description |
---|---|---|
ele | Element | 元素 |
html | string | html字符串 |
Returns
void
removeClass
▸ removeClass(ele
, className
): void
删除类名
Since
1.0.0
Example
ts
import { removeClass } from 'super-tools-lib'
removeClass(dom, 'active')
Parameters
Name | Type | Description |
---|---|---|
ele | HTMLElement | 元素 |
className | string | 类名 |
Returns
void
replaceClass
▸ replaceClass(ele
, newName
, oldName
): void
替换类名
Since
1.0.0
Example
ts
import { replaceClass } from 'super-tools-lib'
replaceClass(dom, 'active', 'oldName')
Parameters
Name | Type | Description |
---|---|---|
ele | HTMLElement | 元素 |
newName | string | 新类名 |
oldName | string | 旧类名 |
Returns
void
scrollToTheBottom
▸ scrollToTheBottom(ele
, callback
, dis?
, delay?
): void
监听滚动条滚动到底部
Since
1.62.0
Example
ts
import { scrollToTheBottom } from 'super-tools-lib'
scrollToTheBottom(dom, () => {
console.log('到底部啦')
})
Parameters
Name | Type | Default value | Description |
---|---|---|---|
ele | HTMLElement | undefined | 元素 |
callback | () => void | undefined | 事件回调 |
dis | number | 0 | 距离滚动条底部多远触发事件 默认0 |
delay | number | 200 | 截流函数时间 默认200毫秒 |
Returns
void
setAttr
▸ setAttr(element
, attrName
, value
): void
用于设置元素的属性值
Since
1.62.0
Example
ts
import { setAttr } from 'super-tools-lib'
setAttr(dom1, 'id', 'box')
Parameters
Name | Type | Description |
---|---|---|
element | HTMLElement | 需要设置属性值的元素 |
attrName | string | 属性名称 |
value | string | 属性值 |
Returns
void
shaking
▸ shaking(params
): void
抖动函数
Since
1.0.0
Example
ts
import { shaking } from 'super-tools-lib'
shaking({ele: dom, attr: 'left' })
Parameters
Name | Type | Description |
---|---|---|
params | Object | |
params.attr | string | 抖动的方向 left top |
params.cb | () => void | 抖动的完成回调 |
params.ele | HTMLElement | 抖动的dom元素 |
params.rate? | number | 抖动次数 默认20次 |
params.time? | number | 每次抖动需要的时间 默认每次50毫秒 |
Returns
void
stopPropagation
▸ stopPropagation(e
): void
阻止冒泡事件
Since
1.0.0
Example
ts
import { stopPropagation } from 'super-tools-lib'
stopPropagation(dom)
Parameters
Name | Type | Description |
---|---|---|
e | Event | Event |
Returns
void
textVisibilityChange
▸ textVisibilityChange(dom
): boolean
计算文字是否溢出容器
Since
1.62.0
Example
ts
import { textVisibilityChange } from 'super-tools-lib'
textVisibilityChange(textDom) // true
Parameters
Name | Type | Description |
---|---|---|
dom | HTMLElement | 承载文字的元素 |
Returns
boolean
返回 true / false