ES6 DEMO
案例:
①匿名封装
(function(window,document){
const HEAD = 1;
let MSG = function(options){
this._init(options);
}
//原型
MSG.prototype._init = function({msg}){
this._doSomeThing(msg);
}
MSG.prototype._doSomeThing = function(msg){
alert(msg);
}
// 挂载
window.$Msg = Msg;
})(window,document);
// 调用
new $Msg({msg : '您好'}); ②PromiseAll运用,图片全部加载才显示出来 (function(document) {
let LoadImg = function(){
let promises = imgs.map(src => {
return this.imgToPromise(src);
}); this.imgLoad(promises);
}
LoadImg.prototype.imgToPromise = src => {
return new Promise((resolve,reject) => {
const img = new Image();
img.src = src;
img.onload = () => {
你resolve(img);
};
img.onerror = (e) => {
reject(e);
}
})
}
LoadImg.prototype.imgLoad = arr => {
Promise.all(arr).then((arr)=>{
arr.forEach(img => {
document.body.append(img);
})
},err => {
// 错误
console.log(err);
})
}
window.LoadImg = LoadImg;
})(document); const imgs = [
'https://pics1.baidu.com/feed/5882b2b7d0a20cf4f2291433b2004030adaf99b5.jpeg?token=00786888f8e87b7704e637824f570ece&s=BB98C8015A64750DC42015C00300A0B2','https://pics7.baidu.com/feed/4610b912c8fcc3ce8b524ed0574cdd8ed53f2056.jpeg?token=024ed25c842ae64030effe79f8832ee7&s=E9271F70592B412C18783DD30300C0B2','https://pics7.baidu.com/feed/4610b912c8fcc3ce8b524ed0574cdd8ed53f2056.jpeg?token=024ed25c842ae64030effe79f8832ee7&s=E9271F70592B412C18783DD30300C0B2'
];
new LoadImg(imgs);
③ promise动画
function moveTo(el,x,y){
return new Promise( resolve => {
el.style.transform = `translate(${x}px,${y}px)`;
setTimeout(function(){
resolve();
},1000)
}) }
let el = document.querySelector('div'); document.querySelector('button').addEventListener('click', e => {
moveTo(el,100,100).then(()=>{
console.log(1);
return moveTo(el,200,200);
}).then(()=>{
console.log(1);
return moveTo(el,100,100);
}).then(()=>{
console.log(1);
return moveTo(el,0,0);
})
}) ④对象封装变形类
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<style type="text/css">
.ball{
width: 150px;
height: 150px;
background: linear-gradient(#ff9b9b 50%,#106dbb 50%);
border-radius: 50%;
}
</style>
<body>
<div class="ball"></div>
</body> <script type="text/javascript">
class Transform{
constructor(el){
this.el = document.querySelector(el);
// 队列
this._queue = [];
// 默认时间
this.default_transition = 3000;
this._transform = {
rotate: '',
translate: '',
scale: '',
}
} // 位移
translate(value, time){
return this._add('translate', value, time);
}
// 缩放
scale(value, time){
return this._add('scale',value,time);
}
// 形变
rotate(value,time){
return this._add('rotate',value,time);
} _add(type, value, time){
this._queue.push({type, value, time});
return this;
}
done() {
this._start();
}
_start() {
if(!this._queue.length)return; // 先进先出
setTimeout(() => {
// shift() 方法用于把数组的第一个元素从其中删除,并返回第一个元素的值。
const info = this._queue.shift();
let transition = info.time ? info.time : this.default_transition; this.el.style.transition = `all ${ transition / 1000}s`; this.el.style.transform = this._getTransform(info);
setTimeout(() => {
this._start();
},transition);
},0)
}
_getTransform({ time, value, type}){
switch(type){
case 'translate':
this._transform.translate = `translate(${ value })`;
break;
case 'scale':
this._transform.scale = `scale(${ value })`;
break;
case 'rotate':
this._transform.rotate = `rotate(${ value }deg)`;
break;
}
return `${this._transform.translate} ${this._transform.scale} ${this._transform.rotate}`;
}
} let tf = new Transform(".ball");
tf.translate('100px,100px').scale('1.5').rotate(2220,220000).done();
</script>
</html>
ES6 DEMO的更多相关文章
- JavaScript面向对象轻松入门之封装(demo by ES5、ES6、TypeScript)
本章默认大家已经看过作者的前一篇文章 <JavaScript面向对象轻松入门之抽象> 为什么要封装? 封装(Encapsulation)就是把对象的内部属性和方法隐藏起来,外部代码访问该对 ...
- vue+node+es6+webpack创建简单vue的demo
闲聊: 小颖之前一直说是写一篇用vue做的简单demo的文章,然而小颖总是给自己找借口,说没时间,这一没时间一下就推到现在了,今天抽时间把这个简单的demo整理下,给大家分享出来,希望对大家也有所帮助 ...
- JavaScript面向对象轻松入门之概述(demo by ES5、ES6、TypeScript)
写在前面的话 这是一个JavaScript面向对象系列的文章,本篇文章主要讲概述,介绍面向对象,后面计划还会有5篇文章,讲抽象.封装.继承.多态,最后再来一个综合. 说实话,写JavaScript面向 ...
- JavaScript面向对象轻松入门之继承(demo by ES5、ES6)
继承是面向对象很重要的一个概念,分为接口继承和实现继承,接口继承即为继承某个对象的方法,实现继承即为继承某个对象的属性.JavvaScript通过原型链来实现接口继承.call()或apply()来实 ...
- JavaScript面向对象轻松入门之多态(demo by ES5、ES6、TypeScript)
多态(Polymorphism)按字面的意思就是"多种状态",同样的行为(方法)在不同对象上有不同的状态. 在OOP中很多地方都要用到多态的特性,比如同样是点击鼠标右键,点击快捷方 ...
- 一个基于ES6+webpack的vue小demo
上一篇文章<一个基于ES5的vue小demo>我们讲了如何用ES5,vue-router做一个小demo,接下来我们来把它变成基于ES6+webpack的demo. 一.环境搭建及代码转换 ...
- 一个webpack,react,less,es6的DEMO
1.package.json如下 { "name": "demo", "version": "1.0.0", " ...
- JavaScript面向对象轻松入门之抽象(demo by ES5、ES6、TypeScript)
抽象的概念 狭义的抽象,也就是代码里的抽象,就是把一些相关联的业务逻辑分离成属性和方法(行为),这些属性和方法就可以构成一个对象. 这种抽象是为了把难以理解的代码归纳成与现实世界关联的概念,比如小狗这 ...
- JavaScript ES6和ES5闭包的小demo
版权声明:署名,允许他人基于本文进行创作,且必须基于与原先许可协议相同的许可协议分发本文 (Creative Commons) 可能有些小伙伴不知道ES6的写法,这儿先填写一个小例子 let conn ...
随机推荐
- Codeforces_731_F
http://codeforces.com/problemset/problem/731/F 其实是暴力枚举,但是有些小技巧,直接保存每个数的数量. 枚举每个起点时,然后依次加上起点大小的分段的数量的 ...
- Codeforces_338_D
http://codeforces.com/problemset/problem/338/D 中国剩余定理的应用,思路是确定可能符合的最小行和最小列,然后判断是否符合.若不符合则后面的(最小的倍数)也 ...
- Codeforces 922 C - Robot Vacuum Cleaner (贪心、数据结构、sort中的cmp)
题目链接:点击打开链接 Pushok the dog has been chasing Imp for a few hours already. Fortunately, Imp knows that ...
- num07---工厂方法模式
一.简单工厂模式 [之所以叫简单,说明没有完全做到 设计模式的要求] 前言:活字印刷术,面向对象思想 复用 维护 扩展 灵活 高内聚低耦合 以 实现 一个计算器 为例: 1.创建 抽象类count, ...
- Go语言实现:【剑指offer】二叉搜索树的第k个的结点
该题目来源于牛客网<剑指offer>专题. 给定一棵二叉搜索树,请找出其中的第k小的结点.例如,(5,3,7,2,4,6,8) 中,按结点数值大小顺序第三小结点的值为4. Go语言实现: ...
- k8s系列--- dashboard认证及分级授权
http://blog.itpub.net/28916011/viewspace-2215214/ 因版本不一样,略有改动 Dashboard官方地址: https://github.com/kube ...
- php 安装扩展插件实例-gd库
今天给php 安装一个扩展插件 gd库 一.gd库是什么 gd库是一个开源的图像处理库,它通过提供一系列用来处理图片的API,使其可以用来创建图表.图形.缩略图以及其他图像的处理操作. gd库支持 ...
- MySQL索引那些事
原文链接 大家有没有遇到过慢查询的情况,执行一条SQL需要几秒,甚至十几.几十秒的时间,这时候DBA就会建议你去把查询的 SQL 优化一下,怎么优化?你能想到的就是加索引吧? 为什么加索引就查的快了? ...
- Python爬虫连载10-Requests模块、Proxy代理
一.Request模块 1.HTTP for Humans,更简洁更友好 2.继承了urllib所有的特征 3.底层使用的是urllib3 4.开源地址:https://github.com/req ...
- Python股票量化第一步环境搭建
很久之前就希望可以量化分析股票,那么国内的股票数据API也有个,最有名的就是tushare,然后还有baostock. 今天我们就来研究一下这个baostock吧. 首先,我们需要下载一个叫做anac ...