针对设备的设计
本文档概述了用于让 Discourse 的用户界面适配不同设备的 API。
触摸与悬停
有些设备只有触摸屏,有些只有传统的鼠标指针,还有些两者兼有。重要的是,触摸屏用户无法"悬停"在元素上。因此,界面的设计应能在完全没有悬停状态的情况下正常工作,并为支持悬停的设备添加悬停相关的增强功能。
有多种通过 CSS 和 JavaScript 检测触摸/悬停能力的方法。为保持一致性,我们推荐使用 Discourse 的辅助方法,而不是直接使用那些 CSS/JS API。
对于 CSS,你可以针对 .discourse-touch 和 .discourse-no-touch 类,它们会被添加到 <html> 元素上。这些类根据 (any-pointer: coarse) 媒体查询来确定。
例如:
html.discourse-touch {
// SCSS rules here will apply to devices with a touch screen,
// including mobiles/tablets and laptops/desktops with touch screens.
}
html.discourse-no-touch {
// SCSS rules here will apply to devices with no touch screen.
}
此信息也可通过 capabilities 服务在 Ember 组件中获取:
import Component from "@glimmer/component";
import { service } from "@ember/service";
class MyComponent extends Component {
@service capabilities;
<template>
{{#if this.capabilities.touch}}
This text will be displayed for devices with a touch screen
{{/if}}
{{#unless this.capabilities.touch}}
This text will be displayed for devices with no touch screen
{{/unless}}
</template>
}
旧版移动 / 桌面模式
过去,Discourse 基于浏览器的 user-agent,为"移动"和"桌面"视图提供两套完全不同的布局和样式表。开发者会通过把 CSS 放入特定的移动/桌面目录、使用 .mobile-view/.desktop-view HTML 类,以及 JavaScript 中的 site.mobileView 布尔值来针对这些模式。
这些技术现在被视为已弃用,应替换为下一份文档中讨论的基于视口和能力的策略。出于向后兼容的考虑,当视口大于/小于 sm 阈值时,会使用旧版桌面/移动 CSS。
另请参阅
- 针对响应式宽度进行设计 — 断点、视口大小和容器查询。
- 使用 BEM 的 CSS 类准则 — CSS 类命名约定。
这篇文档对你有帮助吗?
谢谢你的反馈!