前端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 相关文章推荐
使用户点击后退按钮使效三行代码
Jul 07 Javascript
js取得html iframe中的元素和变量值
Jun 30 Javascript
jquery.cookie.js用法实例详解
Dec 25 Javascript
js简单判断flash是否加载完成的方法
Jun 21 Javascript
JS生成不重复的随机数组的简单实例
Jul 10 Javascript
详解Vue爬坑之vuex初识
Jun 14 Javascript
基于JavaScript实现无缝滚动效果
Jul 21 Javascript
解决Vue编译时写在style中的路径问题
Sep 21 Javascript
在vue里面设置全局变量或数据的方法
Mar 09 Javascript
vue.js使用watch监听路由变化的方法
Jul 08 Javascript
elementUI vue this.$confirm 和el-dialog 弹出框 移动 示例demo
Jul 03 Javascript
Vue学习之组件用法实例详解
Jan 06 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抽奖小程序的实现代码
2013/06/18 PHP
win7 64位系统 配置php最新版开发环境(php+Apache+mysql)
2014/08/15 PHP
如何让thinkphp在模型中自动完成session赋值小教程
2014/09/05 PHP
Yii2针对指定url的生成及图片等的引入方法小结
2016/07/18 PHP
Yii2.0多文件上传实例说明
2017/07/24 PHP
使用jQuery简化Ajax开发 Ajax开发入门
2009/10/14 Javascript
JavaScript 类的定义和引用 JavaScript高级培训 自定义对象
2010/04/27 Javascript
JQuery $.each遍历JavaScript数组对象实例
2014/09/01 Javascript
Jquery动态添加输入框的方法
2015/05/29 Javascript
jquery判断页面网址是否有效的两种方法
2016/12/11 Javascript
利用JS实现scroll自定义滚动效果详解
2017/10/17 Javascript
vue 的点击事件获取当前点击的元素方法
2018/09/15 Javascript
深入理解JS异步编程-Promise
2019/06/03 Javascript
javascript实现滚轮轮播图片
2020/12/13 Javascript
python集合用法实例分析
2015/05/30 Python
简单了解python模块概念
2018/01/11 Python
Python定义二叉树及4种遍历方法实例详解
2018/07/05 Python
python3安装crypto出错及解决方法
2019/07/30 Python
Pytest mark使用实例及原理解析
2020/02/22 Python
浅谈python输出列表元素的所有排列形式
2020/02/26 Python
Python  Asyncio模块实现的生产消费者模型的方法
2021/03/01 Python
css3绘制百度的小度熊
2018/10/29 HTML / CSS
全球知名旅游社区巴西站点:TripAdvisor巴西
2016/07/21 全球购物
美国孕妇装品牌:Destination Maternity
2018/02/04 全球购物
英国一家集合了众多有才华设计师品牌的奢侈店:Wolf & Badger
2018/04/18 全球购物
美国伴娘礼服商店:Evening Collective
2019/10/07 全球购物
Wiggle新西兰:自行车、跑步、游泳
2020/05/06 全球购物
幼儿园中秋节活动反思
2014/02/16 职场文书
中介公司区域经理岗位职责范本
2014/03/02 职场文书
排查整治工作方案
2014/06/09 职场文书
中职招生先进个人材料
2014/08/31 职场文书
2016年习主席讲话学习心得体会
2016/01/20 职场文书
导游词之山西关帝庙
2019/11/01 职场文书
PHP实现考试倒计时功能代码
2021/04/16 PHP
SQL优化老出错,那是你没弄明白MySQL解释计划用法
2021/11/27 MySQL
Apache Pulsar结合Hudi构建Lakehouse方案分析
2022/03/31 Servers