zoukankan      html  css  js  c++  java
  • 模态框居中显示

    Bootstrap模态框水平垂直居中与增加拖拽功

    最近开发一个CMS系统使用上了Bootstrap,在开发一个添加某些选项时,打算弹出一个模态框,但是发现,模态框不会垂直居中到屏幕上,而是在屏幕上方,找了好多资料都没搞定,最终自己试出了一种JS的方法,同时还需要Bootstrap模态框可以拖动,但是发现默认的也不行,翻遍了网络找了出来。现在分享给大家:

    原文地址:http://www.panshy.com/articles/201509/webdev-2524.html

    以下为Bootstrap模态框拖拽功能的增加方法

    $("#myModal").draggable({ handle: ".modal-header", cursor: 'move', refreshPositions: false });

    handle: ".modal-header", 去除将可以整个模态框都可以拖动,其中modal-header代表拖动的DIV的CLASS或ID

    以下为弹出Bootstrap模态框水平垂直居中的代码

    /* center modal */
    function centerModals() { $('#myModal').each(function(i) { var $clone = $(this).clone().css('display', 'block').appendTo('body'); var top = Math.round(($clone.height() - $clone.find('.modal-content').height()) / 2); top = top > 0 ? top : 0; $clone.remove(); $(this).find('.modal-content').css("margin-top", top); }); } $('#myModal').on('show.bs.modal', centerModals); $(window).on('resize', centerModals);
    /*设置模态框居中显示  密码修改模态框居中显示*/
    function centerModals() {
    $('#operDialogPassword').each(function(i) {
    var $clone = $(this).clone().css('display', 'block').appendTo('body');
    var top = Math.round(($clone.height() - $clone.find('.modal-content').height()) / 2);
    top = top > 0 ? top : 0; $clone.remove();
    $(this).find('.modal-content').css("margin-top", top);
    });
    }
    $('#operDialogPassword').on('show.bs.modal', centerModals);

    其中,$(window).on('resize', centerModals); 代表用户改变浏览器时的事件,可以不用,但是改变浏览器,模态框不会跟着变化。

    以上的JS代码加到页面的最后即可

    Bootstrap模态框HTML

    <!-- Modal --> <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button> <h4 class="modal-title" id="myModalLabel">标题</h4> </div> <div style="padding:5px;"> <div class="modal-body" data-scrollbar="true" data-height="200" data-scrollcolor="#000">

    模态框内容

    </div> </div> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button> </div> </div> </div> </div>
  • 相关阅读:
    目前正在自学python,前几天做了一个比较简单的坦克大战游戏,分享出来,想搞一搞的朋友,可以参考。
    我今天给学习运维而英语不好的各位,提供一些计算机英语,感谢惨绿少年的原文和已经离开身边提供英标部分的小虾大佬,只是为了记录。
    前几天看见pthon自动跳一跳很火,自己也按捺不住寂寞,实现了一把。分享一下。图文详解,如果有问题留言,帮解决。
    day01
    java之jvm篇
    mysql
    leecode刷题——数组篇
    java基础
    python进程和线程
    python I/O编程
  • 原文地址:https://www.cnblogs.com/lcyxfei/p/7518950.html
Copyright © 2011-2022 走看看