ES6实现小案例--自定义弹框

按照国际惯例先放效果图

 

index.html

<!DOCTYPE html>
<html lang="en">

head>
  meta charset="UTF-8"name="viewport" content="width=device-width,initial-scale=1.0"http-equiv="X-UA-Compatible"="ie=edge"title>demo</link rel="stylesheet" href="./msg.css"style>
    #pop {
      border: 0 none;
      color #fff
      outline none
      padding 10px 20px
      font-size 18px
      background #2594f0
      cursor pointer;
    }

    #pop:active  blue}
  bodybutton id="pop">弹个框button<!--   <div class="msg__wrap">
    <div class="msg-header">
      <span>确认删除</span>
      <span class="msg-header-close-button">×</span>
    </div>
    <div class="msg-body">
      <div class="msg-body-icon">
        <div class="msg-body-icon-wrong"></div>
      </div>
      <div class="msg-body-content">是否删除</div>
    </div>
    <div class="msg-footer">
      <button class="msg-footer-btn msg-footer-cancel-button">算了吧</button>
      <button class="msg-footer-btn msg-footer-confirm-button">好的</button>
    </div>
  </div> -->
 
  script src="./msg.js"></scripttype="text/javascript"
    document.querySelector('#pop).addEventListener(click,function() {
      new $Msg({
        content: 贤者模式马上就过去了...真的要清空吗?<span style="color: orange;">~~~</span>(e){
          console.log("confirm);
          console.log(this);
        },cancel:cancel20pxlightgreen
        },useHTML:true
      });
    });
  html>

msg.css

/* 弹出框最外层 */
.msg__wrap {
  position: fixed;
  top: 50%;
  left:
  z-index: 10;
  transition: all .3s;
  transform: translate(-50%,-50%) scale(0,0);
  max-width:

  background: #fff;
  box-shadow: 0 0 10px #eee;
  font-size: 10px;
}

 弹出框头部 
.msg__wrap .msg-header {
  padding: 10px 10px 0 10px; 1.8em;
}

.msg__wrap .msg-header .msg-header-close-button {
  float: right;
  cursor: pointer;
}

 弹出框中部 
.msg__wrap .msg-body { 10px 10px 10px 10px;
  display: flex;
}

 图标 
.msg__wrap .msg-body .msg-body-icon{
  width: 80px;
}

.msg__wrap .msg-body .msg-body-icon div{ 45px;
  height:
  margin: 0 auto;
  line-height:
  color:
  border-radius: 50% 50%; 2em;
}

.msg__wrap .msg-body .msg-body-icon .msg-body-icon-success{
  background: #32a323;
  text-align: center;
}

.msg__wrap .msg-body .msg-body-icon .msg-body-icon-success::after{
  content: "成";
}

