前端js弹出框组件使用方法


Posted in Javascript onAugust 24, 2020

下面分享一个js 弹出窗, 分 toast , dialog , load 三种弹窗 , 下面用js css 来实现以下:

首先是js代码 | 采用了 es6 的写法

//公共弹窗加载动画

const DIALOG_TOAST = '1',
 DIALOG_DIALOG = '2',
 DIALOG_LOAD = '3',

class Dialog {

 constructor(type = DIALOG_DIALOG,
  dialogContent = '请求失败',
  wrapClassName = 'common-dialog-wrap',
  dialogWrapClassName = 'common-dialog-content-wrap',
  contentClassName = 'common-dialog-content',
  btnClassName = 'common-btn',
  btnContent = '确定') {


 this.type = type;

 //吐司
 if (type == DIALOG_TOAST) {
  this.dialog = document.createElement('div');
  this.dialog.className = 'common-toast';
  this.dialog.innerHTML = dialogContent;

 }
 //加载动画
 else if (type == DIALOG_LOAD) {
  this.dialog = document.createElement('div');
  this.dialog.className = wrapClassName;
  this.figure = document.createElement('figure');
  this.figure.className = 'common-loadGif';
  this.img = document.createElement('img');
  this.img.src = getAbsolutePath() + '/fenqihui/static/bitmap/travel/loadgif.gif';
  this.figure.appendChild(this.img);
  this.dialog.appendChild(this.figure);

 } else if (type == DIALOG_DIALOG) {
  this.dialog = document.createElement('div');
  this.dialog.className = wrapClassName;
  this.dialogWrap = document.createElement('div');
  this.dialogWrap.className = dialogWrapClassName;
  this.conetent = document.createElement('p');
  this.conetent.innerHTML = dialogContent;
  this.conetent.className = contentClassName;
  this.confirmButton = document.createElement('p');
  this.confirmButton.innerHTML = btnContent;
  this.confirmButton.className = btnClassName;
  this.dialogWrap.appendChild(this.conetent);
  this.dialogWrap.appendChild(this.confirmButton);
  this.dialog.appendChild(this.dialogWrap);
  this.bindEvent();
 }


 }


 bindEvent() {
 this.confirmButton.addEventListener('click', ()=> {
  this.hide();
 })
 }

 show(time) {
 document.querySelector('body').appendChild(this.dialog);
 $(this.dialog).css('display', 'block');

 if (this.type == DIALOG_TOAST) {

  setTimeout(()=> {
  $(this.dialog).css('display', 'none');
  }, time);
 }

 }

 hide() {
 $(this.dialog).css('display', 'none');
 }

}

css 文件如下:

/*公共弹窗*/
.common-dialog-wrap {
 position: fixed;
 background: rgba(0,0,0,.7);
 z-index: 100;
 height: 100%;
 width: 100%;
 top: 0;
}

.common-dialog-content {
 height: 2rem;
 border-bottom: 1px solid #ccc7c7;
 line-height: 2rem;
 text-align: center;
 font-size: .65rem;
}

.common-btn {
 text-align: center;
 height: 2rem;
 color: orange;
 line-height: 2rem;
}

.common-dialog-content-wrap{
 background: #fff;
 width: 10rem;
 height: 4rem;
 border-radius: 5px;
 position: fixed;
 left: 0;
 top:0;
 right: 0;
 bottom: 0;
 margin: auto;
}

/*吐司样式*/
.common-toast{
 height: 1.6rem;
 width: 4rem;
 box-sizing: content-box;
 color: #fff;
 padding: 0 10px;
 position: fixed;
 left: 0;
 top:0;
 bottom: 0;
 right: 0;
 text-align: center;
 line-height: 1.6rem;
 margin: auto;
 background: rgba(0,0,0,.7);
 border-radius: 2rem;
}

.common-loadGif{
 height: 4rem;
 width: 4rem;
 position: fixed;
 top:0;
 left: 0;
 bottom: 0;
 right: 0;
 margin: auto;
}

.common-loadGif img{
 height: 100%;
 width: 100%;
 border-radius: 10px;
}

使用方式

new Dialog(DIALOG_DIALOG).show() | hide()
new Dialog(DIALOG_LOAD).show() | hide()
new Dialog(DIALOG_TOAST).show(time : number) | hide()

效果如下

前端js弹出框组件使用方法

