复选框列表是前端开发中常用的功能之一,而 Material Design 是 Google 推出的一套设计语言,它强调纸片、阴影和动画等元素,使得界面更加美观、易用。本文将介绍如何使用 Material Design 实现复选框列表,包括 HTML、CSS 和 JavaScript 的实现细节。同时,本文还提供了示例代码,供读者参考。
HTML 结构
首先,我们需要在 HTML 中定义一个列表,用来展示复选框选项。HTML 中的结构如下:
// javascriptcn.com 代码示例 <ul class="mdc-list"> <li class="mdc-list-item"> <span class="mdc-list-item__ripple"></span> <span class="mdc-list-item__text">选项1</span> <div class="mdc-checkbox"> <input type="checkbox" class="mdc-checkbox__native-control" id="checkbox-1" /> <div class="mdc-checkbox__background"> <svg class="mdc-checkbox__checkmark" viewBox="0 0 24 24"> <path class="mdc-checkbox__checkmark__path" fill="none" d="M1.73,12.91 8.1,19.28 22.79,4.59" /> </svg> <div class="mdc-checkbox__mixedmark"></div> </div> </div> </li> <li class="mdc-list-item"> <span class="mdc-list-item__ripple"></span> <span class="mdc-list-item__text">选项2</span> <div class="mdc-checkbox"> <input type="checkbox" class="mdc-checkbox__native-control" id="checkbox-2" /> <div class="mdc-checkbox__background"> <svg class="mdc-checkbox__checkmark" viewBox="0 0 24 24"> <path class="mdc-checkbox__checkmark__path" fill="none" d="M1.73,12.91 8.1,19.28 22.79,4.59" /> </svg> <div class="mdc-checkbox__mixedmark"></div> </div> </div> </li> <li class="mdc-list-item"> <span class="mdc-list-item__ripple"></span> <span class="mdc-list-item__text">选项3</span> <div class="mdc-checkbox"> <input type="checkbox" class="mdc-checkbox__native-control" id="checkbox-3" /> <div class="mdc-checkbox__background"> <svg class="mdc-checkbox__checkmark" viewBox="0 0 24 24"> <path class="mdc-checkbox__checkmark__path" fill="none" d="M1.73,12.91 8.1,19.28 22.79,4.59" /> </svg> <div class="mdc-checkbox__mixedmark"></div> </div> </div> </li> </ul>
以上代码定义了一个 ul
元素,其中每个 li
元素都包含了一个复选框和一个文本标签。复选框使用了 Material Design 提供的样式,包括 mdc-checkbox
和 mdc-checkbox__background
。同时,为了实现点击效果,我们还需要添加 mdc-list-item__ripple
类。
CSS 样式
接下来,我们需要为复选框列表添加一些 CSS 样式,以使其更加美观和易用。CSS 样式如下:
// javascriptcn.com 代码示例 .mdc-list { padding: 0; margin: 0; list-style: none; } .mdc-list-item { display: flex; align-items: center; cursor: pointer; height: 48px; padding: 0 16px; } .mdc-list-item__ripple { position: absolute; left: 0; top: 0; right: 0; bottom: 0; border-radius: 50%; opacity: 0; pointer-events: none; transform: scale(0); transition: opacity 15ms linear, transform 300ms cubic-bezier(0, 0, 0.2, 1); background-color: rgba(0, 0, 0, 0.08); } .mdc-list-item__ripple.mdc-ripple-upgraded { opacity: 0.16; transform: scale(1); } .mdc-list-item__text { flex: 1; } .mdc-checkbox { position: relative; display: inline-block; width: 18px; height: 18px; vertical-align: middle; } .mdc-checkbox__native-control { position: absolute; left: -9999px; } .mdc-checkbox__background { position: absolute; top: 0; left: 0; width: 100%; height: 100%; border: 2px solid #000; border-radius: 2px; background-color: #fff; } .mdc-checkbox__mixedmark { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); width: 12px; height: 12px; border: 2px solid #000; border-radius: 2px; background-color: #000; opacity: 0; transition: opacity 150ms linear; } .mdc-checkbox__native-control:checked + .mdc-checkbox__background { background-color: #000; border-color: #000; } .mdc-checkbox__native-control:checked + .mdc-checkbox__background .mdc-checkbox__checkmark { stroke-dashoffset: 0; } .mdc-checkbox__native-control:checked + .mdc-checkbox__background .mdc-checkbox__mixedmark { opacity: 1; } .mdc-checkbox__checkmark { position: absolute; top: 0; left: 0; width: 100%; height: 100%; fill: none; stroke: #fff; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; stroke-dasharray: 24; stroke-dashoffset: 24; transition: stroke-dashoffset 150ms linear; }
以上代码实现了复选框列表的样式,包括列表项、文本标签和复选框的样式。特别地,我们使用了 mdc-ripple
类实现了点击效果,同时使用了 mdc-checkbox
类实现了复选框的样式。
JavaScript 交互
最后,我们需要使用 JavaScript 实现复选框列表的交互功能,包括复选框的选中和取消选中。JavaScript 代码如下:
const checkboxes = document.querySelectorAll('.mdc-checkbox__native-control'); for (let i = 0; i < checkboxes.length; i++) { checkboxes[i].addEventListener('change', function(event) { const listItem = event.target.closest('.mdc-list-item'); listItem.classList.toggle('mdc-list-item--selected'); }); }
以上代码使用了 querySelectorAll
方法选中了所有的复选框元素,然后使用 addEventListener
方法为每个复选框添加了一个 change
事件监听器。当复选框的选中状态发生变化时,我们使用 closest
方法找到最近的列表项元素,然后使用 classList
对其进行样式的切换。
示例代码
最后,我们将以上所有的 HTML、CSS 和 JavaScript 代码整合到一起,形成一个完整的示例。读者可以将其复制到本地环境中,进行调试和学习。
// javascriptcn.com 代码示例 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Material Design 复选框列表</title> <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500"> <link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@material/list@3.2.0/dist/mdc.list.min.css"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@material/ripple@3.2.0/dist/mdc.ripple.min.css"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@material/checkbox@3.2.0/dist/mdc.checkbox.min.css"> <style> .mdc-list { padding: 0; margin: 0; list-style: none; } .mdc-list-item { display: flex; align-items: center; cursor: pointer; height: 48px; padding: 0 16px; } .mdc-list-item__ripple { position: absolute; left: 0; top: 0; right: 0; bottom: 0; border-radius: 50%; opacity: 0; pointer-events: none; transform: scale(0); transition: opacity 15ms linear, transform 300ms cubic-bezier(0, 0, 0.2, 1); background-color: rgba(0, 0, 0, 0.08); } .mdc-list-item__ripple.mdc-ripple-upgraded { opacity: 0.16; transform: scale(1); } .mdc-list-item__text { flex: 1; } .mdc-checkbox { position: relative; display: inline-block; width: 18px; height: 18px; vertical-align: middle; } .mdc-checkbox__native-control { position: absolute; left: -9999px; } .mdc-checkbox__background { position: absolute; top: 0; left: 0; width: 100%; height: 100%; border: 2px solid #000; border-radius: 2px; background-color: #fff; } .mdc-checkbox__mixedmark { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); width: 12px; height: 12px; border: 2px solid #000; border-radius: 2px; background-color: #000; opacity: 0; transition: opacity 150ms linear; } .mdc-checkbox__native-control:checked + .mdc-checkbox__background { background-color: #000; border-color: #000; } .mdc-checkbox__native-control:checked + .mdc-checkbox__background .mdc-checkbox__checkmark { stroke-dashoffset: 0; } .mdc-checkbox__native-control:checked + .mdc-checkbox__background .mdc-checkbox__mixedmark { opacity: 1; } .mdc-checkbox__checkmark { position: absolute; top: 0; left: 0; width: 100%; height: 100%; fill: none; stroke: #fff; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; stroke-dasharray: 24; stroke-dashoffset: 24; transition: stroke-dashoffset 150ms linear; } </style> </head> <body> <ul class="mdc-list"> <li class="mdc-list-item"> <span class="mdc-list-item__ripple"></span> <span class="mdc-list-item__text">选项1</span> <div class="mdc-checkbox"> <input type="checkbox" class="mdc-checkbox__native-control" id="checkbox-1" /> <div class="mdc-checkbox__background"> <svg class="mdc-checkbox__checkmark" viewBox="0 0 24 24"> <path class="mdc-checkbox__checkmark__path" fill="none" d="M1.73,12.91 8.1,19.28 22.79,4.59" /> </svg> <div class="mdc-checkbox__mixedmark"></div> </div> </div> </li> <li class="mdc-list-item"> <span class="mdc-list-item__ripple"></span> <span class="mdc-list-item__text">选项2</span> <div class="mdc-checkbox"> <input type="checkbox" class="mdc-checkbox__native-control" id="checkbox-2" /> <div class="mdc-checkbox__background"> <svg class="mdc-checkbox__checkmark" viewBox="0 0 24 24"> <path class="mdc-checkbox__checkmark__path" fill="none" d="M1.73,12.91 8.1,19.28 22.79,4.59" /> </svg> <div class="mdc-checkbox__mixedmark"></div> </div> </div> </li> <li class="mdc-list-item"> <span class="mdc-list-item__ripple"></span> <span class="mdc-list-item__text">选项3</span> <div class="mdc-checkbox"> <input type="checkbox" class="mdc-checkbox__native-control" id="checkbox-3" /> <div class="mdc-checkbox__background"> <svg class="mdc-checkbox__checkmark" viewBox="0 0 24 24"> <path class="mdc-checkbox__checkmark__path" fill="none" d="M1.73,12.91 8.1,19.28 22.79,4.59" /> </svg> <div class="mdc-checkbox__mixedmark"></div> </div> </div> </li> </ul> <script src="https://cdn.jsdelivr.net/npm/@material/ripple@3.2.0/dist/mdc.ripple.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/@material/list@3.2.0/dist/mdc.list.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/@material/checkbox@3.2.0/dist/mdc.checkbox.min.js"></script> <script> const checkboxes = document.querySelectorAll('.mdc-checkbox__native-control'); for (let i = 0; i < checkboxes.length; i++) { checkboxes[i].addEventListener('change', function(event) { const listItem = event.target.closest('.mdc-list-item'); listItem.classList.toggle('mdc-list-item--selected'); }); } mdc.ripple.MDCRipple.attachTo(document.querySelector('.mdc-list-item__ripple')); mdc.checkbox.MDCCheckbox.attachTo(document.querySelector('.mdc-checkbox')); </script> </body> </html>
总结
本文介绍了如何使用 Material Design 实现复选框列表,包括 HTML、CSS 和 JavaScript 的实现细节。同时,本文提供了示例代码,供读者参考。通过本文的学习,读者可以了解到 Material Design 的设计理念和样式,以及如何使用 JavaScript 实现复选框列表的交互功能。
来源:JavaScript中文网 ,转载请注明来源 本文地址:https://www.javascriptcn.com/post/656a80ebd2f5e1655d2e8e55