码迷,mamicode.com
首页 > 其他好文 > 详细

基于JQ的自定义弹窗组件

时间:2018-11-06 13:42:23      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:没有   move   code   appear   splay   阴影   额外   col   phone   

<!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 () {
var popupModel = new Popup();
//按钮弹窗
$(".btnModel").on("click", function() {
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

 

 

基于JQ的自定义弹窗组件

标签:没有   move   code   appear   splay   阴影   额外   col   phone   

原文地址:https://www.cnblogs.com/wow-jerry/p/9914390.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!