bootstrap modal垂直居中(简单封装)

未封装前:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <script src="js/jquery.min.js"></script>
    <script src="js/bootstrap.js"></script>
</head>
<body>

<button type="button" id="modalBtn" class="btn btn-primary">点击弹出modal</button>

<div class="modal fade" id="myModal">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">Modal 标题</h4>
            </div>
            <div class="modal-body">
                <p>内容&hellip;</p>
                <p>内容&hellip;</p>
                <p>内容&hellip;</p>

            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary">确定</button>
            </div>
        </div>
    </div>
</div>

<script type="text/javascript">
    $(function(){
        var $m_btn = $(‘#modalBtn‘);
        var $modal = $(‘#myModal‘);
        $m_btn.on(‘click‘, function(){
            $modal.modal({backdrop: ‘static‘});
        });

        $modal.on(‘show.bs.modal‘, function(){
            var $this = $(this);
            var $modal_dialog = $this.find(‘.modal-dialog‘);
            // 关键代码,如没将modal设置为 block,则$modala_dialog.height() 为零
            $this.css(‘display‘, ‘block‘);
            $modal_dialog.css({‘margin-top‘: Math.max(0, ($(window).height() - $modal_dialog.height()) / 2) });
        });

    });
</script>

</body>
</html>

封装后:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <script src="js/jquery.min.js"></script>
    <script src="js/bootstrap.js"></script>
</head>
<body>

<button type="button" id="modalBtn" class="btn btn-primary">点击弹出modal</button>

<div class="modal fade" id="myModal">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">Modal 标题</h4>
            </div>
            <div class="modal-body">
                <p>内容&hellip;</p>
                <p>内容&hellip;</p>
                <p>内容&hellip;</p>

            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary">确定</button>
            </div>
        </div>
    </div>
</div>

<script type="text/javascript">
    $(function(){
        $(‘#myModal‘).myfunction({
            $m_btn:$(‘#modalBtn‘),  //触发事件元素
            $modal:$(‘#myModal‘),   //响应事件元素
            eventType:‘click‘     //事件类型
        });
    });
    ;(function ($) {
        $.fn.myfunction=function (options) {
            var defaults={
                $m_btn : $(‘#modalBtn‘),
                $modal : $(‘#myModal‘),
                eventType:‘click‘
            };
            var setting=$.extend({},defaults,options);
            this.each(function(){
                var my_btn = setting.$m_btn;
                var _modal = setting.$modal;
                var _event = setting.eventType;
                my_btn.on(_event, function(){
                    _modal.modal({backdrop: ‘static‘});
                });

                _modal.on(‘show.bs.modal‘, function(){
                    var $this = $(this);
                    var $modal_dialog = $this.find(‘.modal-dialog‘);
                    $this.css(‘display‘, ‘block‘);
                    $modal_dialog.css({‘margin-top‘: Math.max(0, ($(window).height() - $modal_dialog.height()) / 2) });
                });
            });
        }
    })(jQuery);
</script>

</body>
</html>
时间: 2024-08-29 07:21:25

bootstrap modal垂直居中(简单封装)的相关文章

对bootstrap modal的简单扩展封装

对bootstrap modal的简单扩展封装 参考自:http://www.muzilei.com/archives/677   注:原文不支持bootstrap新版本,并且居中等存在问题 此段时间一直在学习bootstrap,主要是用于做后台,一直习惯用easyui,ui,jgrid前端框架,以至于并不习惯bootstrap的风格.近来考虑到easyui性能不太好,就用了bootstrap,先说下我所了解的bootstrap. 1.外国的框架用于显示中文看着总是不妥. 2.默认的样式觉得有些

bootstrap modal垂直居中

使用过bootstrap modal(模态框)组件的人都有一种困惑, 好好的一个弹出框怎么就无法垂直居中了呢? 当然网上一些前辈也给出了不少答案, 感觉不太全而且针对的都是各自的项目难以给我等小白太直观的理解.因而手痒试试写个稍微完整点的解决方案, 作为总结及日后回顾之用. 项目中的bootstrap版本是3.X , 作为项目后台使用. 在项目进行过程中遇到组件弹出框无法垂直居中,示例demo代码如下: <!DOCTYPE html> <html> <head> <

bootstrap modal 垂直居中对齐

bootstrap modal 垂直居中对齐 文章参考 http://www.bubuko.com/infodetail-666582.html http://v3.bootcss.com/JavaScript/#modals Html代码   <div class="modal fade" id="sqh_model"> <div class="modal-dialog"> <div class="mod

Bootstrap模态框(modal)垂直居中

http://v3.bootcss.com/ 自己也试了改了几种方式也不容乐观,发现在窗口弹出之前是获取不到$(this).height()的值,本想着是用($(window).height()-$(this).height())/2,发现还是不可行. 最终只能研究一下源码了,发现可以在bootstrap.js文件后面添加如下代码,便可以实现垂直居中. /* =====================================================================

Bootstrap(v3.2.0)模态框(modal)垂直居中

Bootstrap(v3.2.0)模态框(modal)垂直居中方法: 在bootstrap.js文件900行后面添加如下代码,便可以实现垂直居中. that.$element.children().eq(0).css("position", "absolute").css({ "margin":"0px", "top": function () { return (that.$element.height(

JDBC简单封装

/** * JDBC简单封装 * 需要借助FastJsonUtil可以参考上一篇 * @author huangxincheng * */ public class BaseDao { private static String URL; private static String USERNAME; private static String PASSWORD; private static String DRIVER; private  Connection connection; priv

对系统网络请求进行简单封装

AGConnectionNet对系统网络请求进行简单封装,可便利的进行网络请求,并将数据解析与网络请求封装在同一方法下,使用更加便利(JSON 解析采用自身解析方法, XML 解析采用第三方 ReadXML 进行解析). 方法具体参数说明 初始化方法:/*** 类方法,实例化当前数据请求对象 (单例)** @return 当前请求对象*/+ (instancetype)shareRequestData; 仅进行请求数据方法/*** 请求数据 (session 请求)** @param URLSt

iOS sqlite 增删改查 简单封装(基于 FMDB)

/** *  对 sqlite 的使用进行简单封装,仅涉及简单的单表 增删改查 * *  基于 FMDB * *  操作基于 model ,数据库表字段与 model 属性一一对应,对 model 整体进行操作 * *  根据 model 对象自动建表,字段类型只支持 NSString , NSIteger , float * *  用到 runtime 运行时获取 model 属性 * */ 1 // 2 // AGDatabaseManager.h 3 // 4 // Created by

简单封装sqlite3 实现实体对象与关系型数据库的相关操作【IOS】

源码如下,只有一个类文件 // //  DBHelper.h // //  Created by Jason_Msbaby on 15/10/15. //  Copyright ? 2015年 张杰. All rights reserved. // /**  *  简单封装了对于sqlite的使用 没有加入对事务及其他复杂的特性     基于传入sql语句的方式进行执行     使用单例模式     根据model对象自动创建表 只支持 NSInter NSString float 数据类型的支