短小而精悍的JsvaScript函数
1. 回到顶部, 使用浏览器的刷新频率 requestAnimationFrame 来实现的
const scrollToTop = () => {
const c = document.documentElement.scrollTop || document.body.scrollTop;
if (c > 0) {
window.requestAnimationFrame(scrollToTop);
window.scrollTo(0, c - c / 8);
}
};
scrollToTop()
2. smoothScroll (平滑滚动)
const smoothScroll = element =>
document.querySelector(element).scrollIntoView({
behavior: 'smooth'
});
3. CopyToClipboard (复制到剪切板, 建议在PC端使用)
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);
}
};
4. HH:MM:SS 时间格式的快速获取
const getColonTimeFromDate = date => date.toTimeString().slice(0, 8); getColonTimeFromDate(new Date()); // "09:38:22"
5. debounce(比较常见)
const debounce = (fn, ms = 0) => {
let timeoutId;
return function(...args) {
clearTimeout(timeoutId);
timeoutId = setTimeout(() => fn.apply(this, args), ms);
};
};
window.addEventListener(
'resize',
debounce(() => {
console.log(window.innerWidth);
console.log(window.innerHeight);
}, 250)
);
6. equals 深度对比相等
const equals = (a, b) => {
if (a === b) return true;
if (a instanceof Date && b instanceof Date) return a.getTime() === b.getTime();
if (!a || !b || (typeof a != 'object' && typeof b !== 'object')) return a === b;
if (a === null || a === undefined || b === null || b === undefined) return false;
if (a.prototype !== b.prototype) return false;
let keys = Object.keys(a);
if (keys.length !== Object.keys(b).length) return false;
return keys.every(k => equals(a[k], b[k]));
};
equals({ a: [2, { e: 3 }], b: [4], c: 'foo' }, { a: [2, { e: 3 }], b: [4], c: 'foo' }); // true
7. escapeHTML
const escapeHTML = str =>
str.replace(
/[&<>'"]/g,
tag =>
({
'&': '&',
'<': '<',
'>': '>',
"'": ''',
'"': '"'
}[tag] || tag)
); escapeHTML('<a href="#">Me & you</a>'); // '<a href="#">Me & you</a>'
8. getURLParameters
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'}
getURLParameters('google.com'); // {}
参考链接
https://github.com/Chalarangelo/30-seconds-of-code
http://www.zhangxinxu.com/wordpress/2013/09/css3-animation-requestanimationframe-tween-%E5%8A%A8%E7%94%BB%E7%AE%97%E6%B3%95/ (张鑫旭讲解 requestAnimationFrame)
https://juejin.im/post/59d74afe5188257e8267b03f (scrollIntoView 与 scrollIntoViewIfNeeded)
短小而精悍的JsvaScript函数的更多相关文章
- C++中的inline函数
内联函数: () 内联函数定义和作用: 将一个函数声明为inline,那么函数就成为内联函数.内联函数通常就是它在程序中每个调用点上“内联地”展开.从定义上看,内联函数跟一般函数不一样,一般函数调用的 ...
- C++程序设计基础
01 1 预编译常用的有,宏定义和包含库.2 库:是实用工具的集和,由程序员编写,可以完成一些特定的功能.3 <> 系统库 ""用户自定义库.4 宏定义:定义符号常量, ...
- ios项目里扒出来的json文件
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Menlo; color: #000000 } p.p2 { margin: 0.0px 0. ...
- Github上关于iOS的各种开源项目集合(强烈建议大家收藏,查看,总有一款你需要)
下拉刷新 EGOTableViewPullRefresh - 最早的下拉刷新控件. SVPullToRefresh - 下拉刷新控件. MJRefresh - 仅需一行代码就可以为UITableVie ...
- swift开源项目精选
Swift 开源项目精选-v1.0 2016-03-07 22:11 542人阅读 评论(0) 收藏 举报 分类: iOS(55) Swift(4) 目录(?)[+] 转自 http: ...
- jQuery源码笔记(一):jQuery的整体结构
jQuery 是一个非常优秀的 JS 库,与 Prototype,YUI,Mootools 等众多的 Js 类库相比,它剑走偏锋,从 web 开发的实用角度出发,抛除了其它 Lib 中一些中看但不实用 ...
- iOS - 开发类库
开发类库 UI 项目名称 项目信息 1.MJRefresh 仅需一行代码就可以为UITableView或者CollectionView加上下拉刷新或者上拉刷新功能.可以自定义上下拉刷新的文字说明. ...
- iOS及Mac开源项目和学习资料【超级全面】
UI 下拉刷新 EGOTableViewPullRefresh – 最早的下拉刷新控件. SVPullToRefresh – 下拉刷新控件. MJRefresh – 仅需一行代码就可以为UITable ...
- iOS:iOS开发非常全的三方库、插件等等
iOS开发非常全的三方库.插件等等 github排名:https://github.com/trending, github搜索:https://github.com/search. 此文章转自git ...
随机推荐
- maven包上传私服
选择需要上传的项目右键-->Run As-->Run Configurations-->Maven Buid-->右键 new -->选择 base directory- ...
- python基础题型一
一.执行Python脚本的两种方式 #python name.py Python解析器 #chmod +x name.py 二.简述位.字节的关系 1Byte=8bit 三.简述ascii.uni ...
- IDEA PYCHARM USAGE NOTE
初次安装使用PyCharm,在新建.py文件时会发现文件头并没有什么信息,因此,使用模板会比较方便. 方法如下: 1.打开PyCharm,选择File--Settings 2.依次选择Editor-- ...
- IE8的input兼容性问题
在chrome.firefox.IE9+都是支持input事件 在IE8中,单纯的input事件无法监听输入框中变化,需要与propertychange共用 测试代码如下: <!DOCTYPE ...
- 4--Selenium环境准备---chromedriver.exe 与chrome版本匹配
0.jdk8 和eclipse 4.6 https://www.eclipse.org/downloads/packages/release/neon/3 1.selenium-server-stan ...
- JAVA_概念01_跨域
1.什么是跨域? 协议.域名.端口都相同是同域,否则是跨域. 服务器不允许ajax跨域获取数据 2.解决办法? ①jsonp :Jsonp不是一种数据格式,而json是一种数据格式,jsonp是用来解 ...
- web(四)html表单类标签
表单类标签 操作者用于输入信息,并将信息提交给服务器的标签集合. 表单标签介绍 form标签:表单元素(其余标签)标签的容器标签 input标签:用于用户信息输入的标签. button标签:按钮标签. ...
- win7快捷方式图标修复
test.bat ///////////////////////////////////////////////////////////////////////// rem 关闭Windows外壳程序 ...
- apache php upload file
/********************************************************************************* * apache php uplo ...
- 【c++基础】字符数组和string相互转换
字符数组转化成string类型char ch [] = "ABCDEFG";string str(ch);//也可string str = ch;或者char ch [] = &q ...