Bootstrap模態(tài)框(modal)垂直居中的實例代碼
Bootstrap官網(wǎng)下載:http://v3.bootcss.com/
今天就在使用Bootstrap框架中遇到的一個問題分享一下,在產(chǎn)品開發(fā)的過程中使用了大量的彈出窗口(modal)。
剛開始學(xué)習(xí)使用的過程中就發(fā)現(xiàn)此窗口不能垂直居中,總是偏上,并且不能拖動,看了一下使用說明也沒有提供過多的屬性設(shè)置和方法,就這樣使用默認的方式一直用著。最近,客戶卻提出了一個要求:能不能讓彈出窗口居中,因為一些小的窗口偏上總感覺整體頁面失衡,大一點的還過得去。
因為之前對Bootstrap也不是很熟悉,便開始baidu、google,發(fā)現(xiàn)只有很少的解決方案,如下:
$("#myModal").modal().css({ "margin-top": function () { return - ($(this).height() / 2); } });
參考地址:http://www.g2w.me/2012/06/bootstrap-modal-shown-in-the-center/
這種方法自己試了一下,并不能完全居中,并且窗口的大小不一樣的話,每次顯示的margin-top值也會改變,遮蓋層還會出現(xiàn)滾動條,效果也不好看。
自己也試了改了幾種方式也不容樂觀,發(fā)現(xiàn)在窗口彈出之前是獲取不到$(this).height()的值,本想著是用($(window).height()-$(this).height())/2,發(fā)現(xiàn)還是不可行。
最終只能研究一下源碼了,發(fā)現(xiàn)可以在bootstrap.js文件900行后面添加如下代碼,便可以實現(xiàn)垂直居中。
that.$element.children().eq(0).css("position", "absolute").css({ "margin":"0px", "top": function () { return (that.$element.height() - that.$element.children().eq(0).height()-40) / 2 + "px"; }, "left": function () { return (that.$element.width() - that.$element.children().eq(0).width()) / 2 + "px"; } });
頁面代碼如下:
<div> <button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal"> Launch demo modal </button> <!-- 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">Modal title</h4> </div> <div class="modal-body"> ... </div> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> <button type="button" class="btn btn-primary">Save changes</button> </div> </div><!-- /.modal-content --> </div><!-- /.modal-dialog --> </div><!-- /.modal --> </div>
效果圖如下:
以上所述是小編給大家介紹的Bootstrap模態(tài)框(modal)垂直居中的實例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- 頁面遮罩層,并且阻止頁面body滾動。bootstrap模態(tài)框原理
- Bootstrap每天必學(xué)之模態(tài)框(Modal)插件
- 淺析BootStrap中Modal(模態(tài)框)使用心得
- Bootstrap 模態(tài)框(Modal)帶參數(shù)傳值實例
- bootstrap模態(tài)框消失問題的解決方法
- 在iframe中使bootstrap的模態(tài)框在父頁面彈出問題
- bootstrap 模態(tài)框(modal)實現(xiàn)水平垂直居中顯示
- BootStrap 模態(tài)框?qū)崿F(xiàn)刷新網(wǎng)頁并關(guān)閉功能
- Bootstrap模態(tài)框禁用空白處點擊關(guān)閉
- Bootstrap實現(xiàn)模態(tài)框效果
相關(guān)文章
JavaScript實現(xiàn)兼容IE6的收起折疊與展開效果實例
這篇文章主要介紹了JavaScript實現(xiàn)兼容IE6的收起折疊與展開效果,結(jié)合具體實例形式分析了javascript事件響應(yīng)及針對頁面元素屬性的動態(tài)操作相關(guān)實現(xiàn)技巧,需要的朋友可以參考下2017-09-09解決在layer.open中使用時間控件laydate失敗的問題
今天小編就為大家分享一篇解決在layer.open中使用時間控件laydate失敗的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-09-09uniapp與webview之間的相互傳值的實現(xiàn)
這篇文章主要介紹了uniapp與webview之間的相互傳值的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06