摘要:显示所有指定的元素用操作符和清除所有指定元素的属性。使用了两个事件监听器。将指定的数组元素转换成元素标签,然后将它们插入指定的选择器元素内用和去生成一个元素标签列表复制一个字符串到剪切板。用去执行复制到剪切板。
英文文章来源于:https://github.com/Chalarangelo/30-seconds-of-code/blob/master/README.md
Browser currentURL返回当前页面的 URL。
用 window.location.href 获得当前的 URL.
const currentURL = () => window.location.href; // currentUrl() -> "https://google.com"getURLParameters
返回一个包含当前页面 URL 的参数的对象.
用 match() 和一个合适的正则来获得所有的键值对, 用Array.reduce() 去映射它们到一个对象内。
用 location.search 获得当前 url 的参数.
const getURLParameters = url =>
url.match(/([^?=&]+)(=([^&]*))/g).reduce(
(a, v) => (a[v.slice(0, v.indexOf("="))] = v.slice(v.indexOf("=") + 1), a), {}
);
// getURLParameters("http://url.com/page?name=Adam&surname=Smith") -> {name: "Adam", surname: "Smith"}
redirect
重定向到一个指定的 URL.
用 window.location.href 或 window.location.replace() 重定向 url 。
如果asLink 为 true,则模拟一个 链接单击,否则为重定向。
const redirect = (url, asLink = true) =>
asLink ? window.location.href = url : window.location.replace(url);
// redirect("https://google.com")
httpsRedirect
如果它当前在HTTP中,将页面重定向到HTTPS。另外,按后退按钮不会将其返回到HTTP页面,因为它在历史中被替换了。
用 location.protocol 去获得当前所使用的协议。如果不是 HTTPS,用 location.replace() 将当前页转换为使用 HTTPS 协议。用 location.href 去获得链接地址,用 String.split() 移除当前 URL的协议。
const httpsRedirect = () => {
if(location.protocol !== "https:") location.replace("https://" + location.href.split("//")[1]);
}
detectDeviceType
检测网站是在移动设备,还是在桌面/笔记本上打开。
用一个正则表达式检测 navigator.userAgent 属性来判断是移动设备还是桌面/笔记本。
const detectDeviceType = () =>
/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)
? "Mobile"
: "Desktop";
detectDeviceType(); // "Mobile" or "Desktop"
scrollToTop
平滑滚动到页面顶部。
用 document.documentElement.scrollTop 或 document.body.scrollTop 得到页面滚动后的top值。
用 window.requestAnimationFrame() 去实现平滑滚动.
const scrollToTop = () => {
const c = document.documentElement.scrollTop || document.body.scrollTop;
if (c > 0) {
window.requestAnimationFrame(scrollToTop);
window.scrollTo(0, c - c / 8);
}
};
// scrollToTop()
elementIsVisibleInViewport
如果指定的元素出现在可视窗口内,返回 true;否则,返回 false .
用 Element.getBoundingClientRect() 和 window.inner(Width|Height) 的值来判断给定元素是否在可视窗口内.
如果忽略第二个参数或者指定为 true 将判断元素的部分出现在可是窗口内。
const elementIsVisibleInViewport = (el, partiallyVisible = false) => {
const { top, left, bottom, right } = el.getBoundingClientRect();
const { innerHeight, innerWidth } = window;
return partiallyVisible
? ((top > 0 && top < innerHeight) || (bottom > 0 && bottom < innerHeight)) &&
((left > 0 && left < innerWidth) || (right > 0 && right < innerWidth))
: top >= 0 && left >= 0 && bottom <= innerHeight && right <= innerWidth;
};
// e.g. 100x100 viewport and a 10x10px element at position {top: -1, left: 0, bottom: 9, right: 10}
// elementIsVisibleInViewport(el) -> false (not fully visible)
// elementIsVisibleInViewport(el, true) -> true (partially visible)
bottomVisible
如果到达页面可显示区域的底部,返回 true ,否则返回 false .
用 scrollY, scrollHeight 和 clientHeight 去判断是否到达页面可显示区域底部.
const bottomVisible = () => document.documentElement.clientHeight + window.scrollY >= (document.documentElement.scrollHeight || document.documentElement.clientHeight); // bottomVisible() -> truegetScrollPosition
返回页面滚动后的位置,也即是html文档的卷起位置.
如果浏览器支持 pageXOffset 和 pageYOffset 就用它们;否则就用 scrollLeft 和 scrollTop.
参数 el 的默认值为 window.
const getScrollPosition = (el = window) =>
({x: (el.pageXOffset !== undefined) ? el.pageXOffset : el.scrollLeft,
y: (el.pageYOffset !== undefined) ? el.pageYOffset : el.scrollTop});
// getScrollPosition() -> {x: 0, y: 200}
getStyle
返回指定元素属性的css值。
用 Window.getComputedStyle() 去获取指定元素的属性值。
const getStyle = (el, ruleName) => getComputedStyle(el)[ruleName];
getStyle(document.querySelector("p"), "font-size"); // "16px"
hasClass
如果一个元素有指定的 class,返回 true ;否则,返回 false.
用 element.classList.contains() 去判断一个元素是否有一个指定的class.
const hasClass = (el, className) => el.classList.contains(className);
hasClass(document.querySelector("p.special"), "special"); // true
toggleClass
切换一个元素的class。
用 element.classList.toggle() 去切换指定元素的class。
const toggleClass = (el, className) => el.classList.toggle(className);
toggleClass(document.querySelector("p.special"), "special"); // 这个段落将不再有 "special" class
setStyle
设置指定元素css样式属性的值.
用 element.style 设置指定元素的样式 value。
const setStyle = (el, ruleName, value) => (el.style[ruleName] = value);
setStyle(document.querySelector("p"), "font-size", "20px"); // The first element on the page will have a font-size of 20px
hide
隐藏所有指定的元素。
用 spread (...) 操作符和 Array.forEach() 去给所有的指定元素添加 display: none 样式 。
const hide = (...el) => [...el].forEach(e => (e.style.display = "none"));
hide(document.querySelectorAll("img")); // Hides all
elements on the page
show
显示所有指定的元素
用spread (...) 操作符和 Array.forEach() 清除所有指定元素的 display 属性。
const show = (...el) => [...el].forEach(e => (e.style.display = ""));
show(document.querySelectorAll("img")); // Shows all
elements on the page
speechSynthesis
语音合成 (实验特性)
用 SpeechSynthesisUtterance.voice 和 window.speechSynthesis.getVoices() 将一个消息转换为语音.
用 window.speechSynthesis.speak() 播放消息.
更多详情请参考 SpeechSynthesisUtterance interface of the Web Speech API.
const speechSynthesis = message => {
const msg = new SpeechSynthesisUtterance(message);
msg.voice = window.speechSynthesis.getVoices()[0];
window.speechSynthesis.speak(msg);
};
speechSynthesis("Hello, World"); // // plays the message
onUserInputChange
不管什么时候用户的input类型改变 (mouse or touch) 执行这个函数。用于启用/禁用依赖于输入设备的代码。这个过程是动态的,并于混合设备(例如:触摸屏,笔记本)一起工作。
使用了两个事件监听器。假如 mouse 输入初始化,然后绑定一个 touchstart 时间将听页面。
依据 touchstart 添加一个 mousemove 事件监听器,然后用 performance.now() 在20ms触发两次 mousemove 事件。
在这两种情况下,执行回调函数以input类型作为参数。
const onUserInputChange = callback => {
let type = "mouse",
lastTime = 0;
const mousemoveHandler = () => {
const now = performance.now();
if (now - lastTime < 20)
(type = "mouse"), callback(type), document.removeEventListener("mousemove", mousemoveHandler);
lastTime = now;
};
document.addEventListener("touchstart", () => {
if (type === "touch") return;
(type = "touch"), callback(type), document.addEventListener("mousemove", mousemoveHandler);
});
};
onUserInputChange(type => {
console.log("The user is now using", type, "as an input method.");
});
UUIDGeneratorBrowser
在浏览器中生成UUID。
用 crypto API 生成一个 UUID, 可参考 RFC4122 v.4 。
const UUIDGeneratorBrowser = () =>
([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, c =>
(c ^ (crypto.getRandomValues(new Uint8Array(1))[0] & (15 >> (c / 4)))).toString(16)
);
UUIDGeneratorBrowser(); // "7982fcfe-5721-4632-bede-6000885be57d"
arrayToHtmlList
将指定的数组元素转换成 元素标签,然后将它们插入指定的id选择器元素内.
用 Array.map() 和 document.querySelector() 去生成一个html元素标签列表.
const arrayToHtmlList = (arr, listID) => arr.map(item => document.querySelector("#"+listID).innerHTML+=`复制一个字符串到剪切板。只用在用户触发事件时才会执行 (i.e. 内部用 click 事件监听)。
创建一个 元素,然后给它填充指定的字符串作为文本,最后将这个元素添加到html文档中。
用 Selection.getRangeAt() 存储选中的范围 (如果有的情况下)。
用 document.execCommand("copy") 去执行复制到剪切板。
从HTML文档中移除 元素.
最后,用 Selection().addRange() 去恢复原来的选中范围 (如果有的情况下).
const copyToClipboard = str => {
const el = document.createElement("textarea");
el.value = str;
el.setAttribute("readonly", "");
el.style.position = "absolute";
el.style.left = "-9999px";
document.body.appendChild(el);
const selected =
document.getSelection().rangeCount > 0 ? document.getSelection().getRangeAt(0) : false;
el.select();
document.execCommand("copy");
document.body.removeChild(el);
if (selected) {
document.getSelection().removeAllRanges();
document.getSelection().addRange(selected);
}
};
copyToClipboard("Lorem ipsum"); // "Lorem ipsum" copied to clipboard.
更多关于30-seconds-code中文翻译
https://github.com/lvzhenbang/article/blob/master/js/30-seconds-code/index.md
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/90682.html
摘要:英文文章来源于给定一个键值和一组参数,但给定一个上下文时调用它们。 英文文章来源于:https://github.com/Chalarangelo/30-seconds-of-code/blob/master/README.md Adapter call 给定一个键值和一组参数,但给定一个上下文时调用它们。 使用闭包调用存储的键值与存储的参数 const call = ( key, ....
摘要:英文文章来源于删除对象中除指定键值的属性用递归的方法用方法遍历对象然后删除不是在给定数组中的属性如果你传入,它将对该键所对应的对象进行深度遍历的变形非原著作对所有的键对应的对象进行深度遍历用方法遍历对象然后删除不是在给定数组中的属性如 英文文章来源于:https://github.com/Chalarangelo/30-seconds-of-code/blob/master/READM...
摘要:英文文章来源于计算一个字符串中字符的所有排序情况使用递归遍历字符串中的每个字符计算剩余字符串的所有顺序用区合并该字符和剩余字符串的每种顺序然后用将该字符串的所有顺序合并到一个数组中当字符串的等于或者时,是两个基例字符串的首字母大写用 英文文章来源于:https://github.com/Chalarangelo/30-seconds-of-code/blob/master/README...
摘要:英文文章来源于返回参数列表中第一个非和的参数用实现返回第一个非参数返回一个用自定义函数中的函数是否返回来对中传入的参数列表尽心过滤用去遍历参数列表,用给定的函数的返回值来过滤参数列表返回给定值的基本类型返回给定值的构造函数名字的小 Utility 英文文章来源于:https://github.com/Chalarangelo/30-seconds-of-code/blob/master...
摘要:英文文章来源于数组最大公约数计算数字数组最大公约数用和运算式使用递归计算一个数字数组的最大公约数数组最小公倍数求数字数组的最小公倍数用和运算式使用递归计算一个数字数组的最小公倍数返回一个数组中的最大值。 英文文章来源于:https://github.com/Chalarangelo/30-seconds-of-code/blob/master/README.md Array 数组最大公...
阅读 3526·2021-11-25 09:43
阅读 3660·2021-10-11 10:58
阅读 2985·2021-09-27 13:59
阅读 3373·2021-09-24 09:55
阅读 2360·2019-08-30 15:52
阅读 2079·2019-08-30 14:03
阅读 2417·2019-08-30 11:11
阅读 2206·2019-08-28 18:12