当前位置: 首页 > news >正文

jquery之弹框

jquery实现弹框脚本的封装,

(function() {
    $.MsgBox = {
        Alert: function(title, msg) {
            GenerateHtml("alert", title, msg);
            btnOk(); //alert只是弹出消息,因此没必要用到回调函数callback
            btnNo();
        },
        Confirm: function(title, msg, callback) {
            GenerateHtml("confirm", title, msg);
            btnOk(callback);
            btnNo();
        }
    }
    //生成Html
    var GenerateHtml = function(type, title, msg) {
        var _html = "";
        _html += '<div id="mb_box"></div><div id="mb_con"><span id="mb_tit">' + title + '</span>';
        _html += '<a id="mb_ico">x</a><div id="mb_msg">' + msg + '</div><div id="mb_btnbox">';
        if (type == "alert") {
            _html += '<input id="mb_btn_ok" type="button" value="确定" />';
        }
        if (type == "confirm") {
            _html += '<input id="mb_btn_ok" type="button" value="确定" />';
            _html += '<input id="mb_btn_no" type="button" value="取消" />';
        }
        _html += '</div></div>';
        //必须先将_html添加到body,再设置Css样式
        $("body").append(_html);
        //生成Css
        GenerateCss();
    }

    //生成Css
    var GenerateCss = function() {
        $("#mb_box").css({
            width: '100%',
            height: '100%',
            zIndex: '99999',
            position: 'fixed',
            filter: 'Alpha(opacity=60)',
            backgroundColor: 'black',
            top: '0',
            left: '0',
            opacity: '0.6'
        });
        $("#mb_con").css({
            zIndex: '999999',
            width: '300px',
            position: 'fixed',
            backgroundColor: 'White',
            borderRadius: '5px'
        });
        $("#mb_tit").css({
            display: 'block',
            fontSize: '.4rem',
            color: '#444',
            padding: '10px 15px',
            textAlign: 'center',
            fontWeight: 'bold'
        });
        $("#mb_msg").css({
            padding: '20px',
            lineHeight: '20px',
            textAlign: 'center',
            borderBottom: '1px dashed #DDD',
            fontSize: '13px'
        });
        $("#mb_ico").css({
            display: 'block',
            position: 'absolute',
            right: '10px',
            top: '9px',
            border: '1px solid Gray',
            width: '18px',
            height: '18px',
            textAlign: 'center',
            lineHeight: '16px',
            cursor: 'pointer',
            borderRadius: '12px',
            fontFamily: '微软雅黑'
        });
        $("#mb_btnbox").css({
            margin: '15px 0 10px 0',
            textAlign: 'center'
        });
        $("#mb_btn_ok,#mb_btn_no").css({
            width: '2.85rem',
            height: '.84rem',
            lineHeight: '.84rem',
            fontSize: '.31rem',
            color: '#1aad19',
            border: 'none',
            backgroundColor: 'white'
        });
        $("#mb_btn_no").css({
            marginLeft: '20px'
        });
        //右上角关闭按钮hover样式
        $("#mb_ico").hover(function() {
            $(this).css({
                backgroundColor: 'Red',
                color: 'White'
            });
        }, function() {
            $(this).css({
                backgroundColor: '#DDD',
                color: 'black'
            });
        });
        var _widht = document.documentElement.clientWidth; //屏幕宽
        var _height = document.documentElement.clientHeight; //屏幕高
        var boxWidth = $("#mb_con").width();
        var boxHeight = $("#mb_con").height();
        //让提示框居中
        $("#mb_con").css({
            top: (_height - boxHeight) / 2 + "px",
            left: (_widht - boxWidth) / 2 + "px"
        });
    }
    //确定按钮事件
    var btnOk = function(callback) {
        $("#mb_btn_ok").click(function() {
            $("#mb_box,#mb_con").remove();
            if (typeof(callback) == 'function') {
                callback();
            }
        });
    }
    //取消按钮事件
    var btnNo = function() {
        $("#mb_btn_no,#mb_ico").click(function() {
            $("#mb_box,#mb_con").remove();
        });
    }
})();

