蓝色天空网mbsky.com · 知识 · 实践 · 创造Foundation · 本地阅读预览
教程 / 前端开发 / Foundation

Foundation 教程

系统梳理 Foundation 5 移动优先响应式前端框架的核心定位、面板与三列栅格入门结构,以及 13 类内置 UI 组件的离线 Chromium 实测行为。

本站独立整理的内容草稿。实例显示的是预期结果;浏览器、服务端运行与技术内容验收尚待完成,本页只用于阅读和图文核对。

学完这一章,你能够

Foundation 5 框架定位与移动优先理念

Foundation 5 是一套面向响应式 Web 开发的 HTML、CSS 与 JavaScript 前端框架,以“移动优先(Mobile First)”作为默认布局策略,既兼顾轻量易用,又提供灵活的栅格与交互组件,可适配手机、平板与桌面等多种终端。

学习 Foundation 5 时,最直观的入口是将 `.row`、`.columns`、`.panel` 与 `.button` 组合到一个可随时缩放视口的页面中实时对照效果。本站在离线 Playwright Chromium 中按 Foundation 5 样式规则实测入门示例:顶部 `.panel` 呈现浅灰背景 `rgb(242, 242, 242)`、`20px` 内边距与 `1px solid rgb(216, 216, 216)` 边框,内部 `.button.small` 背景为 `rgb(0, 140, 186)`、字号 `13px`、内边距 `14px 28px`;下方的三组 `.medium-4.columns` 在 `480px` 移动端视口下纵向堆叠且均占满 `480px`,扩展至 `1024px` 视口后在最大宽度 `1000px`(`62.5rem`)的 `.row` 内横向三等分并排,单列宽 `333px` 且左侧列间隙内边距为 `15px`。

核心视觉与布局组件概览

Foundation 5 将常见界面元素封装为可组合的语义类名:按钮(`.button` 及色彩/尺寸修饰类)用于触发操作;表格(`<table>`)默认附带浅灰表头与偶数行斑马条纹;缩略图/图片弹窗(`.th` 与 Clearing Lightbox)支持图文画廊浏览;提醒框(`.alert-box`)用于展示状态提示并支持关闭交互。

在页面结构与状态反馈方面,12 列响应式网格(`.row` + `.small-*`/`.medium-*`/`.large-*`)控制跨设备列宽,进度条(`.progress` + `.meter`)以百分比直观呈现任务进度,面板(`.panel`)用于突出信息区块。本站离线实测核对表格偶数行背景为 `rgb(249, 249, 249)`,设置 `width:65%` 的 `.meter` 占轨道内部可用宽度的 `65%`。

交互型 JavaScript 组件与 DOM 状态切换

除纯 CSS 样式外,Foundation 5 还内置下拉菜单(Dropdown)、手风琴折叠面板(Accordion)、顶部导航栏(Top Bar)、模态框(Reveal Modal)、拨动开关(Switch)与范围滑块(Range Slider)等交互组件,统一通过 `data-*` 属性与 `$(document).foundation()` 初始化。本站离线交互测试验证了核心 DOM 状态切换:点击触发器后,下拉菜单 `.f-dropdown`、手风琴内容区 `.content` 与模态框 `.reveal-modal` 均由 `display: none` 切换为 `display: block`,点击提醒框 `.close` 后对应 `[data-alert]` 节点立即从 DOM 中移除。

离线验证 Foundation 5 入门页面、响应式栅格断点与交互组件状态

python · 阅读源码
"""Offline Playwright Chromium lab for Foundation Ch 01 (tutorial)."""
import json
from playwright.sync_api import sync_playwright