.msg__wrap .msg-body .msg-body-icon .msg-body-icon-wrong{ #ff8080;

.msg__wrap .msg-body .msg-body-icon .msg-body-icon-wrong::after{ "误";
}

.msg__wrap .msg-body .msg-body-icon .msg-body-icon-info{ #80b7ff;

.msg__wrap .msg-body .msg-body-icon .msg-body-icon-info::after{ "注";
}

 内容 
.msg__wrap .msg-body .msg-body-content{
  min-width: 200px; 1.5em;
  word-break: break-all; flex;
  align-items: center;
  padding-left: 10px;
  box-sizing: border-box;
}

 弹出框底部 
.msg__wrap .msg-footer { 0 10px 10px 10px;
  flex-direction: row-reverse;
}

.msg__wrap .msg-footer .msg-footer-btn { 50px; 30px;
  border: 0 none;
  outline: none; 1em; 2px;
  margin-left: 5px; pointer;
}

.msg__wrap .msg-footer .msg-footer-cancel-button{
  background-color: #ff3b3b;
}

.msg__wrap .msg-footer .msg-footer-cancel-button:active{ #ff6f6f;
}

.msg__wrap .msg-footer .msg-footer-confirm-button{ #4896f0;
}

.msg__wrap .msg-footer .msg-footer-confirm-button:active{ #1d5fac;
}

 遮罩层 
.msg__overlay { 0;
  right:
  bottom: 5; rgba(0,.4);
  opacity: 0;
}

msg.js

//放入自执行的匿名函数,避免污染全局作用域
//将window和document作为参数传入,可以直接在内部获取,不用再去外部找,性能更优
(function(window,document){
    构造函数
    let Msg=(options){
        初始化一个弹出框
        this._init(options);
    }

    初始化一个弹出框
    Msg.prototype._init=function({content="",confirm=null,cancel=false,contentStyle={},contentFontSize="1.5em"}){
        this.content=content;
        this.confirm=confirm;
        this.cancel=cancel;
        this.useHTML=useHTML;
        this.contentStyle=contentStyle;
        this.contentFontSize=contentFontSize;

        生成DOM元素
        ._createElement();
        绑定事件
        this._bind([this._el,._overlay]);
        显示
        this._show([._overlay]);
    }

    生成DOM元素
    Msg.prototype._createElement=(){
        let wrap=document.createElement("div");
        wrap.className="msg__wrap";
        换行前加上\转义
        wrap.innerHTML='<div class="msg-header">\
                          <span>确认删除</span>\
                          <span class="msg-header-close-button">×</span>\
                        </div>\
                        <div class="msg-body">\
                          <div class="msg-body-icon">\
                            <div class="msg-body-icon-info"></div>\
                          </div>\
                          <div class="msg-body-content"></div>\
                        </div>\
                        <div class="msg-footer">\
                          <button class="msg-footer-btn msg-footer-cancel-button">算了吧</button>\
                          <button class="msg-footer-btn msg-footer-confirm-button">好的</button>\
                        </div>';

        根据传入的参数控制content样式
        let contentDom=wrap.querySelector(".msg-body .msg-body-content");
        用解构赋值来合并对象
        const contentStyle={
            contentFontSize:.contentFontSize,....contentStyle
        }
        for(let i in contentStyle){
            如果是自身的属性
            if(contentStyle.hasOwnProperty(i)){
                style[i]==>style.i
                i是属性名,contentStyle[i]是属性值
                contentDom.style[i]=contentStyle[i];
            }
        }
        如果使用html
        if(.useHTML){
            contentDom.innerHTML=.content;
        }else{
            contentDom.innerText=.content;
        }

        let overlay=document.createElement("div");
        overlay.className="msg__overlay"this._el=wrap;
        this._overlay=overlay;
    }

    显示
    Msg.prototype._show=([el,overlay]){
        document.body.appendChild(el);
        document.body.appendChild(overlay);

        设置动画效果显示
        延迟实现异步效果
        setTimeout((){
            el.style.transform='translate(-50%,-50%) scale(1,1)';        
            overlay.style.opacity=1;    
        });

    }

    绑定事件
    Msg.prototype._bind=隐藏事件
        const hideMsg=;        
            overlay.style.opacity=0;

            动画结束后移除元素(css里设置的动画时间是300ms)
            setTimeout((){
                document.body.removeChild(el);
                document.body.removeChild(overlay);
            },300);            
        }

        取消事件
        const cancel=(e){
            如果传入了回调,则执行回调,参数是e
            _this.cancel && _this.cancel.call(._cancel,e);
            hideMsg();
        }

        确认事件
        const confirm=(e){
            _this.confirm && _this.confirm.call(_this.confirm,1)">点击遮罩
        overlay.addEventListener("click"点击右上角叉叉
        el.querySelector(".msg-header .msg-header-close-button").addEventListener("click"点击取消按钮
        el.querySelector(".msg-footer .msg-footer-cancel-button").addEventListener("click"点击确认按钮
        el.querySelector(".msg-footer .msg-footer-confirm-button").addEventListener("click"挂到window上可全局访问
    window.$Msg=Msg;

})(window,document);

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


原文连接:https://www.cnblogs.com/dupd/p/5951311.htmlES6之前已经出现了js模块加载的方案,最主要的是CommonJS和AMD规范。commonjs主要应用于服务器,实现同步加载,如nodejs。AMD规范应用于浏览器,如requirejs,为异步加载。同时还有CMD规范,为同步加载方案如seaJS。ES6在语言规格的层面上,实现了模块功能,而且...
以为Es6,javascript第一次支持了module。ES6的模块化分为导出(export)与导入(import)两个模块,其中在项目中,我们会经常看到一种用法import * as obj from,这种写法是把所有的输出包裹到obj对象里。示例一 1 2 3 4 5 6 7 // index.js export function fn1(data){ console.log(1) } export f.
视频讲解关于异步处理,ES5的回调使我们陷入地狱,ES6的Promise使我们脱离魔障,终于、ES7的async-await带我们走向光明。今天就来学习一下 async-await。async-await和Promise的关系经常会看到有了 async-await、promise 还有必要学习吗、async await优于promise的几个特点,接收了这些信息后,就蒙圈了。现在才知道...
TypeScript什么是TypeScript?TypeScript是由微软开发的一款开源的编程语言TypeScript是JavaScript的超集,遵循最新的ES5 ES6规范,TypeScript扩展了JavaScript的语法TypeScript更像后端 Java c# 这样的面向对象语言可以让js开发大型企业项目谷歌也在大力支持TypeScript的推广,React ,VUE 都集成了TypeScriptTypeScript安装安装-- 安装npm install -g type
export class AppComponent { title = 'Tour of heroes'; hero: Hero = { id: 1, name: '张三' };}export class Hero { id: number; name: string;}就是这一段,看起来有点晕,这里是实例化一个Hero类型的对象hero,还是创建一个变量?后面是赋值,但是不知道什么意思?hero: Hero = { id: 1, na.
用 async/await 来处理异步昨天看了一篇vue的教程,作者用async/ await来发送异步请求,从服务端获取数据,代码很简洁,同时async/await 已经被标准化,是时候学习一下了。先说一下async的用法,它作为一个关键字放到函数前面,用于表示函数是一个异步函数,因为async就是异步的意思, 异步函数也就意味着该函数的执行不会阻塞后面代码的执行。 写一个async 函数async function timeout() {return 'hello world'
es6的语法已经出了很长的时间了,在使用上也可以通过babel这类的编译工具转译为浏览器可以识别的es5的语法,但是依旧有很多开发在写代码的时候,依旧没有用es6的语法,而是习惯使用老的语法,这篇文章主要会介绍解构赋值基本用法以及在实际使用场景中相比es5语法的优势,让大家从根本上了解es6语法的优势基本用法数组解构让我们一起先来看数组解构的基本用法:let [a, b, c] ...
参考链接1 参考链接2写法1 - 使用 function 关键字function greeter(fn: (a: string) =&gt; void) { fn("Hello, World");}function printToConsole(s: string) { console.log(s);}greeter(printToConsole);(a: string) =&gt; void上述语法的含义:表示一个函数,接收一个字符串作为输入参数,没有返回参数。可
ES6简介-ECMAScript是javascript标准-ES6就是ECMAScript的第6个版本-ECMAScript6.0(以下简称ES6)是JavaScript语言的下一代标准,已经在2015年6月正式发布了。它的目标,是使得JavaScript语言可以用来编写复杂的大型应用程序,成为企业级开发语言。ES6新增加的功能:1.let
ES6  在ES5的基础上增加了一些新的特性和写法,特别在函数写法上,增加了箭头函数 1.正经的函数写法//普通的传递值的写法functionsum1(x,y){returnx+y;}constres=sum1(2,3);console.log(res);//传递对象的方式,调用时需要传递一个对象过去function
ES5及ES6es表示ECMASCript,他是从es3,es5,es6,es5是2009.12月发布的,es6是2015.6月发布的。vue2完全支持es5的(vue3完全支持es6的),react完全支持es6es5的新特性严格模式(对应的相反的称为怪异模式)'usestrict'//一般用于相关的设计上面书写一个严格模式底下的代码就需要按照严格模
ES5的严格模式所谓严格模式,从字面上就很好理解,即更严格的模式,在这种模式下执行,浏览器会对JS的要求更苛刻,语法格式要求更细致,更符合逻辑。怪异模式:就是我们之前一直使用的开发模式,就叫怪异模式。因为很多时候出来的结果是非常怪异的,所以才称之为怪异模式。'usestrict'//一般用
相同点export与exportdefault均可用于导出常量、函数、文件、模块可在其它文件或模块中通过import+(常量|函数|文件|模块)名的方式,将其导入,以便能够对其进行使用不同点一、在一个文件或模块中,export、import可以有多个,exportdefault仅有一个//model.jsle
24.class类 25.class中的extend 26.super关键字 27.super应用 28.class属性 30.静态成员和实例成员 31.构造函数问题 32.构造函数原型 33.原型链 34.js查找机制 35.原型对象中this指向 36.扩展内置对象方法 37.call方法 38.借用父构造函数
什么是ES6ES的全称是ECMAScript,它是由ECMA国际标准化组织,制定的一项脚本语言的标准化规范。泛指2015年发布的es2015版极其后续版本ES6中新增语法letES6中新增的用于声明变量的关键字。注意:使用let关键字声明的变量才具有块级作用域,使用var声明的变量不具备块级作用域特
命名修饰符let:不能重复声明变量、块级作用域leta=1;leta=2;//报错const:初始化常量,必须给初始值,否则报错、在同一个作用域内,const定义的常量不能修改其值、块级作用域consta=10a=100//报错,不能重复声明解构constobj={name:'jack'age:18sex:'
ES6新增了let命令,用来声明变量。它的用法类似于var,但是所声明的变量,只在let命令所在的代码块内有效。1{2leta=10;3varb=1;4}56a//ReferenceError:aisnotdefined.7b//1上面代码在代码块之中,分别用let和var声明了两个变量。然后在代码块之外调用
<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metaname="viewport"content="width=device-width,initial-scale=1.0"><metahttp-equiv="X-UA-Compatib
一,RegExp构造函数es5中,RegExp构造函数的参数有两种情况。1,参数是字符串,第二个参数表示正则表达式的修饰符(flag)。2,参数是一个正则表达式,返回一个原有正则表达式的拷贝。es6中,如果RegExp构造函数第一个参数是一个正则对象,那么可以使用第二个参数指定修饰符。而
一、字符的Unicode表示法JavaScript允许采用\uxxxx形式表示一个字符,其中xxxx表示字符的Unicode码点。表示法只限于码点在\u0000~\uFFFF之间的字符,超过该范围需要用两个双字节表示ES6改进:将码点放入大括号,就能正确解读该字符。转换参考:https://blog.csdn.net/hezh1994/ar