使用示例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <!-- 手机端适配 -->
	<meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no;" name="viewport" />
    <!-- 必须依赖jquery -->
	<script type="text/javascript" src="js/jquery-3.1.0.min.js"></script>
	<script type="text/javascript" src="js/msgBox.js"></script>
</head>
<body>
	<input id="add" type="button" value="添加">
	<input id="update" type="button" value="修改">
	
	
	<script type="text/javascript">
		$(function(){
			$("#add").bind("click", function() {
				$.MsgBox.Alert("提示", "哈哈,添加成功!");
			});

			//回调函数可以直接写方法function(){alert("你点击了确定,重新进行认证");}
			function test() {
				alert("你点击了确定,重新进行认证");
			}
			//也可以传方法名 test
			$("#update").bind("click", function() {
				$.MsgBox.Confirm("温馨提示", "确定要进行修改吗?", test);
			});
		});
	</script>
</body>
</html>

页面显示

image

相关文章:

  • Python3.7上安装Django1.11启动报错
  • Curler 超简单免费网站监测工具,30秒轻松完成相关设定
  • Jquery实现简单的点击展开和收起
  • CenOS 7下安装jdk 8
  • CentOS 7 上安装Nginx
  • Intellij IDEA 2018用快捷键自动生成序列化id
  • Vue packags version mismatch
  • Spring Boot发送邮件(使用Thymeleaf模板)
  • Android SDK 的安装与配置
  • Java做selenium自动化测试时,关于guava的报错
  • APPIUM自动化测试之APP启动
  • appium之元素定位(uiautomatorviewer)
  • 使用Python 3 将txt文件转成csv文件
  • 使用Java的IO流拼接SQL语句
  • 多线程run方法获取不到@Autowired注入bean对象
  • 「面试题」如何实现一个圣杯布局?
  • 【Redis学习笔记】2018-06-28 redis命令源码学习1
  • 230. Kth Smallest Element in a BST
  • express如何解决request entity too large问题
  • golang中接口赋值与方法集
  • isset在php5.6-和php7.0+的一些差异
  • JS实现简单的MVC模式开发小游戏
  • Netty+SpringBoot+FastDFS+Html5实现聊天App(六)
  • Python - 闭包Closure
  • Rancher如何对接Ceph-RBD块存储
  • STAR法则
  • 官方解决所有 npm 全局安装权限问题
  • 老板让我十分钟上手nx-admin
  • 硬币翻转问题,区间操作
  • 云大使推广中的常见热门问题
  • AI又要和人类“对打”,Deepmind宣布《星战Ⅱ》即将开始 ...
  • ​2021半年盘点,不想你错过的重磅新书
  • ​secrets --- 生成管理密码的安全随机数​
  • #includecmath
  • #我与Java虚拟机的故事#连载07:我放弃了对JVM的进一步学习
  • #预处理和函数的对比以及条件编译
  • $.extend({},旧的,新的);合并对象,后面的覆盖前面的
  • $HTTP_POST_VARS['']和$_POST['']的区别
  • (第一天)包装对象、作用域、创建对象
  • (附源码)spring boot网络空间安全实验教学示范中心网站 毕业设计 111454
  • (附源码)springboot“微印象”在线打印预约系统 毕业设计 061642
  • (六) ES6 新特性 —— 迭代器(iterator)
  • (十一)JAVA springboot ssm b2b2c多用户商城系统源码:服务网关Zuul高级篇
  • (一)kafka实战——kafka源码编译启动
  • (转)原始图像数据和PDF中的图像数据
  • *2 echo、printf、mkdir命令的应用
  • .htaccess 强制https 单独排除某个目录
  • .Net 知识杂记
  • .py文件应该怎样打开?
  • :O)修改linux硬件时间
  • [Android] 240204批量生成联系人,短信,通话记录的APK
  • [Android]通过PhoneLookup读取所有电话号码
  • [BUG]Datax写入数据到psql报不能序列化特殊字符
  • [CareerCup] 13.1 Print Last K Lines 打印最后K行
  • [CF703D]Mishka and Interesting sum/[BZOJ5476]位运算