FOUNDATION_CSS = """
*, *::before, *::after { box-sizing: border-box; }
html { font-size: 16px; }
body { margin: 0; font-family: "Helvetica Neue", Helvetica, Roboto, Arial, sans-serif; font-size: 1rem; line-height: 1.5; color: #222222; background: #ffffff; }
.row { width: 100%; margin: 0 auto; max-width: 62.5rem; }
.row::before, .row::after { content: " "; display: table; }
.row::after { clear: both; }
.columns { position: relative; padding-left: 0.9375rem; padding-right: 0.9375rem; width: 100%; float: left; }
@media only screen and (min-width: 40.0625em) {
  .medium-4 { width: 33.33333%; }
  .medium-6 { width: 50%; }
  .medium-12 { width: 100%; }
}
.panel { border: 1px solid #d8d8d8; margin-bottom: 1.25rem; padding: 1.25rem; background: #f2f2f2; color: #333333; }
.button { display: inline-block; background-color: #008CBA; color: #ffffff; padding: 1rem 2rem 1.0625rem 2rem; font-size: 1rem; border: 0; cursor: pointer; text-decoration: none; }
.button.small { padding: 0.875rem 1.75rem 0.9375rem 1.75rem; font-size: 0.8125rem; }
.alert-box { display: block; width: 100%; padding: 0.875rem 1.5rem 0.875rem 0.875rem; margin-bottom: 1rem; background: #43AC6A; color: #ffffff; position: relative; font-size: 0.8125rem; }
.alert-box .close { position: absolute; right: 0.5rem; top: 0.5rem; color: #ffffff; text-decoration: none; cursor: pointer; }
.progress { background: #f6f6f6; height: 1.5625rem; border: 1px solid #ffffff; padding: 0.125rem; width: 100%; }
.progress .meter { background: #008CBA; height: 100%; display: block; }
table { width: 100%; background: #ffffff; border: 1px solid #dddddd; border-collapse: separate; border-spacing: 0; }
table thead { background: #f5f5f5; }
table tr:nth-of-type(even) { background: #f9f9f9; }
table th, table td { padding: 0.5rem 0.625rem; text-align: left; font-size: 0.875rem; }
.f-dropdown { display: none; list-style: none; margin: 0; padding: 0; border: 1px solid #cccccc; background: #ffffff; width: 180px; }
.f-dropdown.open { display: block; }
.accordion-navigation .content { display: none; padding: 0.75rem; }
.accordion-navigation.active .content { display: block; }
.reveal-modal { display: none; padding: 1.5rem; background: #ffffff; border: 1px solid #666666; }
.reveal-modal.open { display: block; }
"""

HTML_PAGE = f"""<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>{FOUNDATION_CSS}</style>
</head>
<body>
  <div id="hero-row" class="row">
    <div id="hero-col" class="medium-12 columns">
      <div id="hero-panel" class="panel">
        <h1>Foundation 页面</h1>
        <p>调整浏览器窗口大小以验证移动优先响应式栅格。</p>
        <button id="hero-btn" type="button" class="button small">交互按钮</button>
      </div>
    </div>
  </div>
  <div id="grid-row" class="row">
    <div id="col1" class="medium-4 columns"><h3>栅格第一列</h3><p>移动端默认堆叠,中等屏起三列并排。</p></div>
    <div id="col2" class="medium-4 columns"><h3>栅格第二列</h3><p>左右各 15px 内边距形成 30px 列间隙。</p></div>
    <div id="col3" class="medium-4 columns"><h3>栅格第三列</h3><p>行容器最大宽度为 62.5rem(1000px)。</p></div>
  </div>
  <section id="feature-preview" class="row">
    <div class="medium-12 columns">
      <div id="demo-alert" data-alert class="alert-box">操作成功提示<a id="alert-close" href="#" class="close">&times;</a></div>
      <div id="demo-progress" class="progress"><span id="demo-meter" class="meter" style="width:65%"></span></div>
      <table id="demo-table">
        <thead><tr><th>组件</th><th>核心类</th></tr></thead>
        <tbody>
          <tr id="tr-odd"><td>面板</td><td>.panel</td></tr>
          <tr id="tr-even"><td>按钮</td><td>.button</td></tr>
        </tbody>
      </table>
      <button id="dd-trigger" type="button" class="button small">展开菜单</button>
      <ul id="dd-menu" class="f-dropdown"><li>选项 A</li><li>选项 B</li></ul>
      <div id="acc-item" class="accordion-navigation">
        <a id="acc-toggle" href="#">手风琴面板</a>
        <div id="acc-content" class="content">展开后的折叠说明内容</div>
      </div>
      <button id="modal-open" type="button" class="button small">打开模态框</button>
      <div id="demo-modal" class="reveal-modal">模态对话框正文</div>
    </div>
  </section>
  <script>
    document.getElementById('alert-close').addEventListener('click', (e) => {{
      e.preventDefault();
      document.getElementById('demo-alert').remove();
    }});
    document.getElementById('dd-trigger').addEventListener('click', () => {{
      document.getElementById('dd-menu').classList.toggle('open');
    }});
    document.getElementById('acc-toggle').addEventListener('click', (e) => {{
      e.preventDefault();
      document.getElementById('acc-item').classList.toggle('active');
    }});
    document.getElementById('modal-open').addEventListener('click', () => {{
      document.getElementById('demo-modal').classList.add('open');
    }});
  </script>
</body>
</html>
"""


