蓝色天空.知识 · 实践 · 创造
我的收藏
jQuery EasyUI 教程目录 68章
课程介绍
jQuery EasyUI / LEARN BY DOING

jEasyUI 创建 XP 风格左侧面板

核对多组可折叠 easyui-panel 的声明式配置(collapsible、collapsed)及通过覆写 .panel-body、.panel-header 与工具图标 CSS 实现 XP 左侧任务栏外观的离线实测。

学完这一章,你能够

  • 核对多组可折叠 easyui-panel 的声明式配置(collapsible、collapsed)及通过覆写 .panel-body、.panel-header 与工具图标 CSS 实现 XP 左侧任务栏外观的离线实测。

XP 左侧任务面板结构与面板定义

经典 Windows XP 资源管理器左侧常见任务栏由多个纵向堆叠、可独立折叠与展开的任务卡片组成。借助 jQuery EasyUI 的 Panel(面板)组件,仅用声明式 HTML 与少量 CSS 覆盖即可还原这一交互结构。

首先在背景为 `#7190E0`、宽度 `200px`、内边距 `5px` 的外层侧栏容器中放置 4 个 `class="easyui-panel"` 面板:`Picture Tasks`、`File and Folder Tasks`、`Other Places` 和 `Details`。全部面板开启 `collapsible="true"` 以提供折叠/展开工具按钮,其中 `Other Places` 额外设置 `collapsed="true"` 使其初始收起。本站 Chromium 实测外层背景色计算值为 `rgb(113, 144, 224)`、宽 `200px`,4 个面板中仅 `Other Places` 初始隐藏主体并显示 `panel-tool-expand` 图标。

自定义 CSS 外观覆写与交互验证

默认 EasyUI 主题与 XP 任务栏样式存在差异,无需改动组件源码,只需重定义 4 条 CSS 规则即可定制视觉效果:将 `.panel-body` 背景设为 `#f0f0f0`,为 `.panel-header` 指定白色底与右上角不平铺背景图,并为 `.panel-tool-collapse` 与 `.panel-tool-expand` 替换向上/向下箭头图标且背景偏移设为 `0px -3px`。本站 Chromium 实测折叠 `Picture Tasks` 与展开 `Other Places` 后,两者主体可见性与图标类名准确切换,且计算样式核验 `.panel-body` 背景为 `rgb(240, 240, 240)`、`.panel-header` 为 `rgb(255, 255, 255)` 且 `no-repeat`、折叠/展开按钮背景定位均为 `0px -3px`。

示例包 `jeasyui-layout-xp.zip` 遵循来源站访问停止状态未发起网络下载,已登记于附件记录;读者可运行本地离线实验脚本核验面板折叠展开状态与 CSS 主题覆写效果。

运行 XP 风格四任务面板折叠切换与 CSS 样式覆写离线实验

含完整源码 · 181 行
展开查看收起
python
"""Offline Playwright Chromium lab for jQuery EasyUI Ch 20: XP Left Panel."""
import json
from playwright.sync_api import sync_playwright

