W3schools.cn 在线教程 - 编程参考手册

HowTo 操作方法

HowTo Home

Menus

Icon Bar Menu Icon Accordion Tabs Vertical Tabs Tab Headers Full Page Tabs Hover Tabs Top Navigation Responsive Topnav Navbar with Icons Search Menu Search Bar Fixed Sidebar Side Navigation Responsive Sidebar Fullscreen Navigation Off-Canvas Menu Hover Sidenav Buttons Sidebar with Icons Horizontal Scroll Menu Vertical Menu Bottom Navigation Responsive Bottom Nav Bottom Border Nav Links Right Aligned Menu Links Centered Menu Link Equal Width Menu Links Fixed Menu Slide Down Bar on Scroll Hide Navbar on Scroll Shrink Navbar on Scroll Sticky Navbar Navbar on Image Hover Dropdowns Click Dropdowns Cascading Dropdown Dropdown in Topnav Dropdown in Sidenav Resp Navbar Dropdown Subnavigation Menu Dropup Mega Menu Mobile Menu Curtain Menu Collapsed Sidebar Collapsed Sidepanel Pagination Breadcrumbs Button Group Vertical Button Group Sticky Social Bar Pill Navigation Responsive Header

Images

Slideshow Slideshow Gallery Modal Images Lightbox Responsive Image Grid Image Grid Tab Gallery Image Overlay Fade Image Overlay Slide Image Overlay Zoom Image Overlay Title Image Overlay Icon Image Effects Black and White Image Image Text Image Text Blocks Transparent Image Text Full Page Image Form on Image Hero Image Blur Background Image Change Bg on Scroll Side-by-Side Images Rounded Images Avatar Images Responsive Images Center Images Thumbnails Border Around Image Meet the Team Sticky Image Flip an Image Shake an Image Portfolio Gallery Portfolio with Filtering Image Zoom Image Magnifier Glass Image Comparison Slider

Buttons

Alert Buttons Outline Buttons Split Buttons Animated Buttons Fading Buttons Button on Image Social Media Buttons Read More Read Less Loading Buttons Download Buttons Pill Buttons Notification Button Icon Buttons Next/prev Buttons More Button in Nav Block Buttons Text Buttons Round Buttons Scroll To Top Button

Forms

Login Form Signup Form Checkout Form Contact Form Social Login Form Register Form Form with Icons Newsletter Stacked Form Responsive Form Popup Form Inline Form Clear Input Field Hide Number Arrows Copy Text to Clipboard Animated Search Search Button Fullscreen Search Input Field in Navbar Login Form in Navbar Custom Checkbox/Radio Custom Select Toggle Switch Check Checkbox Detect Caps Lock Trigger Button on Enter Password Validation Toggle Password Visibility Multiple Step Form Autocomplete Turn off autocomplete Turn off spellcheck File Upload Button Empty Input Validation

Filters

Filter List Filter Table Filter Elements Filter Dropdown Sort List Sort Table

Tables

Zebra Striped Table Center Tables Responsive Tables Comparison Table

More

Fullscreen Video Modal Boxes Delete Modal Timeline Scroll Indicator Progress Bars Skill Bar Range Sliders Tooltips Display Element Hover Popups Collapsible Calendar HTML Includes To Do List Loaders Star Rating User Rating Overlay Effect Contact Chips Cards Flip Card Profile Card Product Card Alerts Callout Notes Labels Circles Style HR Coupon List Group List Without Bullets Responsive Text Cutout Text Glowing Text Fixed Footer Sticky Element Equal Height Clearfix Responsive Floats Snackbar Fullscreen Window Scroll Drawing Smooth Scroll Gradient Bg Scroll Sticky Header Shrink Header on Scroll Pricing Table Parallax Aspect Ratio Responsive Iframes Toggle Like/Dislike Toggle Hide/Show Toggle Dark Mode Toggle Text Toggle Class Add Class Remove Class Active Class Tree View Remove Property Offline Detection Find Hidden Element Redirect Webpage Zoom Hover Flip Box Center Vertically Center Button in DIV Transition on Hover Arrows Shapes Download Link Full Height Element Browser Window Custom Scrollbar Hide Scrollbar Show/Force Scrollbar Device Look Contenteditable Border Placeholder Color Text Selection Color Bullet Color Vertical Line Dividers Animate Icons Countdown Timer Typewriter Coming Soon Page Chat Messages Popup Chat Window Split Screen Testimonials Section Counter Quotes Slideshow Closable List Items Typical Device Breakpoints Draggable HTML Element JS Media Queries Syntax Highlighter JS Animations JS String Length JS Default Parameters Get Current URL Get Current Screen Size Get Iframe Elements

