前端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 相关文章推荐
jquery 上下滚动广告
Jun 17 Javascript
jQuery+html5实现div弹出层并遮罩背景
Apr 15 Javascript
JavaScript使用DeviceOne开发实战(一) 配置和起步
Dec 01 Javascript
基于jQuery实现火焰灯效果导航菜单
Jan 04 Javascript
对于js垃圾回收机制的理解
Sep 14 Javascript
微信小程序使用slider设置数据值及switch开关组件功能【附源码下载】
Dec 09 Javascript
js数组常用最重要的方法
Feb 04 Javascript
jquery层次选择器的介绍
Jan 18 jQuery
vue 实现小程序或商品秒杀倒计时
Apr 14 Javascript
Layui带搜索的下拉框的使用以及动态数据绑定方法
Sep 28 Javascript
解决vue axios跨域 Request Method: OPTIONS问题(预检请求)
Aug 14 Javascript
React 条件渲染最佳实践小结(7种)
Sep 27 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
如何在PHP中使用Oracle数据库(1)
2006/10/09 PHP
PHP 输出简单动态WAP页面
2009/06/09 PHP
php 生成文字png图片的代码
2011/04/17 PHP
PHP定时更新程序设计思路分享
2014/06/10 PHP
Centos7 Yum安装PHP7.2流程教程详解
2019/07/02 PHP
JavaScript的eval JSON object问题
2009/11/15 Javascript
需要做特殊处理的DOM元素属性的访问
2010/11/05 Javascript
jQuery Ajax使用 全解析
2010/12/15 Javascript
js 弹出菜单/窗口效果
2011/10/30 Javascript
jquery ajax例子返回值详解
2012/09/11 Javascript
JS获取页面input控件中所有text控件并追加样式属性
2013/02/25 Javascript
使用jQuery时Form表单元素ID和name命名大忌
2014/03/06 Javascript
AngularJS + Node.js + MongoDB开发的基于高德地图位置的通讯录
2015/01/02 Javascript
JavaScript中Textarea滚动条不能拖动的解决方法
2015/12/15 Javascript
一款简单的jQuery图片标注效果附源码下载
2016/03/22 Javascript
vue获取input输入值的问题解决办法
2017/10/17 Javascript
不得不知的ES6小技巧
2018/07/28 Javascript
使用Three.js实现太阳系八大行星的自转公转示例代码
2019/04/09 Javascript
微信小程序实现的canvas合成图片功能示例
2019/05/03 Javascript
Nodejs环境实现socket通信过程解析
2020/07/03 NodeJs
使用python编写脚本获取手机当前应用apk的信息
2014/07/21 Python
Python之父谈Python的未来形式
2016/07/01 Python
Python之Scrapy爬虫框架安装及使用详解
2017/11/16 Python
python学生管理系统
2019/01/30 Python
Django实现单用户登录的方法示例
2019/03/28 Python
Python画图高斯分布的示例
2019/07/10 Python
米兰必去买手店排行榜首位:Antonioli
2016/09/11 全球购物
暑期实习鉴定
2013/12/16 职场文书
数控机械专业个人的自我评价
2014/01/02 职场文书
四好少年事迹材料
2014/01/12 职场文书
工作疏忽检讨书
2014/01/25 职场文书
2014年五一劳动节社区活动总结
2014/04/14 职场文书
工作经验交流材料
2014/12/30 职场文书
2015年教师个人业务工作总结
2015/10/23 职场文书
考研经验交流会策划书
2015/11/02 职场文书
学校教代会开幕词
2016/03/04 职场文书