模态框以及大小和位置

  • Post author:
  • Post category:其他


1.  描述模态框

Bootstrap Modals(模态框)是使用定制的 Jquery 插件创建的。它可以用来创建模态窗口丰富用户体验,或者为用户添加实用功能。您可以在 Modals(模态框)中使用 Popover(弹出框)和 Tooltip(工具提示插件)。

在本教程中,将通过一些实例和解释来讨论如何使用 Bootstrap 创建模态窗口。同时,我们也会讨论用于定制的各种可用选项。

2.需要的文件

您需要 Jquery、Bootstrap CSS 和 JavaScript 文件 bootstrap-modal.js。这个 js 文件位于您下载的 Bootstrap 主文件夹中的 js 文件夹内。

Jquery 位于您的 Bootstrap 主文件夹中的 docs > assets > js 下,名为 jquery.js。或者您可以直接访问 https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js下载 Jquery。

3.代码演示

<div class=”container”>

<h2>使用Bootstrap创建模态框</h2>

<divid=”example” class=”modal hide fade in”style=”display: none; “>

<divclass=”modal-header”>

<a class=”close”data-dismiss=”modal”>×</a>

<h3>这是一个模态框标题</h3>

</div>

<divclass=”modal-body”>

<h4>模态框中的文本</h4>

<p>你可以在这添加一些文本。</p>

</div>

<divclass=”modal-footer”>

<a href=”#” class=”btn btn-success”>唤醒活动</a>

<a href=”#” class=”btn”data-dismiss=”modal”>关闭</a>

</div>

</div>

<p><a data-toggle=”modal” href=”#example”class=”btn btn-primary btn-large”>发动演示模态框</a></p>

</div>

<scriptsrc=”https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js”></script>

<scriptsrc=”/twitter-bootstrap/twitter-bootstrap-v2/js/bootstrap-modal.js”></script>

4.调整模态框的大小和位置

Bootstrap其实就是设置了一堆已经准备好的css,你直接拿过来用就可以了,

其中模态框有几点比较重要

1) overflow

overflow 一共有5个属性,在任意css中可以修改。

overflow:auto :内容会被修剪,超出设置的宽高后会出现滚动条。

overflow:scroll:内容会被修剪,不管内容是否超出,都会出现滚动条的位置。

overflow:visible:这个是默认值,内容不会被修剪,会出现在元素框外面。

overflow:hidden:内容被修剪,多余的内容被隐藏。

overflow:inherit:从父元素那里继承overflow的值。

一般我们使用第一个overflow:auto 属性的就可以将过多的内容,以滚动条

形式显示出来,上面的5个属性,还可以分为:overflow-x:和overflow-y,分别是x轴方向和y轴方向

2)max-height和max-width

模态框的大小我们通过height和width来调整,设置max-height和max-width

这两个属性可以将模态框的最大高度和宽度固定,这只是固定height和width的上限,如果height和width没有超过max-height和max-height,那height和width则不会收到影响。需要注意的是,虽然这个两个属性比较鸡肋,但是如果在bootstrap.css会有写到,比如

如果发现自己调整不了模态框的大小,那么就要想一想是不是使用了这个属性。

3)调整模态框位置

直接调整

可以通过top,right,bottom,left属性用百分比来直接调整

也可以通过js让模态框水平居中显示

$modal.on(‘shown.bs.modal’,function(event) {

varieVersion = getIEVersion();

// 非ie或者ie9+使用css已经可以了

if(ieVersion === false || ieVersion > 8) {

returntrue;

}

var$modalDialog = $modal.find(‘.modal-dialog’),

h =$modalDialog.height(),

w =$modalDialog.width();

$modalDialog.css({

marginLeft: -w / 2,

marginTop: -h / 2

});

});

// 兼容ie9及以下版本

function getIEVersion() {

var v = 3,

div =document.createElement(‘div’),

beacon =div.getElementsByTagName(‘i’);

while (

div.innerHTML = ‘<!–[if gt IE ‘ + (++v) + ‘]><i></i><![endif]–>’,

beacon[0]

);

return v> 4 ? v : false;

}



版权声明:本文为qq_32993549原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。