弹出框插件——dialog
基于jquery和dot.js弹出框插件,兼容IE6+等其他浏览器。
思想:弹出框元素插入body节点中,并在页面垂直居中显示(fixed定位),触发确定和关闭事件绑定。
注意ie6包含两个问题:
一、select、flash不能遮罩,采用iframe。
二、fixed属性采用滚动时重新计算高度或在样式中采用表达式计算expression。
源码分析:
1、creatHtml:采用doT.js初始化元素添加到body中;
2、show:设置宽度,高度,居中显示;
3、events:为关闭和确定绑定事件;
4、removeCallback:移出元素节点,如有执行回调方法;
5、ie6fixed:ie6中fixed的兼容性处理。
参数使用说明:
width:定义弹出框的宽度,默认值是400。
height:定义弹出框的高度,默认值是100.
title:定义弹出框的标题,默认值是空。
html:定义弹出框的内容,默认值是空。
type:定义弹出框的类型,默认值是default,其他conform和alert。
closed: 标题栏中的关闭按钮,回调方法,默认为null。
conform:包含name指按钮的名称,默认值为确定,callback回调方法,默认值为null。
cancel:包含name指按钮的名称,默认值为取消,callback回调方法,默认值为null。
备注:如果使用conform或alert时,不设置type类型是不起作用。
放在页面底部,作为公用部分:
使用方法:
$.Dialog.init({
    height: 180,
    title: "弹出框",
    html: "亲,默认弹出框哦",
    closed: function() {
        alert("关闭");
    }
});
github地址:https://github.com/benpaobenpao/dialog
DEMO:
<!doctype html>
<html>
<head>
    <meta charset="UTF-8">
    <title>dialog弹出框benpaobenpao</title>
    <style>
        *{margin:0px;padding: 0px;}
        .clearfix{*zoom:1;}
        .clearfix:after{content:"\0020";display:block;height:0;clear:both;overflow:hidden;visibility:hidden;}
        .dialogbg{display:block; *zoom:1; position: absolute; left: 0px; top: 0px; height: 100%; width: 100%; opacity: 0.35;filter:alpha(opacity=30); background: #ccc;z-index: 9999;}
        .dialogbox{position: fixed;_position: absolute;-webkit-box-shadow: 0px 0px 6px #999;border: 1px solid #ccc;box-shadow: 0px 0px 3px #999;border-radius: 6px; z-index: 99999;left: 50%;top: 50%;background: #f5fffa;overflow: hidden;}
        .dialogboxrunstart{-webkit-transform: scale(0px,0px);-webkit-transition:all 0.2s ease-in-out;-moz-transform: scale(0px,0px);-moz-transition:all 0.2s ease-in-out;-ms-transform: scale(0px,0px);-ms-transition:all 0.2s ease-in-out;-o-transform: scale(0px,0px);-o-transition:all 0.2s ease-in-out;transform: scale(0px,0px);transition:all 0.2s ease-in-out;}
        .dialogtit{position: relative;height: 30px; line-height: 30px; overflow: hidden;padding: 0px 10px;border-bottom: 1px solid #274863;font-size: 16px;background: #4682b4;}
        .dialogclosed{position: absolute;right: 10px;color: #fff;font-weight: 700;font-size: 20px;cursor: pointer;}
        .dialogclosed:hover{color: #f00;}
        .dialogtxt{color: #fff;font-weight: 700;margin-right: 20px;}
        .dialogmain{padding: 10px 20px 10px 20px;}
        .dialogBtns{text-align: center;padding-top: 5px;}
        .dialogconform,.dialogcancel{display: inline-block;*display: inline;*zoom:1;padding: 0px 10px;height: 24px;line-height: 24px;color: #fff;font-size: 12px;border-radius: 4px;background: #005eac;border-width: 1px;border-style: solid;border-color: #b8d4e8 #124680 #124680  #b8d4e8;cursor: pointer;}
        .dialogcancel{margin-left: 10px;}
        .dialogconform:hover,.dialogcancel:hover{background: #0000ff;}
</style>
    <script src="http://files.cnblogs.com/kuikui/jquery-1.10.2.min.js"></script>
    <script src="http://files.cnblogs.com/kuikui/dot.min.js"></script>
    <style>
        body{height: 1500px;}
        .main{width: 960px; margin: 60px auto 0px; }
        .main .btn{width: 100px; height: 30px; line-height: 30px; text-align: center; cursor: pointer;}
        .mtable{ height: 100px; overflow-y: auto;}
        .mtable table{width: 100%; text-align: center;}
        .mtable table th, .mtable table td{padding:10px 0px;}
    </style>
</head>
<body>
    <div class="main">
        <div>
            <img src="http://images.cnblogs.com/cnblogs_com/kuikui/354173/r_test1.jpg" />
            <select><option>one</option><option>two</option></select>
            弹出框插件测试。。。
        </div>
        <div>
            点击测试:
            <input class="btn" id="btndef" type="button" value="default" />
            <input class="btn" id="btnconf" type="button" value="confirm" />
            <input class="btn" id="btnal" type="button" value="alert" />
            <p>使用conform、alert必须制定其type类型,type默认值是default。</p>
        </div>
    </div>
<script id="arrtmpl" type="text/x-dot-template">
        <div class="mtable">
            <table >
                <thead>
                    <tr>
                        <th>第一项</th>
                        <th>第二项</th>
                        <th>第三项</th>
                    </tr>
                </thead>
                <tbody>
                    {{~it.array:value:index }}
                    <tr>
                        <td>{{= value.one }}</td><td>{{= value.two }}</td><td>{{= value.three }}</td>
                    </tr>
                    {{~}}
                </tbody>
            </table>
        </div>
    </script>
<script id="dialogtmpl" type="text/x-dot-template">
        {{? it.isIE6===false }}
        <div id="dialogbg" class="dialogbg"></div>
        {{??  }}
        <iframe id="dialogbg" class="dialogbg"></iframe>
        {{?}}
        <div id="dialogbox" class="dialogbox">
            <div class="dialogcont">
                <div class="dialogtit clearfix">
                    <a class="dialogclosed" title="关闭">⊗</a>
                    <div class="dialogtxt">{{=it.title}}</div>
                </div>
                <div class="dialogmain">{{=it.html}}</div>
                {{? it.type === "conform"}}
                <div class="dialogBtns"><a class="dialogconform">{{=it.conform.name||"确定"}}</a></div>
                {{?? it.type === "alert"}}
                <div class="dialogBtns"><a class="dialogconform">{{=it.conform.name||"确定"}}</a><a class="dialogcancel">{{=it.cancel.name||"取消"}}</a></div>
                {{?}}
            </div>
        </div>
    </script>
<script>
        (function($) {
            $.Dialog = {
                init: function(options) {
                    var _this = this,
                        defaults = {
                            width: "400",
                            height: "100",
                            title: "",
                            html: "",
                            type: "default", //  default   conform   alert
                            closed: null,
                            conform: {
                                name: "确定",
                                callback: null
                            },
                            cancel: {
                                name: "取消",
                                callback: null
                            }
                        };
                    options.isIE6 = !! window.ActiveXObject && !window.XMLHttpRequest;
                    _this.options = $.extend({}, defaults, options);
                    _this.creatHtml();
                    _this.show();
                    _this.events();
                    if(_this.options.isIE6){
                       _this.ie6fixed();
                    }
                },
                creatHtml: function() {
                    var _this = this;
                    var interText = doT.template($("#dialogtmpl")[0].text);
                    $("body").append(interText(_this.options));
                },
                show: function() {
                    var _this = this;
                    _this.dialogbg = $("#dialogbg");
                    _this.dialogbox = $("#dialogbox");
_this.dialogbg.css({
                        "height": $(document).height()
                    });
_this.dialogbox.css({
                        "margin-top": (-_this.options.height / 2) + "px",
                        "margin-left": (-_this.options.width / 2) + "px",
                        "width": _this.options.width + "px",
                        "height": _this.options.height + "px"
                    });
_this.dialogbox.addClass("dialogboxrunstart");
                },
                events: function() {
                    var _this = this;
                    _this.dialogclosed = _this.dialogbox.find(".dialogclosed");
                    _this.dialogconform = _this.dialogbox.find(".dialogconform");
                    _this.dialogcancel = _this.dialogbox.find(".dialogcancel");
_this.dialogclosed.on("click", function() {
                        _this.removeCallback(_this.options.closed);
                    });
_this.dialogconform.on("click", function() {
                        _this.removeCallback(_this.options.conform.callback);
                    });
_this.dialogcancel.on("click", function() {
                        _this.removeCallback(_this.options.cancel.callback);
                    });
                },
                removeCallback: function(call) {
                    var _this = this;
                    _this.dialogbg.remove();
                    _this.dialogbox.remove(); !! call && call();
                },
                ie6fixed: function() {
                    var _this = this;
                    $(window).scroll(function() {
                        _this.dialogbox.css({
                            "top": (($(window).height() - _this.options.height) / 2 + $(document).scrollTop()) + "px"
                        });
                    });
                }
            }
        })(jQuery);
    </script>
<script>
        (function($) {
            $("#btndef").click(function() {
                $.Dialog.init({
                    height: 180,
                    title: "弹出框",
                    html: "亲,默认弹出框哦",
                    closed: function() {
                        alert("关闭");
                    }
                });
            });
$("#btnconf").click(function() {
                $.Dialog.init({
                    height: 100,
                    html: "",
                    type: "conform",
                    conform: {
                        name: "确认按钮哦哦哦哦"
                    }
                });
            });
$("#btnal").click(function() {
                var arr = {
                    "array": [{
                        "one": "1",
                        "two": "2",
                        "three": "3"
                    }, {
                        "one": "11",
                        "two": "22",
                        "three": "33"
                    }, {
                        "one": "111",
                        "two": "222",
                        "three": "333"
                    }, {
                        "one": "1111",
                        "two": "2222",
                        "three": "3333"
                    }, {
                        "one": "11111",
                        "two": "22222",
                        "three": "33333"
                    }, {
                        "one": "111111",
                        "two": "222222",
                        "three": "333333"
                    }]
                };
                var interText = doT.template($("#arrtmpl")[0].text);
                var html = interText(arr);
                $.Dialog.init({
                    height: 190,
                    html: html,
                    type: "alert",
                    conform: {
                        callback: function() {
                            alert("确定");
                        }
                    },
                    cancel: {
                        callback: function() {
                            alert("取消");
                        }
                    }
                });
            });
})(jQuery);
    </script>
</body>
</html>
运行代码
弹出框插件——dialog的更多相关文章
- 弹出框插件layer使用
		layer是一款近年来备受青睐的web弹层组件,她具备全方位的解决方案,致力于服务各水平段的开发人员,您的页面会轻松地拥有丰富友好的操作体验. 插件官方地址:http://layer.layui.co ... 
- JS弹出框插件zDialog再次封装
		zDialog插件网址:http://www.jq22.com/jquery-info2426 再次封装zDialog的代码: (function ($) { $.extend({ iDialog: ... 
- jQuery+css3弹出框插件
		先来看DEMO:https://codepen.io/jonechen/pen/regjGG 插件的开发很简单,运用了CSS3的动画效果,并且弹出框的内容可以自定义.插件的默认配置参数有三个: var ... 
- Android--自定义弹出框-自定义dialog
		项目要用到弹出框,还要和苹果的样式一样(Android真是没地位),所以就自己定义了一个,不是很像(主要是没图),但是也还可以. 废话不多说了,直接上代码 1.先看布局文件 <?xml vers ... 
- js登录弹出框插件
		第一步:页面引入css:<link rel="stylesheet" type="text/css"" href="common/cs ... 
- layer弹出框插件参数及方法介绍
		layerui下载:http://www.layui.com 更多参数请阅读开发文档:http://www.layui.com/doc/modules/layer.html Layui 是一款采用自身 ... 
- jQuery zxxbox弹出框插件(v3.0)
		插件地址: http://www.zhangxinxu.com/study/201009/jquery-zxxbox-v3-demo.html 
- bootbox弹出框插件
		具体用法查看官网http://bootboxjs.com/examples.html {% load staticfiles %} <!DOCTYPE html> <html lan ... 
- BootStrap弹出框插件popover简单实例
		1.网上实例地址 http://www.runoob.com/bootstrap/bootstrap-popover-plugin.html 2.具体demo $("#pieId&q ... 
随机推荐
- Python3练习题系列(07)——列表操作原理
			目标: 理解列表方法的真实含义. 操作: list_1.append(element) ==> append(list_1, element) mystuff.append('hello') 这 ... 
- PHP Math 函数  mt_rand() 使用 Mersenne Twister 算法返回随机整数。
			语法 mt_rand(min,max) 说明 如果没有提供可选参数 min 和 max,mt_rand() 返回 0 到 RAND_MAX 之间的伪随机数.例如想要 5 到 15(包括 5 和 15) ... 
- Tcp的三次握手,以及原理详解
			http://blog.sina.com.cn/s/blog_6002b97001018fxh.html 
- 喵哈哈村的魔法考试 Round #11 (Div.2) 题解
			喵哈哈村的星星与月亮(一) 打表发现答案就等于a*b%mod 注意a*b可能爆longlong #include<bits/stdc++.h> using namespace std; c ... 
- spring如何解决单例循环依赖问题?
			更多文章点击--spring源码分析系列 1.spring循环依赖场景2.循环依赖解决方式: 三级缓存 1.spring循环引用场景 循环依赖的产生可能有很多种情况,例如: A的构造方法中依赖了B的实 ... 
- ArcGIS中国工具2.5正式发布
			ArcGIS中国工具2.5正式发布 1. 支持1:500,1:1000,1:2000的梯形接幅表和图框打印(见3.2) 2. 支持接幅表,一定间隔如1秒加节点生成并能自动识别打印(见3. ... 
- 从安装node js到构建一个vue并启动它
			1.安装node js 下载地址:http://nodejs.cn/download/2.安装完成后运行Node.js command prompt(node -v查看安装版本) 3.安装npm(由于 ... 
- 了解 JavaScript (2)- 需要了解的一些概念
			Ajax 是什么 Ajax 是一种创建交互式 Web 应用程序的方式. Ajax 是 Asynchronous JavaScript and XML 缩写(异步的 JavaScript 和 XML), ... 
- Gradle Build Tool
			转自知乎: nonesuccess 通俗的说:gradle是打包用的. 你觉得解决你的问题了吗?如果没解决,那是你的问题提得不够好.比如我猜你应该提:为什么要打包发布,打包发布有几种常见方法,为什么这 ... 
- Python操作redis系列之 列表(list) (五)
			# -*- coding: utf- -*- import redis r =redis.Redis(host=,password=") 1. Lpush 命令将一个或多个值插入到列表头部. ... 