Website

Make a Website Make a Website (W3.CSS) Make a Website (BS3) Make a Website (BS4) Make a WebBook Center Website Contact Section About Page Big Header Example Website

Grid

2 Column Layout 3 Column Layout 4 Column Layout Expanding Grid List Grid View Mixed Column Layout Column Cards Zig Zag Layout Blog Layout

Google

Google Charts Google Fonts

Converters

Convert Weight Convert Temperature Convert Length Convert Speed


如何 - CSS/JS 模态框


了解如何使用 CSS 和 JavaScript 创建模态框。


如何创建模态框

模态框是显示在当前页面顶部的对话框/弹出窗口:

×

Modal Header

Hello World!

Modals are awesome!

Modal Footer

亲自试一试 »
步骤 1) 添加 HTML:

实例

<!-- 触发/打开模态 -->
<button id="myBtn">Open Modal</button>

<!-- 模态 -->
<div id="myModal" class="modal">

  <!-- 模态内容 -->
  <div class="modal-content">
    <span class="close">&times;</span>
    <p>Some text in the Modal..</p>
  </div>

</div>

步骤 2) 添加 CSS:

实例

/* 模态(背景) */
.modal {
  display: none; /* 默认隐藏 */
  position: fixed; /* 留在原地 */
  z-index: 1; /* Sit on top */
  left: 0;
  top: 0;
  width: 100%; /* 全屏宽度 */
  height: 100%; /* 全屏高度 */
  overflow: auto; /* 如果需要,启用滚动 */
  background-color: rgb(0,0,0); /* 后备颜色 */
  background-color: rgba(0,0,0,0.4); /* 黑色带不透明度 */
}

/* 模态内容/框 */
.modal-content {
  background-color: #fefefe;
  margin: 15% auto; /* 15% 从顶部和居中 */
  padding: 20px;
  border: 1px solid #888;
  width: 80%; /* 可能或多或少,取决于屏幕大小 */
}

/* 关闭按钮 */
.close {
  color: #aaa;
  float: right;
  font-size: 28px;
  font-weight: bold;
}

.close:hover,
.close:focus {
  color: black;
  text-decoration: none;
  cursor: pointer;
}

步骤 3) 添加 JavaScript:

实例

// 获取模态
var modal = document.getElementById("myModal");

// 获取打开模态的按钮
var btn = document.getElementById("myBtn");

// 获取关闭 modal 的 <span> 元素
var span = document.getElementsByClassName("close")[0];

// 当用户点击按钮时,打开模式
btn.onclick = function() {
  modal.style.display = "block";
}

// 当用户点击 <span>(x) 时,关闭 modal
span.onclick = function() {
  modal.style.display = "none";
}

// 当用户点击模态框外的任意位置时,将其关闭
window.onclick = function(event) {
  if (event.target == modal) {
    modal.style.display = "none";
  }
}
亲自试一试 »


添加页眉和页脚

为 modal-header、modal-body 和 modal-footer 添加一个类:

实例

<!-- 模态内容 -->
<div class="modal-content">
  <div class="modal-header">
    <span class="close">&times;</span>
    <h2>Modal Header</h2>
  </div>
  <div class="modal-body">
    <p>Some text in the Modal Body</p>
    <p>Some other text...</p>
  </div>
  <div class="modal-footer">
    <h3>Modal Footer</h3>
  </div>
</div>

设置模态页眉、正文和页脚的样式,并添加动画(在模态中滑动):

实例

/* Modal Header */
.modal-header {
  padding: 2px 16px;
  background-color: #5cb85c;
  color: white;
}

/* Modal Body */
.modal-body {padding: 2px 16px;}

/* Modal Footer */
.modal-footer {
  padding: 2px 16px;
  background-color: #5cb85c;
  color: white;
}

/* Modal Content */
.modal-content {
  position: relative;
  background-color: #fefefe;
  margin: auto;
  padding: 0;
  border: 1px solid #888;
  width: 80%;
  box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2),0 6px 20px 0 rgba(0,0,0,0.19);
  animation-name: animatetop;
  animation-duration: 0.4s
}

/* Add Animation */
@keyframes animatetop {
  from {top: -300px; opacity: 0}
  to {top: 0; opacity: 1}
}
亲自试一试 »

底部模态 ("Bottom sheets")

关于如何创建从底部滑入的全宽模态的示例:

提示: 另请查看 Modal Images 模态图像Lightbox 灯箱