varmyModal=document.getElementById('myModal')varmyInput=document.getElementById('myInput')myModal.addEventListener('shown.bs.modal',function(){myInput.focus()
該元件的動畫效果取決於 prefers-reduced-motion media queries。請參考 reduced motion section of our accessibility documentation
繼續閱讀 demos 和使用指南。
動態視窗元件
下面是一個 靜態 動態視窗範例(意思是它的 position 和 display 已被覆寫)。 包括動態視窗標題、動態視窗主體(padding 必要)和動態視窗頁腳(可選)。盡可能地包含動態視窗標題與移除按鈕,或提供另一個明確的移除操作。
<divclass="modal"tabindex="-1"><divclass="modal-dialog"><divclass="modal-content"><divclass="modal-header"><h5class="modal-title">Modal title</h5><buttontype="button"class="btn-close"data-bs-dismiss="modal"aria-label="Close"></button></div><divclass="modal-body"><p>Modal body text goes here.</p></div><divclass="modal-footer"><buttontype="button"class="btn btn-secondary"data-bs-dismiss="modal">Close</button><buttontype="button"class="btn btn-primary">Save changes</button></div></div></div></div>
This is some placeholder content to show the scrolling behavior for modals. Instead of repeating the text the modal, we use an inline style set a minimum height, thereby extending the length of the overall modal and demonstrating the overflow scrolling. When content becomes longer than the height of the viewport, scrolling will move the modal as needed.
This is some placeholder content to show the scrolling behavior for modals. We use repeated line breaks to demonstrate how content can exceed minimum inner height, thereby showing inner scrolling. When content becomes longer than the prefedined max-height of modal, content will be cropped and scrollable within the modal.
This content should appear at the bottom after you scroll.
This is some placeholder content to show a vertically centered modal. We've added some extra copy here to show how vertically centering the modal works when combined with scrollable modals. We also use some repeated line breaks to quickly extend the height of the content, thereby triggering the scrolling. When content becomes longer than the prefedined max-height of modal, content will be cropped and scrollable within the modal.
<divclass="modal-body"><h5>Popover in a modal</h5><p>This <ahref="#"role="button"class="btn btn-secondary popover-test"title="Popover title"data-bs-content="Popover body content is set in this attribute.">button</a> triggers a popover on click.</p><h5>Tooltips in a modal</h5>
<p><ahref="#"class="tooltip-test"title="Tooltip">This link</a> and <ahref="#"class="tooltip-test"title="Tooltip">that link</a> have tooltips on hover.</p></div>
有數個按鈕要用來觸發相同互動視窗,但會導入不同內容? 使用 event.relatedTarget 和 HTML data-bs-* 屬性 依據點擊按鈕來更改動態視窗的內容。
下面是 HTML 和 JavaScript 的範例。 有關 relatedTarget 的詳細資訊,請參閱 互動視窗的事件。
<buttontype="button"class="btn btn-primary"data-bs-toggle="modal"data-bs-target="#exampleModal"data-bs-whatever="@mdo">Open modal for @mdo</button><buttontype="button"class="btn btn-primary"data-bs-toggle="modal"data-bs-target="#exampleModal"data-bs-whatever="@fat">Open modal for @fat</button><buttontype="button"class="btn btn-primary"data-bs-toggle="modal"data-bs-target="#exampleModal"data-bs-whatever="@getbootstrap">Open modal for @getbootstrap</button><divclass="modal fade"id="exampleModal"tabindex="-1"aria-labelledby="exampleModalLabel"aria-hidden="true"><divclass="modal-dialog"><divclass="modal-content"><divclass="modal-header"><h5class="modal-title"id="exampleModalLabel">New message</h5><buttontype="button"class="btn-close"data-bs-dismiss="modal"aria-label="Close"></button></div><divclass="modal-body"><divclass="mb-3"><labelfor="recipient-name"class="col-form-label">Recipient:</label><inputtype="text"class="form-control"id="recipient-name"></div><divclass="mb-3"><labelfor="message-text"class="col-form-label">Message:</label><textareaclass="form-control"id="message-text"></textarea></div></form></div><divclass="modal-footer"><buttontype="button"class="btn btn-secondary"data-bs-dismiss="modal">Close</button><buttontype="button"class="btn btn-primary">Send message</button></div></div></div></div>
varexampleModal=document.getElementById('exampleModal')exampleModal.addEventListener('show.bs.modal',function(event){// Button that triggered the modal
varbutton=event.relatedTarget// Extract info from data-bs-* attributes
varrecipient=button.getAttribute('data-bs-whatever')// If necessary, you could initiate an AJAX request here
// and then do the updating in a callback.
// Update the modal's content.
varmodalTitle=exampleModal.querySelector('.modal-title')varmodalBodyInput=exampleModal.querySelector('.modal-body input')modalTitle.textContent='New message to '+recipientmodalBodyInput.value=recipient
Toggle between modals
Toggle between multiple modals with some clever placement of the data-bs-target and data-bs-toggle attributes. For example, you could toggle a password reset modal from within an already open sign in modal. Please note multiple modals cannot be open at the same time—this method simply toggles between two separate modals.
<divclass="modal fade"id="exampleModalToggle"aria-hidden="true"aria-labelledby="exampleModalToggleLabel"tabindex="-1"><divclass="modal-dialog modal-dialog-centered"><divclass="modal-content"><divclass="modal-header"><h5class="modal-title"id="exampleModalToggleLabel">Modal 1</h5><buttontype="button"class="btn-close"data-bs-dismiss="modal"aria-label
="Close"></button></div><divclass="modal-body">
Show a second modal and hide this one with the button below.
</div><divclass="modal-footer"><buttonclass="btn btn-primary"data-bs-target="#exampleModalToggle2"data-bs-toggle="modal"data-bs-dismiss="modal">Open second modal</button></div></div></div></div><divclass="modal fade"id="exampleModalToggle2"aria-hidden="true"aria-labelledby="exampleModalToggleLabel2"tabindex="-1"><divclass="modal-dialog modal-dialog-centered"><divclass="modal-content"><divclass="modal-header"><h5class="modal-title"id="exampleModalToggleLabel2">Modal 2</h5><buttontype="button"class="btn-close"data-bs-dismiss="modal"aria-label="Close"></button></div><divclass="modal-body">
Hide this modal and show the first with the button below.
</div><divclass="modal-footer"><buttonclass="btn btn-primary"data-bs-target="#exampleModalToggle"data-bs-toggle="modal"data-bs-dismiss="modal">Back to first</button></div></div></div></div><aclass="btn btn-primary"data-bs-toggle="modal"href="#exampleModalToggle"role="button">Open first modal</a>
互動視窗有三個可選的大小,透過 .modal-dialog 上的 modifier Class 啟用。 這些尺寸會在某些中斷點調整,以避免在較窄的 viewport 上出現水平捲軸。
Class
Modal max-width
<divclass="modal-dialog modal-xl">...</div><divclass="modal-dialog modal-lg">...</div><divclass="modal-dialog modal-sm">...</div>$modal-dialog-margin-y-sm-up:1.75rem;$modal-title-line-height:$line-height-base;$modal-content-color:null;$modal-content-bg:$white;$modal-content-border-color:rgba($black,.2);$modal-content-border-width:$border-width;$modal-content-border-radius:$border-radius-lg;$modal-content-inner-border-radius:subtract($modal-content-border-radius,$modal-content-border-width);$modal-content-box-shadow-xs:$box-shadow-sm;$modal-content-box-shadow-sm-up:$box-shadow;$modal-backdrop-bg:$black;$modal-backdrop-opacity:.5;$modal-header-border-color:$border-color;$modal-footer-border-color:$modal-header-border-color;$modal-header-border-width:$modal-content-border-width;$modal-footer-border-width:$modal-header-border-width;$modal-header-padding-y:$modal-inner-padding;$modal-header-padding-x:$modal-inner-padding;$modal-header-padding:$modal-header-padding-y$modal-header-padding-x;// Keep this for backwards compatibility
$modal-sm:300px;$modal-md:500px;$modal-lg:800px;$modal-xl:1140px;$modal-fade-transform:translate(0,-50px);$modal-show-transform:none;$modal-transition:transform.3sease-out;$modal-scale-transform:scale(1.02);
Default
Description
boolean or the string 'static'
Includes a modal-backdrop element. Alternatively, specify static for a backdrop which doesn't close the modal on click.
keyboard
boolean
Closes the modal when escape key is pressed
focus
boolean
Puts the focus on the modal when initialized.
Asynchronous methods and transitions
All API methods are asynchronous and start a transition. They return to the caller as soon as the transition is started but before it ends. In addition, a method call on a transitioning component will be ignored.