def launch_browser(p):
    try:
        return p.chromium.launch(headless=True)
    except Exception:
        return p.chromium.launch(headless=True, channel="chrome")


def main():
    cases = []
    with sync_playwright() as p:
        browser = launch_browser(p)
        page = browser.new_page(viewport={"width": 480, "height": 900})
        page.set_content(HTML_PAGE)

        # Case 1: hero panel and small button styles
        actual_1 = page.evaluate("""() => {
            const panel = getComputedStyle(document.getElementById('hero-panel'));
            const btn = getComputedStyle(document.getElementById('hero-btn'));
            return {
                panel_bg: panel.backgroundColor,
                panel_padding: panel.paddingTop,
                panel_border: panel.borderTopWidth + ' ' + panel.borderTopStyle + ' ' + panel.borderTopColor,
                btn_bg: btn.backgroundColor,
                btn_font_size: btn.fontSize,
                btn_padding: btn.paddingTop + ' ' + btn.paddingRight
            };
        }""")
        expected_1 = {
            "panel_bg": "rgb(242, 242, 242)",
            "panel_padding": "20px",
            "panel_border": "1px solid rgb(216, 216, 216)",
            "btn_bg": "rgb(0, 140, 186)",
            "btn_font_size": "13px",
            "btn_padding": "14px 28px",
        }
        cases.append({
            "name": "hero_panel_and_small_button",
            "expected": expected_1,
            "actual": actual_1,
            "passed": actual_1 == expected_1,
        })

        # Case 2: mobile-first stacking at 480px vs 3-column horizontal grid at 1024px
        mobile = page.evaluate("""() => {
            const c1 = document.getElementById('col1').getBoundingClientRect();
            const c2 = document.getElementById('col2').getBoundingClientRect();
            const c3 = document.getElementById('col3').getBoundingClientRect();
            return {
                widths: [Math.round(c1.width), Math.round(c2.width), Math.round(c3.width)],
                stacked: c2.top > c1.top && c3.top > c2.top
            };
        }""")
        page.set_viewport_size({"width": 1024, "height": 900})
        desktop = page.evaluate("""() => {
            const row = document.getElementById('grid-row').getBoundingClientRect();
            const c1 = document.getElementById('col1').getBoundingClientRect();
            const c2 = document.getElementById('col2').getBoundingClientRect();
            const c3 = document.getElementById('col3').getBoundingClientRect();
            const pad = getComputedStyle(document.getElementById('col1')).paddingLeft;
            return {
                row_width: Math.round(row.width),
                widths: [Math.round(c1.width), Math.round(c2.width), Math.round(c3.width)],
                same_row: Math.abs(c1.top - c2.top) < 2 && Math.abs(c2.top - c3.top) < 2,
                col_padding_left: pad
            };
        }""")
        actual_2 = {"mobile_480": mobile, "desktop_1024": desktop}
        expected_2 = {
            "mobile_480": {"widths": [480, 480, 480], "stacked": True},
            "desktop_1024": {"row_width": 1000, "widths": [333, 333, 333], "same_row": True, "col_padding_left": "15px"},
        }
        cases.append({
            "name": "mobile_first_grid_breakpoint_switch",
            "expected": expected_2,
            "actual": actual_2,
            "passed": actual_2 == expected_2,
        })

        # Case 3: core visual components (table zebra, progress meter width)
        actual_3 = page.evaluate("""() => {
            const evenBg = getComputedStyle(document.getElementById('tr-even')).backgroundColor;
            const progW = document.getElementById('demo-progress').getBoundingClientRect().width;
            const meterW = document.getElementById('demo-meter').getBoundingClientRect().width;
            return {
                table_even_row_bg: evenBg,
                meter_ratio_pct: Math.round((meterW / (progW - 6)) * 100)
            };
        }""")
        expected_3 = {
            "table_even_row_bg": "rgb(249, 249, 249)",
            "meter_ratio_pct": 65,
        }
        cases.append({
            "name": "component_preview_table_and_progress",
            "expected": expected_3,
            "actual": actual_3,
            "passed": actual_3 == expected_3,
        })

        # Case 4: interactive UI state transitions (dropdown, accordion, modal, alert close)
        before_4 = page.evaluate("""() => ({
            dropdown_display: getComputedStyle(document.getElementById('dd-menu')).display,
            accordion_display: getComputedStyle(document.getElementById('acc-content')).display,
            modal_display: getComputedStyle(document.getElementById('demo-modal')).display,
            alert_exists: !!document.getElementById('demo-alert')
        })""")
        page.click("#dd-trigger")
        page.click("#acc-toggle")
        page.click("#modal-open")
        page.click("#alert-close")
        after_4 = page.evaluate("""() => ({
            dropdown_display: getComputedStyle(document.getElementById('dd-menu')).display,
            accordion_display: getComputedStyle(document.getElementById('acc-content')).display,
            modal_display: getComputedStyle(document.getElementById('demo-modal')).display,
            alert_exists: !!document.getElementById('demo-alert')
        })""")
        actual_4 = {"before": before_4, "after": after_4}
        expected_4 = {
            "before": {"dropdown_display": "none", "accordion_display": "none", "modal_display": "none", "alert_exists": True},
            "after": {"dropdown_display": "block", "accordion_display": "block", "modal_display": "block", "alert_exists": False},
        }
        cases.append({
            "name": "interactive_components_dom_states",
            "expected": expected_4,
            "actual": actual_4,
            "passed": actual_4 == expected_4,
        })

        browser.close()
    print(json.dumps({"cases": cases}, ensure_ascii=False, indent=2))


