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 1249F Maximum Weight Subset (贪心)
题意 在一颗有点权的树上,选若干个点,使得这些点两两距离大于k,且点权和最大 思路 贪心的取比较大的值即可 将所有点按照深度从大到小排序,如果当前点点权\(a[i]\)大于0,则将距离为k以内的所有点 ...
- cookie 笔记
Cookie “小甜点” Cookie的作用是与服务器进行交互,作为HTTP规范的一部分而存在 ,而Web Storage仅仅是为了在本地“存储”数据而生 用来记录:用户信息 计算机信息 浏 ...
- 基于 Serverless Component 全栈解决方案
什么是 Serverless Component Serverless Component 是 Serverless Framework 的,支持多个云资源编排和组织的场景化解决方案. Serverl ...
- Django表单Form类对空值None的替换
最近在写项目的时候用到Form,发现这个类什么都好,就是有些空值的默认赋值真是很不合我胃口. 查阅资料.官方文档后发现并没有设置该值的方式.于是,便开始了我的踩坑之路...... 不过现在完美解决了, ...
- Classmethod and Staticmethod - Python 类方法 和 静态方法
classmethod and staticmethod classmethod 的是一个参数是类对象 cls (本类,或者子类), 而不是实例对象 instance (普通方法). classmet ...
- FastDFS 配置文件 tracker.conf
FastDFS 版本5.05 配置文件分为三部分 控制器:tracker.conf存储器:storage.conf 客户端:client.conf 文件位置:/etc/fdfs 基本配置(基础配置 ...
- centos6.5安装openLDAP2.3
查看系统版本,内核,定时任务同步时间,关闭防火墙selinux等 [root@ldap-master ~]# cat /etc/redhat-release CentOS release 6.5 (F ...
- light oj 1214 - Large Division 大数除法
1214 - Large Division Given two integers, a and b, you should check whether a is divisible by b or n ...
- Spring有哪些配置方式
1.XML 配置文件. Bean 所需的依赖项和服务在 XML 格式的配置文件中指定.这些配置文件通常包含许多 bean 定义和特定于应用程序的配置选项.它们通常以 bean 标签开头.例如: < ...
- linux中压缩解压缩命令
目录 gzip gunzip tar(打包压缩) tar(解包解压) zip unzip bzip2 bunzip2 gzip 解释 命令名称:gzip 命令英文原意:GUN zip 命令所在路径:/ ...