基於JQ的自定義彈窗組件

来源:https://www.cnblogs.com/wow-jerry/archive/2018/11/06/9914390.html
-Advertisement-
Play Games

window.onresize = function() { /* 預設設置字體為可視區/7.5 max:256px min */ document.documentElement.style.fontSize = document.documentElement.clientWidth / 7.5 ...


<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>基於JQ的自定義彈窗組件</title>

    <link rel="stylesheet" href="css/reset.css">
    <link rel="stylesheet" href="css/popup.css">
 </head>
  <style>
  a {display: inline-block;text-align: center;text-decoration: none;font-size:20px;background: #e5e5e5;}
  </style>
  <body> <div id="box"> <a href="javascript:;" class="btnModel">按鈕彈窗</a> <a href="javascript:;" class="tipModel">提示彈窗</a> <a href="javascript:;" class="loadModel">載入彈窗</a> <input type="submit" value="提交"> </div> </body>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script src="js/rem.js"></script>
<script src="js/popup.js"></script>
<script>
$(function () {

//按鈕彈窗
$(".btnModel").on("click", function() {
       var popupModel = new Popup();
            popupModel.init({
title : '你好,我是標題',
tipText : '我是文本內容',
showBtn : true,
showDouble : true,
success : function (res) {
console.log(res);
if(res.confirm){
console.log('用戶點擊了確認' + JSON.stringify(res));
}else{
console.log('用戶點擊了取消' + JSON.stringify(res));
}
}
});
});
//提示彈窗
$(".tipModel").on("click", function() {
var popupModel = new Popup();
popupModel.init({
title : '提示彈窗標題',
tipText : '提示彈窗文本'
});
setTimeout(function () {
popupModel.close();
}, 3000)
});
//載入彈窗
$(".loadModel").on("click", function() {
var popupModel = new Popup();
popupModel.init({
loadImg: './img/load.gif'
});
setTimeout(function () {
popupModel.close();
}, 1500)
});
})
</script>
</html>
window.onresize = function() {
    /*
        預設設置字體為可視區/7.5 -----max:256px min
    */
    document.documentElement.style.fontSize = document.documentElement.clientWidth / 7.5 + 'px';
    
    var deviceWidth = document.documentElement.clientWidth;
    
    if (deviceWidth > 640) {
        deviceWidth = 640
    }
};
onresize();
rem.js
@charset "UTF-8";

body,div,dl,dt,dd,ul,li,pre,form,fieldset,select,input,textarea,button,p,img,iframe{  margin: 0;  padding: 0;  }
h1,h2,h3,h4,h5,h6{ font-weight:normal; margin: 0; padding: 0; }
body{  width: 100%;  font-family: "microsoft yahei","SimSun","宋體";  background-color: #fff; -webkit-overflow-scrolling: touch;overflow-scrolling: touch; }
/* 重置button邊框 */
button{ border: none; }
/* 去掉列表前的標識, li 會繼承 */
ol,ul{list-style: none;}
/* 讓鏈接預設不顯示下劃線*/
a{cursor: pointer;text-decoration: none;}
/* 清理浮動 */
.clearfix:before,.clearfix:after{ display: block;  content: " ";  clear: both;  }
/* for ie67*/
.clearfix{zoom: 1;}
/* HTML5 媒體文件跟 img 保持一致 */
audio,canvas,video{  display: inline-block;  *display: inline;  *zoom: 1;  }
address,caption,cite,code,dfn,em,th{  font-style: normal;  font-weight: normal;  }
.box-sizing{  -moz-box-sizing: border-box;  -webkit-box-sizing: border-box;  -o-box-sizing: border-box;  -ms-box-sizing: border-box;  box-sizing: border-box;  }
/*p{ text-align:justify; text-justify:distribute;}*/
div, p, span {  text-overflow: ellipsis;  word-break: break-all;  word-wrap: break-word;  }
/*iphone及ipad下輸入框預設內陰影*/
input, button {  outline: none;  -webkit-appearance: none;  }
textarea {  resize: none;  outline: none;  }
img {  vertical-align: middle;  border: 0;  width: 100%;  }
a:active, a:hover {  outline: 0;  }
/*ios和android下觸摸元素時出現半透明灰色遮罩*/
a, input {  border: none;  outline: none;  -webkit-tap-highlight-color: rgba(255, 255, 255, 0);  }
input[type=button] {  cursor: pointer;  }
input[type=submit] {  cursor: pointer;  }
reset.css
@charset "UTF-8";
/*
   彈窗樣式class --alert_Wrap 儘量不要修改樣式名稱,如若修改,JS文件中class也要做對應修改
*/
/*遮罩層*/
.alert_Wrap{
    position: fixed;
    width: 100%;
    height: 100%;
    left: 0;
    top: 0;
    background: rgba(0,0,0,0.7);
    z-index: 99;
    text-align: center
}
/*彈窗盒子*/
.alert_Wrap .alertCont{
    position: absolute;
    border-radius: 5px;
    text-align: center;
    width: 5.3rem;
    background: white;
    top:35%;
    left:1.1rem;
    box-sizing: border-box;
}
/*載入彈窗中的彈窗盒子*/
.alert_Wrap .alertLoad{
    background: rgba(0,0,0,0);
}
/*彈窗標題*/
.alert_Wrap .alertTitle{
    font-size: 0.28rem;
    color: black;
    text-align: center;
    padding: 0.2rem;
    word-wrap: break-word;
}
/*彈窗文本*/
.alert_Wrap .alertText{
    font-size: 0.25rem;
    color: black;
    text-align: center;
    padding-bottom: 0.15rem;
    word-wrap: break-word;
}
/*彈窗文本域*/
.alert_Wrap .alertArea{
    font-size: 0.28rem;
    color: black;
    text-align: center;
    padding-top: 0.1rem;
    padding-bottom: 0.15rem;
    word-wrap: break-word;
}
/*彈窗按鈕*/
.alert_Wrap .alertBtn{
    width: 100%;
    background: white;
    text-align: center;
    border-bottom-right-radius: 5px;
    border-bottom-left-radius: 5px;
    height: auto;
}
.alert_Wrap .alertBtn input{
    width: 50%;
    float: left;
    background: whitesmoke;
    color: black;
    cursor: pointer;
    padding: 0.2rem;
    font-size: 0.28rem;
}
/*取消按鈕*/
.alert_Wrap .alertBtn .cancel_btn{
    border-right: 1px solid #d3cdcd;
    border-top: 1px solid #d3cdcd;
    border-bottom-left-radius: 5px;
}
/*確認按鈕*/
.alert_Wrap .alertBtn .alert_confirm_btn{
    border-top: 1px solid #d3cdcd;
    border-bottom-right-radius: 5px;
    color: rgb(0, 174, 255);
}
.alert_Wrap .alertBtn .confirm_int{
    width: 100%;
    border-bottom-right-radius: 5px;
    border-bottom-left-radius: 5px;
}
/*載入圖片*/
.alert_Wrap .load_img{
    width: 40px;
    height: 40px;
    position: absolute;
    left: 50%;
    top:50%;
    margin-left: -20px;
    margin-top: -20px;
}
@media screen and (max-width:321px) {
    html,
    body {
        font-size: 14px;
        line-height: 18px;
    }
    .alert_Wrap .alertInput{
        margin-left: 0.03rem;
    }
}
@media screen and (min-width:321px) and (max-width:400px) {
    html,
    body {
        font-size: 16px;
        line-height: 22px;
    }
    .alert_Wrap .alertInput{
        margin-left: 0.03rem;
    }
}
@media screen and (min-width:400px) {
    html,
    body {
        font-size: 18px;
        line-height: 28px;
    }
    .alert_Wrap .alertInput{
        margin-left: 0.04rem;
    }
}
@media screen and (min-width:640px){
    html,body {
        font-size: 18px;
        line-height: 28px;
    }
    .alert_Wrap .alertInput{
        margin-left: 0.05rem;
    }
}
popup.css
/*
    @description --model:1.btn 2.tip 3.load
    @param --setting --存放配置 $.extend()  外部配置可以覆蓋預設配置

*/
function Popup() {
    this.alertWrap = null;//遮罩層
    this.alertCont = null;//彈窗盒子
    this.result = {}; //消息存放
    //預設配置參數
    this.setting = {
        // title:'標題',
        // tipText:'說明文字',
        // textArea : '額外添加區域',
        // loadImg : '載入圖片url',
        showBtn: false,   //true 顯示按鈕 false 隱藏 (預設沒有按鈕)
        showDouble: false,  //true 雙按鈕 false 單按鈕
        cancelText: '取消',//預設,外部定義可覆蓋
        confirmText: '確認'
    }
}
Popup.prototype.init = function (options) {
    $.extend(this.setting, options); //預設配置與自定義配置
    this.mark();
    this.create();
    this.dir();
};
//遮罩
Popup.prototype.mark = function () {
    this.alertWrap = $("<div class='alert_Wrap'></div>");
};
//彈窗
Popup.prototype.create = function () {
    this.alertCont = $("<div class='alertCont'></div>");
    //title
    if (this.setting.title) {
        this.alertTitle = $("<div class='alertTitle'>" + this.setting.title + "</div>");
        this.alertCont.append(this.alertTitle);
    }
    //tipText
    if (this.setting.tipText) {
        this.alertText = $("<div class='alertText'>" + this.setting.tipText + "</div>");
        this.alertCont.append(this.alertText);
    }
    //textArea
    if (this.setting.textArea) {
        this.alertArea = $("<div class='alertArea'>" + this.setting.textArea + "</div>");
        this.alertCont.append(this.alertArea);
    }
    //loadImg
    if (this.setting.loadImg) {
        this.alertCont.addClass("alertLoad");
        this.alertLoadImg = $("<img class='load_img' src='" + this.setting.loadImg + "' />");
        this.alertCont.append(this.alertLoadImg);
    }
    //showBtn
    if (this.setting.showBtn && this.setting.showDouble === true) {
        //
        this.alertBtn = $("<div class='alertBtn clearfix'></div>");
        this.cancel_btn = $("<input class='cancel_btn' type='button' value=" + this.setting.cancelText + ">");
        this.confirm_btn = $("<input class='alert_confirm_btn' type='button' value=" + this.setting.confirmText + ">");
        this.alertBtn.append(this.cancel_btn);
        this.alertBtn.append(this.confirm_btn);
        this.alertCont.append(this.alertBtn);
    } else if (this.setting.showBtn && this.setting.showDouble === false) {
        //
        this.alertBtn = $("<div class='alertBtn'></div>");
        this.confirm_btn = $("<input class='alert_confirm_btn confirm_int' type='button' value=" + this.setting.confirmText + ">");
        this.alertBtn.append(this.confirm_btn);
        this.alertCont.append(this.alertBtn);
    }
    this.alertWrap.append(this.alertCont);
    $("body").append(this.alertWrap);
};
//自定義函數
Popup.prototype.dir = function () {
    var _this = this;
    if (this.cancel_btn) {
        this.cancel_btn.on("click", function () {
            _this.alertWrap.remove();
            _this.result = {
                cancel: true,
                showCancel: "取消操作!"
            };
            if (_this.setting.success) {
                _this.setting.success(_this.result);
            }
        })
    }
    if (this.confirm_btn) {
        this.confirm_btn.on("click", function () {
            _this.alertWrap.remove();
            _this.result = {
                confirm: true,
                showCancel: "確認操作!"
            };
            if (_this.setting.success) {
                _this.setting.success(_this.result);
            }
        })
    }
};
//關閉
Popup.prototype.close = function () {
    this.alertWrap.remove();
};
popup.js

 

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 今天發現了一種ajax上傳圖片的方式,是以前沒有用過的,首先來說下為什麼要用這種方式。是因為原來後臺是用的form表單的方式來提交表單數據的。但是覺得呢,這種方式不太好,因為要刷新頁面,前臺只用控制台看不到傳輸的參數(如果哪位大神知道怎麼看到,請不吝賜教),所以我們就改成了ajax的方式,結果後臺老 ...
  • 第1章 伺服器端渲染基礎本章主要講解客戶端與伺服器端渲染的概念,分析客戶端渲染和伺服器端渲染的利弊,帶大家對伺服器端渲染有一個粗淺認識。1-1 課程導學1-2 什麼是伺服器端渲染1-3 什麼是客戶端渲染1-4 React 客戶端渲染的優勢與弊端第2章 React中的伺服器端渲染本章將藉助Node.j ...
  • 1.手機郵箱正則 近兩年出來很多新號碼,聽說199什麼的都有了- -導致以前的正則不能用了....這就很難過,總是過一段時間出一種新號碼。因此,我決定使用返樸歸真的手機正則。 手機正則:var reg=/^1[0-9]\d{9}$/; 郵箱正則:var mailReg = /^[a-zA-Z0-9_ ...
  • 準備工具 ====== "Node.js" Angular requires Node.js version 8.x or 10.x. 查看當前你的node版本可以在CMD中輸入 開發工具強烈推薦 "visual studio Code" 安裝 agular_cli 新建工程 用CLI命令建立 ng ...
  • 微信小程式組件里沒有下拉框,正好要用到,記下來以後參考 wxml代碼 wxss代碼 js代碼 效果 ...
  • 這篇文章介紹了Vue.js 父子組件之間通信的十種方式,不管是初學者還是已經在用 Vue 的開發者都會有所收穫。無可否認,現在無論大廠還是小廠都已經用上了 Vue.js 框架,簡單易上手不說,教程詳盡,社區活躍,第三方套件還多。真的是前端開發人員必備技能。而且在面試當中也往往會問到關於 Vue 方面... ...
  • ** 創建string對象 *** var str ="acb"; ** 方法和屬性 ** 屬性 length:字元串的長度 ** 方法 (1)與HTML相關的方法 - bold():加粗 - fontcolor():設置字體的顏色 - fontsize():設置字體的大小(1-7) - link( ...
  • 看到掘金上翻出一個老梗,前端如何不帶髒字得罵產品經理傻逼(sb),複製(!(~+[]) + {})[--[~+""][+[]] * [~+[]] + ~~!+[]] + ({} + [])[[~!+[]] * ~+[]]到瀏覽器控制台回車,看到輸出sb二字。 原理很簡單,就是取字元串里的字母進行拼湊 ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...