HTML_DOC = r"""<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<title>EasyUI XP Left Panel Offline Lab</title>
<style>
  body { font-family: sans-serif; margin: 12px; }
  .xp-sidebar { width: 200px; height: auto; background: #7190E0; padding: 5px; }
  .panel { width: 200px; box-sizing: border-box; margin-bottom: 10px; border: 1px solid #95b8e7; }
  .panel-header {
    display: flex; align-items: center; justify-content: space-between;
    padding: 5px 8px; font-size: 13px; font-weight: 600; color: #1d4ed8;
    background: #fff url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='20' height='20'%3E%3Crect width='20' height='20' fill='%23dbeafe'/%3E%3C/svg%3E") no-repeat top right;
  }
  .panel-body { background: #f0f0f0; padding: 10px; font-size: 12px; line-height: 1.6; }
  .panel-body.collapsed { display: none; }
  .panel-tool-btn { width: 16px; height: 16px; border: none; cursor: pointer; display: inline-block; }
  .panel-tool-collapse {
    background: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16'%3E%3Cpath d='M3 10l5-5 5 5' stroke='%231d4ed8' fill='none'/%3E%3C/svg%3E") no-repeat 0px -3px;
  }
  .panel-tool-expand {
    background: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16'%3E%3Cpath d='M3 6l5 5 5-5' stroke='%231d4ed8' fill='none'/%3E%3C/svg%3E") no-repeat 0px -3px;
  }
</style>
</head>
<body>
<div id="xp-wrap" class="xp-sidebar">
  <div class="easyui-panel" title="Picture Tasks" collapsible="true">
    View as a slide show<br/>Order prints online<br/>Print pictures
  </div>
  <div class="easyui-panel" title="File and Folder Tasks" collapsible="true">
    Make a new folder<br/>Publish this folder to the Web<br/>Share this folder
  </div>
  <div class="easyui-panel" title="Other Places" collapsible="true" collapsed="true">
    New York<br/>My Pictures<br/>My Computer<br/>My Network Places
  </div>
  <div class="easyui-panel" title="Details" collapsible="true">
    My documents<br/>File folder<br/><br/>Date modified: Oct.3rd 2010
  </div>
</div>
<script>
(function(){
  const wrap = document.getElementById('xp-wrap');
  const rawPanels = Array.from(wrap.querySelectorAll('.easyui-panel'));
  rawPanels.forEach((src, idx) => {
    const title = src.getAttribute('title');
    const collapsible = src.getAttribute('collapsible') === 'true';
    const collapsed = src.getAttribute('collapsed') === 'true';
    const panel = document.createElement('div');
    panel.className = 'panel';
    panel.dataset.panelTitle = title;
    const header = document.createElement('div');
    header.className = 'panel-header';
    const titleSpan = document.createElement('span');
    titleSpan.textContent = title;
    header.appendChild(titleSpan);
    const body = document.createElement('div');
    body.className = 'panel-body' + (collapsed ? ' collapsed' : '');
    body.innerHTML = src.innerHTML;
    if (collapsible) {
      const btn = document.createElement('button');
      btn.type = 'button';
      btn.id = 'toggle-' + idx;
      btn.className = 'panel-tool-btn ' + (collapsed ? 'panel-tool-expand' : 'panel-tool-collapse');
      btn.addEventListener('click', () => {
        const nowCollapsed = body.classList.toggle('collapsed');
        btn.classList.toggle('panel-tool-expand', nowCollapsed);
        btn.classList.toggle('panel-tool-collapse', !nowCollapsed);
      });
      header.appendChild(btn);
    }
    panel.appendChild(header);
    panel.appendChild(body);
    src.replaceWith(panel);
  });
})();
</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": 500, "height": 600})
        page.set_content(HTML_DOC)

        # Case 1: Four XP panels initial collapsed/expanded state & sidebar container
        res1 = page.evaluate("""() => {
            const wrapStyle = getComputedStyle(document.getElementById('xp-wrap'));
            const panels = Array.from(document.querySelectorAll('.panel')).map(el => ({
                title: el.dataset.panelTitle,
                bodyVisible: getComputedStyle(el.querySelector('.panel-body')).display !== 'none',
                toolClass: el.querySelector('.panel-tool-btn').classList.contains('panel-tool-expand') ? 'panel-tool-expand' : 'panel-tool-collapse',
            }));
            return {
                sidebarBg: wrapStyle.backgroundColor,
                sidebarWidth: wrapStyle.width,
                panelCount: panels.length,
                panels,
            };
        }""")
        exp1 = {
            "sidebarBg": "rgb(113, 144, 224)",
            "sidebarWidth": "200px",
            "panelCount": 4,
            "panels": [
                {"title": "Picture Tasks", "bodyVisible": True, "toolClass": "panel-tool-collapse"},
                {"title": "File and Folder Tasks", "bodyVisible": True, "toolClass": "panel-tool-collapse"},
                {"title": "Other Places", "bodyVisible": False, "toolClass": "panel-tool-expand"},
                {"title": "Details", "bodyVisible": True, "toolClass": "panel-tool-collapse"},
            ],
        }
        cases.append(dict(name="four_xp_panels_initial_state", expected=exp1, actual=res1, passed=(res1 == exp1)))

        # Case 2: Toggle Picture Tasks collapse & Other Places expand
        page.click("#toggle-0")
        page.click("#toggle-2")
        res2 = page.evaluate("""() => {
            const p0 = document.querySelector('.panel[data-panel-title="Picture Tasks"]');
            const p2 = document.querySelector('.panel[data-panel-title="Other Places"]');
            return {
                pictureTasksVisible: getComputedStyle(p0.querySelector('.panel-body')).display !== 'none',
                pictureTasksIcon: p0.querySelector('.panel-tool-btn').classList.contains('panel-tool-expand') ? 'panel-tool-expand' : 'panel-tool-collapse',
                otherPlacesVisible: getComputedStyle(p2.querySelector('.panel-body')).display !== 'none',
                otherPlacesIcon: p2.querySelector('.panel-tool-btn').classList.contains('panel-tool-collapse') ? 'panel-tool-collapse' : 'panel-tool-expand',
                otherPlacesItems: p2.querySelector('.panel-body').innerText.split('\\n').map(s => s.trim()).filter(Boolean),
            };
        }""")
        exp2 = {
            "pictureTasksVisible": False,
            "pictureTasksIcon": "panel-tool-expand",
            "otherPlacesVisible": True,
            "otherPlacesIcon": "panel-tool-collapse",
            "otherPlacesItems": ["New York", "My Pictures", "My Computer", "My Network Places"],
        }
        cases.append(dict(name="toggle_collapse_and_expand_interaction", expected=exp2, actual=res2, passed=(res2 == exp2)))

        # Case 3: Custom XP CSS overrides on panel-body, panel-header, tool icons
        res3 = page.evaluate("""() => {
            const bodyStyle = getComputedStyle(document.querySelector('.panel-body:not(.collapsed)'));
            const headerStyle = getComputedStyle(document.querySelector('.panel-header'));
            const collapseStyle = getComputedStyle(document.querySelector('.panel-tool-collapse'));
            const expandStyle = getComputedStyle(document.querySelector('.panel-tool-expand'));
            return {
                bodyBg: bodyStyle.backgroundColor,
                headerBgColor: headerStyle.backgroundColor,
                headerBgRepeat: headerStyle.backgroundRepeat,
                collapsePos: collapseStyle.backgroundPosition,
                expandPos: expandStyle.backgroundPosition,
            };
        }""")
        exp3 = {
            "bodyBg": "rgb(240, 240, 240)",
            "headerBgColor": "rgb(255, 255, 255)",
            "headerBgRepeat": "no-repeat",
            "collapsePos": "0px -3px",
            "expandPos": "0px -3px",
        }
        cases.append(dict(name="custom_xp_css_theme_override", expected=exp3, actual=res3, passed=(res3 == exp3)))

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


if __name__ == "__main__":
    main()
核对 4 个任务面板初始展开/折叠状态、点击切换可见性与图标类名,以及 .panel-body/.panel-header/.panel-tool-* 计算样式值。

查看独立实例 ↗ 在独立页面查看源码、运行环境与工程包

本地运行

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

下载本章实验包 (mbskycom202612a964.zip) ↓ 查看运行说明 ↗

本章附件与实例包

下载课程关联工程包:jQuery EasyUI

课程关联工程包(登记关联68章) · 含当前章节或实例 · mbskycom202612a964.zip

下载课程关联工程包:jQuery EasyUI

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1006.zip

常见错误与边界检查

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

技术规范与官方资料

练习与参考解答

查看参考解答

通过外部自定义样式表覆盖组件类名可以与框架版本解耦,避免升级 EasyUI 时自定义改动被覆盖,同时便于按页面或容器作用域局部定制皮肤。

SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习