if __name__ == "__main__":
    main()
预期结果 · 待实际运行核验
  • 在离线 Chromium 中核对 .panel/.button.small 计算样式、480px 堆叠与 1024px 三等分栅格、表格斑马条纹与 65% 进度条,以及下拉/手风琴/模态框/提醒框关闭状态切换。
离线验证 Foundation 5 入门页面、响应式栅格断点与交互组件状态

所需环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。

当前源码 SHA256:6123e2e8016888106872e63a231368d3ef9d2f08b6d5202571c3feac38024fea

下载本站示例源码

本地运行

解压后按 README 说明在对应本地环境运行脚本。

下载Foundation 教程实验

所需环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。

ZIP SHA256:37cb6191b1e468cfc62592271d907349fcfc003da2d7bfa403a0465d7ecd8ecd

常见错误与边界

  • 实际端口、文档资源与应用配置都应和测试条件一致。

练习与解答

在 Foundation 5 的移动优先网格中,只给列元素设置 .medium-4.columns 时,小屏手机与中等及以上桌面端会分别如何排版?

展开参考解答

未指定 .small-* 时,小屏(<641px)默认按 100% 宽度纵向堆叠;视口达到 medium 断点(≥641px,40.0625em)后,.medium-4 生效并按 33.33333% 宽度横向三列并排。

规范与官方依据