五个罕见的 JavaScript 原生 API

开发 前端
本文带来5个难得一见的JavaScriot原生API,为我们的前端开发带来意想不到的便利。

本文带来5个难得一见的JavaScriot原生API,为我们的前端开发带来意想不到的便利。

1. getBoundingClientRect()

Element.getBoundingClientRect() 方法返回一个 DOMRect 对象,该对象提供有关元素大小及其相对于视口的位置的信息。

domRect = element.getBoundingClientRect();

返回左、上、右、下、x、y、宽度和高度元素的值。

图片

例如,获取DOM元素相对于页面左上角的top和left定位距离的值。

const h3 = document.querySelector("h3");
const rect = h3.getBoundingClientRect();
const topElement = document.documentElement;


const positionTop = topElement.scrollTop + rect.top;
const positionLeft = topElement.scrollLeft + rect.left;

2. window.getComputedStyle() 

window.getComputedStyle() 方法返回一个 CSSStyleDeclaration 对象,其类型与样式属性相同,其中包含元素的计算样式。

document.defaultView.getComputedStyle(element, [pseudo-element])
// or
window.getComputedStyle(element, [pseudo-element])

它有两个参数,第一个是计算样式的元素,第二个是伪元素;如果伪元素不存在,则传递 null。

例子:

<!DOCTYPE html>
<html>
<head>
<style type="text/css">
#root {
background-color: pink;
width: 100px;
height: 200px;
}
#root::after {
content: 'Haskell';
display: table;
clear: both;
}
</style>
</head>
<body>
<div id="root" style="background-color: rgb(135, 206, 235);"></div>
</body>
<script>
function getStyleByAttr(node, name) {
return window.getComputedStyle(node, null)[name]
}
const node = document.getElementById('root')
// rgb(135, 206, 235)
console.log(getStyleByAttr(node, 'backgroundColor'))
// 100px
console.log(getStyleByAttr(node, 'width'))
// 200px
console.log(getStyleByAttr(node, 'height'))
// table
console.log(window.getComputedStyle(node, '::after').display)
// Haskell
console.log(window.getComputedStyle(node, '::after').content)
</script>
</html>

3. once: true

once: true 不是 API,看起来也不像。用于属性配置,有了它,lodash的once就不用了。

const container = document.querySelector<HTMLDivElement>('.container');


container?.addEventListener('click', () => {
console.log('I will only do it once !')
}, {
// After configuring once, it will be called at most once
once: true
})

4. getModifierState()

如果指定的修改键被按下或激活,则 getModifierState() 方法返回 true。

例如,我们可以使用它来监听用户在打字时是否按下了尺寸切换键,然后根据情况给出适当的提示。

<input type="text" size="40" onkeydown="myFunction(event)">


<p id="demo"></p>


<script>
function myFunction(event) {
var x = event.getModifierState("CapsLock");
document.getElementById("demo").innerHTML = "Caps Lock: " + x;
}
</script>

5.clipboard.readText()

clipboard,我敢肯定,是一个常用的功能。

要从剪贴板中读取文本,请调用 navigator.clipboard.readText() 并等待返回的 Promise 进行解析。

async function getClipboardContents() {
try {
const text = await navigator.clipboard.readText();
console.log('Pasted content: ', text);
} catch (err) {
console.error('Failed to read clipboard contents: ', err);
}
}

要将文本复制到剪贴板,只需调用 writeText()。

async function copyPageUrl() {
try {
await navigator.clipboard.writeText(location.href);
console.log('Page URL copied to clipboard');
} catch (err) {
console.error('Failed to copy: ', err);
}
}

总结

以上就是我今天想与你分享的5个关于JavaScript原生的API的知识内容,希望这些内容对你有所帮助。

责任编辑:华轩 来源: web前端开发
相关推荐

2021-08-30 09:08:31

云数据库JavaScriptSDK

2021-08-13 06:22:55

云原生安全云原生云安全

2023-12-19 13:30:00

JavaScrip原生API函数

2020-09-01 07:58:34

API漏洞黑客

2022-07-12 10:18:05

JavaScript单行代码

2022-05-13 14:28:03

云原生权限云原生

2021-12-30 21:51:10

JavaScript开发内存

2023-08-18 15:12:00

JavaScript开发

2021-06-16 15:04:06

JavaScript内存开发

2022-01-17 21:37:24

JavaScriptHTMLCSS

2022-07-28 08:51:17

应用程序接口APICIO

2023-02-03 17:29:46

2016-03-03 14:29:15

2022-02-07 08:41:42

云原生Kubernetes

2021-10-09 07:10:31

JavaScript对象Python

2011-11-18 09:28:17

地理位置API

2021-02-05 08:18:29

JavaScript开发代码

2024-04-23 08:16:21

2021-12-06 10:40:01

One-Liner代码前端

2022-07-08 09:53:51

JavaScript单行代码
点赞
收藏

51CTO技术栈公众号