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

下面分享一个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弹出框
前端组件化、前端组件开发、前端组件库、前端组件化开发、前端组件,以便于您获取更多的相关知识。

时间: 2024-09-20 01:13:44

前端js弹出框组件使用方法_javascript技巧的相关文章

JS实现从网页顶部掉下弹出层效果的方法_javascript技巧

本文实例讲述了JS实现从网页顶部掉下弹出层效果的方法.分享给大家供大家参考.具体如下: 这里介绍的JavaScript动画背景出层,实现从网页顶部掉下来的感觉,停止时还带有缓冲弹跳的意思,从上到下显示的动画弹出层,带关闭功能,没有怎么美化,喜欢的用时候息动手美化下,觉得不错. 运行效果如下图所示: 具体代码如下: <HTML><HEAD><TITLE>窗口从上掉下来</TITLE> </HEAD> <BODY bgColor=#fef4d9

JS实现仿中关村论坛评分后弹出提示效果的方法_javascript技巧

本文实例讲述了JS实现仿中关村论坛评分后弹出提示效果的方法.分享给大家供大家参考.具体实现方法如下: 复制代码 代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/x

fckeditor粘贴Word时弹出窗口取消的方法_javascript技巧

本文实例讲述了fckeditor粘贴Word时弹出窗口取消的方法.分享给大家供大家参考.具体方法如下: 用fckeditor作为用户发布的编辑框,允许用户发布Word.默认的情况下,粘取进word时,提示是否清除word样式,选择"是"则弹出一个框,需要再粘贴一次来清除word样式.这个操作很麻烦,因此要取消. 开始找到的方法是把ckeditor/" target="_blank">fckeditoreditorjs下的fckeditorcode_i

js实现文本框选中的方法_javascript技巧

本文实例讲述了js实现文本框选中的方法.分享给大家供大家参考.具体如下: 这段javascript代码可解决文本框获得焦点,即使得文本框的内容被选中. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www

js的alert弹出框出现乱码解决方案_javascript技巧

我的页面是: 复制代码 代码如下: <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 在页面中调用了另一个function.php文件里面的一个函数, 这个函数里面的代码是: cho '<SCRIPT language=Javascript>alert("感谢参与!您的10次投票机会已经全部用完!");</script>'

javascript实现在网页任意处点左键弹出隐藏菜单的方法_javascript技巧

在网页的任意处点击左键就会弹出来,实际上是用JS控制DIV层的显示,DIV层内嵌套的是一个表格,表格里放菜单,这样修改方便,不会CSS也没关系,你可以把它复制下来,用DW打开重新修改一下菜单样式,换成你自己喜欢的色彩 <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>隐藏菜单</ti

js弹出确认是否删除对话框_javascript技巧

复制代码 代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv=&qu

js 弹出框插件实例与代码使用方法

js 弹出框插件实例与代码使用方法 使用方法: 1. 调用jquery库以及zxxbox插件文件,如下代码: <script type="text/网页特效" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script><script type="text/javascript" src="/study/js/jq

js弹出框、对话框、提示框、弹窗实现方法总结(推荐)_javascript技巧

一.JS的三种最常见的对话框 //====================== JS最常用三种弹出对话框 ======================== //弹出对话框并输出一段提示信息 function ale() { //弹出一个对话框 alert("提示信息!"); } //弹出一个询问框,有确定和取消按钮 function firm() { //利用对话框返回的值 (true 或者 false) if (confirm("你确定提交吗?")) { aler