前端js弹出框组件使用方法

前端js弹出框组件使用方法

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
JavaScript 常用函数
Dec 30 Javascript
JS 弹出层 定位至屏幕居中示例
May 21 Javascript
js 加密压缩出现bug解决方案
Nov 25 Javascript
JavaScript 异常处理 详解
Feb 06 Javascript
JavaScript的面向对象编程基础
Aug 13 Javascript
Bootstrap每天必学之导航条(二)
Mar 01 Javascript
JQuery ZTree使用方法详解
Jan 07 Javascript
Node.js 8 中的重要新特性
Jun 28 Javascript
jQuery中extend函数简单用法示例
Oct 11 jQuery
基于Vue、Vuex、Vue-router实现的购物商城(原生切换动画)效果
Jan 09 Javascript
AngularJS下$http服务Post方法传递json参数的实例
Mar 29 Javascript
深入了解Vue.js 混入(mixins)
Jul 23 Javascript
完美解决JS文件页面加载时的阻塞问题
Dec 18 #Javascript
教你一步步用jQyery实现轮播器
Dec 18 #Javascript
Angular.js实现注册系统的实例详解
Dec 18 #Javascript
无阻塞加载js,防止因js加载不了影响页面显示的问题
Dec 18 #Javascript
Node.js连接postgreSQL并进行数据操作
Dec 18 #Javascript
纯js实现悬浮按钮组件
Dec 17 #Javascript
Jquery Easyui搜索框组件SearchBox使用详解(19)
Dec 17 #Javascript
You might like
世界咖啡生产者论坛呼吁:需要立即就咖啡价格采取认真行动
2021/03/06 咖啡文化
php日期转时间戳,指定日期转换成时间戳
2012/07/17 PHP
Smarty中的注释和截断功能介绍
2015/04/09 PHP
PHP使用递归方式列出当前目录下所有文件的方法
2015/06/02 PHP
jquery调用asp.net 页面后台的实现代码
2011/04/27 Javascript
JS实现模仿微博发布效果实例代码
2013/12/16 Javascript
浅析jQuery中调用ajax方法时在不同浏览器中遇到的问题
2014/06/11 Javascript
浅谈javascript 函数内部属性
2015/01/21 Javascript
jquery+javascript编写国籍控件
2015/02/12 Javascript
JavaScript暂停和继续定时器的实现方法
2016/07/18 Javascript
jQuery实现简洁的轮播图效果实例
2016/09/07 Javascript
实例解析angularjs的filter过滤器
2016/12/14 Javascript
使用JavaScript触发过渡效果的方法
2017/01/19 Javascript
js数组方法reduce经典用法代码分享
2018/01/07 Javascript
MUI 实现侧滑菜单及其主体部分上下滑动的方法
2018/01/25 Javascript
js中getBoundingClientRect的作用及兼容方案详解
2018/02/01 Javascript
Vue.js单向绑定和双向绑定实例分析
2018/08/14 Javascript
JS中DOM元素的attribute与property属性示例详解
2018/09/04 Javascript
vue 设置 input 为不可以编辑的实现方法
2019/09/19 Javascript
[45:14]Optic vs VP 2018国际邀请赛淘汰赛BO3 第二场 8.24
2018/08/25 DOTA
Pycharm技巧之代码跳转该如何回退
2017/07/16 Python
django中静态文件配置static的方法
2018/05/20 Python
Python实现分段线性插值
2018/12/17 Python
解决在Python编辑器pycharm中程序run正常debug错误的问题
2019/01/17 Python
Python3.5装饰器典型案例分析
2019/04/30 Python
HTML5基于flash实现播放RTMP协议视频的示例代码
2020/12/04 HTML / CSS
Vans奥地利官方网站:美国原创极限运动潮牌
2018/09/30 全球购物
竟聘演讲稿范文
2013/12/31 职场文书
黄河象教学反思
2014/02/10 职场文书
新学期国旗下演讲稿
2014/05/08 职场文书
英语专业毕业生求职信
2014/05/24 职场文书
工程承包协议书
2014/10/20 职场文书
小孩不笨观后感
2015/06/03 职场文书
花木兰观后感
2015/06/10 职场文书
Python编程根据字典列表相同键的值进行合并
2021/10/05 Python
mysql timestamp比较查询遇到的坑及解决
2021/11/27 MySQL