jquery实现自定义弹出框

摘要:
消息:弹出框中显示的内容。
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<script src="../js/jquery-1.8.3.min.js" type="text/javascript"></script>
<script src="../js/bootstrap-typeahead.js" type="text/javascript"></script>
<link href="../css/bootstrap.min.css" rel="stylesheet"/>
<script src="../js/bootstrap.min.js" type="text/javascript"></script>
<script src="../js/bootstrap-select.js" type="text/javascript"></script>
<link href="../css/bootstrap-select.min.css" rel="stylesheet"/>

<script type="text/javascript">
    /*
     用途描述:自定义的消息提示框和消息确认框,采用jquery的闭包方法实现,但必须依赖
     与jQuery,否则没有效果。
     使用说明:
     alert框请调用:zdalert(title,top,width, message, function(ret))
     confirm框请调用:zdconfirm(title,top,width, message, function(ret))
     方法参数说明:
     title:弹出框的显示标题;
     top:弹出框位于当前窗体的高度,请填写整数值,使用的分数形式(_height - boxHeight) / top
     width:弹出框的宽度,请填写px值。
     message:弹出框显示的内容。
     function:回调函数,需要执行的内容方法,参数ret有一个,为ture和false值*/


    (function($) {
//声明闭包方法
        $.alerts = {
            alert: function(title,top,width, message, callback) {
                if( title == null ) title = 'Alert';
                $.alerts._show(title,top,width, message, null, 'alert', function() {
                    if( callback ) callback(result);
                });
            },

            confirm: function(title, top,width,message, callback) {
                if( title == null ) title = 'Confirm';
                $.alerts._show(title, top,width,message, null, 'confirm', function(result) {
                    if( callback ) callback(result);
                });
            },


            _show: function(title,top,width,msg, value, type, callback) {

                var _html = "";

                _html += '<div id="mb_box"></div><div id="mb_con"><span id="mb_tit">' + title + '</span>';
                _html += '<div id="mb_msg">' + msg + '</div><div id="mb_btnbox">';
                if (type == "alert") {
                    _html += '<input   type="button" value="确定" />';
                }
                if (type == "confirm") {
                    _html += '<input   type="button" value="确定" />';
                    _html += '<input   type="button" value="取消" />';
                }
                _html += '</div></div>';

                //必须先将_html添加到body,再设置Css样式
                $("body").append(_html); GenerateCss(top,width);
                //判断是确认框还是提示框
                switch( type ) {
                    case 'alert':

                        $("#mb_btn_ok").click( function() {
                            $.alerts._hide();
                            callback(true);
                        });
                        $("#mb_btn_ok").focus().keypress( function(e) {
                            if( e.keyCode == 13 || e.keyCode == 27 ) $("#mb_btn_ok").trigger('click');
                        });
                        break;
                    case 'confirm':

                        $("#mb_btn_ok").click( function() {
                            $.alerts._hide();
                            if( callback ) callback(true);
                        });
                        $("#mb_btn_no").click( function() {
                            $.alerts._hide();
                            if( callback ) callback(false);
                        });
                        $("#mb_btn_no").focus();
                        //键盘的按键快捷键
                        $("#mb_btn_ok, #mb_btn_no").keypress( function(e) {
                            //enter键
                            if( e.keyCode == 13 ) {$("#mb_btn_ok").trigger('click');}
                            //esc键
                            if( e.keyCode == 27 ){ $("#mb_btn_no").trigger('click');}
                        });
                        break;


                }
            },
            _hide: function() {
                $("#mb_box,#mb_con").remove();
            }
        }
        // 显示提示框,top,top窗体位置当前窗口的高低的百分比,必须填写整数;width窗体显示的宽度。
        zdalert = function(title,top,width, message, callback) {
            $.alerts.alert(title,top,width, message, callback);
        }
        //显示确认框,top,top窗体位置当前窗口的高低的百分比,必须填写整数;width窗体显示的宽度。
        zdconfirm = function(title,top,width, message, callback) {
            $.alerts.confirm(title,top,width, message, callback);
        };




        //生成Css
        var GenerateCss = function (top,width) {

            $("#mb_box").css({  '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',  '350px', position: 'fixed',
             backgroundColor: 'White', borderRadius: '15px'
             });*/
            //去掉边框的圆
            $("#mb_con").css({ zIndex: '999999', width, position: 'fixed',
                backgroundColor: 'White'
            });

            $("#mb_tit").css({ display: 'block', fontSize: '14px', color: 'white', padding: '10px 15px',
                backgroundColor: '#31B0D5', borderRadius: '0 0 0 0',
                borderBottom: '3px solid #999', fontWeight: 'bold'
            });

            $("#mb_msg").css({ padding: '20px', lineHeight: '20px',
                borderBottom: '1px dashed #DDD', fontSize: '16px',backgroundColor:'#FCFCFC'
            });

            $("#mb_ico").css({ display: 'block', position: 'absolute', right: '10px', top: '9px',
                border: '1px solid Gray',  '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({  '85px', height: '30px', color: 'white', border: 'none' });
            $("#mb_btn_ok").css({ backgroundColor: '#168bbb' });
            $("#mb_btn_no").css({ backgroundColor: '#168bbb', 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) / top + "px", left: (_widht - boxWidth) / 2 + "px" });
        }


    })(jQuery);
</script>

<body>
<input id="add" type="button" value="添加" />
<input id="update" type="button" value="修改" />

<script type="text/javascript">

    $("#add").bind("click", function () {
        // $.MsgBox.Alert("消息", "哈哈,添加成功!");

        zdalert('系统提示',3,"350px",'请输入正确数值',function(){

            //要回调的方法
            window.location.href="http://www.baidu.com"});

    });


    //也可以传方法名 test
    $("#update").bind("click", function () {


        // $.MsgBox.Confirm("温馨提示", "确定要进行修改吗?", test);

        zdconfirm('系统确认框',6,"350px",'你确认提交该条数据吗',function(r){
            if(r)
            {
                //...点确定之后执行的内容

                window.location.href="http://www.baidu.com"
            }
        });


    });


</script>

</body>
</html>

免责声明:文章转载自《jquery实现自定义弹出框》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Linux系统备份-----Respinmysql负载飙高原因分析下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

jquery 判断元素是否存在于数组中

要判断数组中是否包含某个元素,从原理来来说,就是遍历整个数组,然后判断是否相等 可以使用Jquery提供的方法: $.inArray("元素(字符串)",数组名称) 进行判断 ,当存在该元素(字符串)时,返回该元素在数组的下标,不存在时返回 -1 $(function () { var array = ["asp.net", "as...

60款很酷的 jQuery 幻灯片演示和下载【转】

jQuery是一个非常优秀的JavaScript框架,使用简单灵活,同时还有许多成熟的插件可供选择,它可以帮助你在项目中加入漂亮的效果,其中之一就是幻灯片效果的实现,这是一种在有限的网页空间内展示系列项目时非常好的方法。 今天这篇文章要给大家分享的是60款很酷的jQuery幻灯片插件,相信里面一定会有你喜欢的。 Cloud Carousel(演示|下载)...

jQuery Mobile 1.3.0发布

jQuery基金会发布了旗下的JavaScript和HTML5/CSS框架jQuery Mobile 1.3.0。更新主要集中在响应式的web设计并新增了多个移动应用的widget。 响应式web设计(RWD)旨在提供最佳的用户浏览体验,不管使用的是什么设备、什么浏览器。RWD使用CSS媒体查询(media queries)语言在浏览器中动态适配页面布局,...

bower简明入门教程

什么是bower:   Bower是一个客户端技术的软件包管理器,它可用于搜索、安装和卸载如JavaScript、HTML、CSS之类的网络资源。其他一些建立在Bower基础之上的开发工具,如YeoMan和Grunt,这个会在以后的文章中介绍。    准备工作:   1、安装 node 环境:node.js   2、安装 Git,bower 从远程 gi...

jquery圆角插件

为了实现div的圆角效果,你还在用古老的背景图片拼凑的方法吗?还是在用各种浏览器不互相兼容的CSS方式?如果你还在用这样的方式实现圆角,那我告诉你你真的out了,或许是我out了,竟然以前没发现有这样一个强大的jquery圆角插件,既然今天发现了,那我就不得不拿出来和大家分享了。求发展,先分享,少废话,贴代码:-) 这个插件有一个特点是如果用户的浏览器支持...

jQuery.easyui与jQuery UI冲突

在最近的一个项目中,更新用户头像功能模块用到jQuery UI中的功能:拖放,重设大小,为了图方便,直接使用了整个UI库,而会话框以及弹出框则使用的是jQuery.easyui中集成的功能; 但在实际使用当中,发现jquery ui与jquery.easyui存在冲突,因为两个都是基于jQuery的,因而可能存在部分方法或参数名重复,造成其中之一的功能丢失...