Skeleton 骨架屏
用接近真实内容的占位形状表示加载中的页面,减少内容跳动和等待感。
何时使用
在线体验
基本用法
import "github.com/tangoui/tango/ui"
func skeletonPage(c *ui.Context) {
ui.Skeleton(c).Rows(3).Active().Show()
}Active() 开启 shimmer 动画,Rows(n) 设置段落行数。Show() 构建占位内容。
常见用法
头像、标题和段落
func skeletonProfile(c *ui.Context) {
ui.Skeleton(c).Avatar(false).Rows(3).Round().Show()
}Avatar(false) 添加圆形头像,传入 true 使用方形头像;Round() 让条带使用胶囊形状。
加载完成后切换真实内容
var profileLoading = true
func skeletonLoading(c *ui.Context) {
ui.Skeleton(c).Loading(profileLoading).Body(func() {
ui.Text(c, "用户资料已加载")
})
}Loading(true) 显示骨架,Loading(false) 构建 Body 中的真实内容。把加载状态放在页面级变量或 ui.State,不要在渲染函数中重置。
控件占位
func skeletonControls(c *ui.Context) {
ui.SkeletonButton(c, ui.Middle, "default", true)
ui.SkeletonButton(c, ui.Middle, "round", true)
ui.SkeletonInput(c, ui.Middle, true)
ui.SkeletonAvatar(c, 32, false, true)
ui.SkeletonImage(c, 96, 96, false)
}四个专用构造函数分别提供按钮、输入框、头像和图片形状;active 控制动画,尺寸使用 ui.Small、ui.Middle 或 ui.Large。
隐藏标题和段落
func skeletonMinimal(c *ui.Context) {
ui.Skeleton(c).NoTitle().Rows(0).Show()
}NoTitle() 隐藏标题条,Rows(0) 隐藏段落条,适合只需要局部占位的场景。
主题与样式
Skeleton 的条带高度、圆角、动画颜色和间距由主题控制。ConfigProvider.Defaults("Skeleton", ...) 支持 active 与 round 布尔默认值;单个实例的 Active、Round 优先。大量同时动画会增加绘制开销,应按可见区域拆分骨架。
注意事项
骨架形状应尽量匹配最终内容,避免加载完成后大幅跳动。加载状态来自真实请求状态;不要用固定延时伪造完成。动画应避免覆盖错误和空状态,必要时提供可读的加载说明或配合 A11y 文案。大列表优先使用虚拟化或只渲染视口附近的骨架。
示例代码
以下是上方在线演示运行的 Go 源码(来自官方 gallery),点击标题展开。函数接收的 c *ui.Context 由窗口的 UI 循环每帧传入,接入方式见 Go GUI 入门。
API
签名与说明由 site/tools/compdoc 从 Go 源码和文档注释提取;点击名称跳到 Go API 参考 的对应条目。
入口函数
ui.Skeleton
func Skeleton(c *Context) *SkeletonWSkeleton starts a skeleton with a title and paragraph rows: three rows, or two next to an avatar or without a title (antd).
ConfigProvider.Defaults("Skeleton", ...) keys: "active", "round" (bool).
返回 *SkeletonW,可链式调用的方法见 SkeletonW 方法。
ui.SkeletonAvatar
func SkeletonAvatar(c *Context, size float32, square, active bool)SkeletonAvatar adds an avatar placeholder.
ui.Button
func Button(c *Context, label string) *ButtonWButton adds a button.
返回 *ButtonW,可链式调用的方法见 ButtonW 方法。
ui.Input
func Input(c *Context, val *string) *InputWInput adds a text input bound to *val.
返回 *InputW,可链式调用的方法见 InputW 方法。
ui.Image
func Image(c *Context, img image.Image) *ImageWImage adds an image; img may be nil (shows the placeholder). The default size is the image's pixel size (200×200 when unknown); use Width/Height.
返回 *ImageW,可链式调用的方法见 ImageW 方法。
SkeletonW 方法
ui.SkeletonW:SkeletonW is a loading placeholder: optional avatar, a title bar and paragraph lines, with an optional shimmer ("active").
| 方法 | 说明 | 签名 |
|---|---|---|
Active | Active turns on the shimmer animation. | Active() *SkeletonW |
Avatar | Avatar adds a round avatar placeholder (square=true for square). | Avatar(square bool) *SkeletonW |
AvatarSize | AvatarSize sets the avatar placeholder size in px (antd avatar.size; default the large control height, 40). | AvatarSize(px float32) *SkeletonW |
Body | Body builds the skeleton while loading, else fn. | Body(fn func()) *Box |
Count | Count repeats the skeleton n times (Element count). | Count(n int) *SkeletonW |
Loading | Loading switches between the skeleton (true) and the real content. | Loading(on bool) *SkeletonW |
NoTitle | NoTitle hides the title bar. | NoTitle() *SkeletonW |
ParagraphWidth | ParagraphWidth sets the paragraph line widths (antd paragraph.width): one value sets the last line, several set the lines in order; fractions when 0 < w <= 1, else px. | ParagraphWidth(w ...float32) *SkeletonW |
Round | Round uses pill-shaped bars. | Round() *SkeletonW |
Rows | Rows sets the paragraph rows (0 hides the paragraph). | Rows(n int) *SkeletonW |
Show | Show builds the skeleton. | Show() *Box |
Throttle | Throttle delays the switch (EP throttle): the skeleton appears only after Loading(true) lasted leading and goes away only after Loading(false) lasted trailing (0: at once), so quick loads do not flash. | Throttle(leading, trailing time.Duration) *SkeletonW |
ThrottleInitial | ThrottleInitial sets what a throttled skeleton shows before its first delay ends: true the skeleton, false (default) the content (EP throttle.initVal). | ThrottleInitial(on bool) *SkeletonW |
TitleWidth | TitleWidth sets the title bar width (antd title.width): a fraction of the row when 0 < w <= 1, else px. | TitleWidth(w float32) *SkeletonW |
ButtonW 方法
ui.ButtonW:ButtonW is a button under construction; chain modifiers then query.
| 方法 | 说明 | 签名 |
|---|---|---|
A11yLabel | A11yLabel sets the accessible name read by screen readers. | A11yLabel(s string) *ButtonW |
Appearance | Appearance sets how the button wears its color (Ant v6 variant, plus Element's plain). | Appearance(a ButtonAppearance) *ButtonW |
AutoInsertSpace | AutoInsertSpace sets whether a label of exactly two Chinese characters is drawn with a space between them ("确定" shows as "确 定"; Ant / Element ConfigProvider button.autoInsertSpace). | AutoInsertSpace(on bool) *ButtonW |
Block | Block stretches the button to the container width. | Block() *ButtonW |
Circle | Circle makes an icon-only round button. | Circle() *ButtonW |
Clicked | Clicked reports activation (never when disabled or loading). | Clicked() bool |
Color | Color sets the button color (Ant v6 color): a role, an Ant preset or a hex color ("#13c2c2"). | Color(col ButtonColor) *ButtonW |
Danger | Danger() *ButtonW | |
Dashed | Dashed() *ButtonW | |
Disabled | Disabled disables the button when on. | Disabled(on bool) *ButtonW |
Ghost | Ghost() *ButtonW | |
Hovered | Hovered reports hover. | Hovered() bool |
ID | ID returns the widget ID. | ID() ID |
Icon | Icon puts an icon before the label. | Icon(ic *icon.Icon) *ButtonW |
IconPlacement | IconPlacement places the icon at the inline "start" of the label (default) or at its "end" (Ant v6 iconPlacement, which replaces iconPosition). | IconPlacement(p string) *ButtonW |
IconRight | IconRight puts the icon after the label (Icon(ic).IconPlacement("end")). | IconRight(ic *icon.Icon) *ButtonW |
Info | Info makes a solid info button (Element type="info"). | Info() *ButtonW |
Large | Large() *ButtonW | |
Layout | Layout applies layout options. | Layout(opts ...layout.Option) *ButtonW |
Link | Link() *ButtonW | |
Loading | Loading shows a spinner and blocks clicks. | Loading(on bool) *ButtonW |
LoadingDelay | LoadingDelay postpones the loading state (Ant loading.delay): after Loading(true) the button stays clickable and shows no spinner until d has passed; a loading that ends sooner never shows. | LoadingDelay(d time.Duration) *ButtonW |
LoadingIcon | LoadingIcon replaces the loading spinner with ic, drawn spinning (Ant loading.icon, Element loading-icon). | LoadingIcon(ic *icon.Icon) *ButtonW |
Plain | Plain uses Element's plain style: a light tint of the color with a tinted border, filled with the color under the pointer. | Plain() *ButtonW |
Primary | Primary() *ButtonW | |
Round | Round() *ButtonW | |
Secondary | Secondary makes a neutral filled button (shadcn "secondary"). | Secondary() *ButtonW |
Size | Size sets the control size. | Size(s ControlSize) *ButtonW |
Small | Small() *ButtonW | |
Success | Success makes a solid success button (Element type="success"); add Plain for the plain style. | Success() *ButtonW |
Text | Text() *ButtonW | |
Variant | Variant sets the variant explicitly. | Variant(v ButtonVariant) *ButtonW |
Warning | Warning makes a solid warning button (Element type="warning"). | Warning() *ButtonW |
InputW 方法
ui.InputW:InputW is a single-line text input.
| 方法 | 说明 | 签名 |
|---|---|---|
A11yLabel | A11yLabel sets the accessible name. | A11yLabel(s string) *InputW |
AllowClear | AllowClear shows a clear button when the input has a value. | AllowClear() *InputW |
Bg | Bg replaces the background color of the frame (a grey search field on a white bar). | Bg(col theme.Color) *InputW |
Borderless | Borderless drops the frame (background, border and focus halo), for inputs embedded in another surface such as the Command search row. | Borderless() *InputW |
Changed | Changed reports whether the value was edited this frame. | Changed() bool |
Disabled | Disabled disables editing. | Disabled(on bool) *InputW |
EnterKey | EnterKey labels the soft keyboard's return key (input.EnterSearch, input.EnterNext, …). | EnterKey(k input.EnterKey) *InputW |
Focus | Focus requests keyboard focus. | Focus() *InputW |
Focused | Focused reports keyboard focus. | Focused() bool |
ID | ID returns the editor's widget ID. | ID() ID |
Kind | Kind tells soft keyboards what the field holds (input.TextEmail, input.TextNumber, …): the keyboard layout and whether it suggests words. | Kind(k input.TextKind) *InputW |
Layout | Layout applies layout options to the outer frame. | Layout(opts ...layout.Option) *InputW |
MaxLength | MaxLength limits the number of runes. | MaxLength(n int) *InputW |
NoBorder | NoBorder drops the border and the focus halo but keeps the background (Bg or the variant's), e.g. a filled search bar. | NoBorder() *InputW |
Password | Password masks the value and adds a visibility toggle. | Password() *InputW |
Placeholder | Placeholder(s string) *InputW | |
Prefix | Prefix(ic *icon.Icon) *InputW | |
PrefixText | PrefixText / SuffixText show short static text (units, currency). | PrefixText(s string) *InputW |
ReadOnly | ReadOnly prevents edits but allows selection. | ReadOnly() *InputW |
SelectAll | SelectAll selects the full text of the input. | SelectAll() *InputW |
ShowCount | ShowCount shows the character count ("3 / 20" with MaxLength) at the end of the field (Ant showCount, Element show-word-limit). | ShowCount() *InputW |
Size | Size sets the control size. | Size(s ControlSize) *InputW |
Status | Status sets the validation status: "error" or "warning". | Status(s string) *InputW |
Submitted | Submitted reports an Enter key press. | Submitted() bool |
Suffix | Suffix(ic *icon.Icon) *InputW | |
SuffixText | SuffixText(s string) *InputW | |
Variant | Variant sets the look of the frame (Ant Input variant): "outlined" (default), "filled" (grey fill, border on focus), "borderless" (no frame) or "underlined" (bottom line only). | Variant(v string) *InputW |
VisibilityToggle | VisibilityToggle shows (true, the default) or hides the eye button of a Password field (antd Input.Password visibilityToggle; Element type="password" without show-password). | VisibilityToggle(on bool) *InputW |
Width | Width fixes the width. | Width(px float32) *InputW |
ImageW 方法
ui.ImageW:ImageW displays a bitmap with loading placeholder, error fallback and an optional full-screen preview (Ant Design Image / Element Plus el-image).
| 方法 | 说明 | 签名 |
|---|---|---|
A11yLabel | A11yLabel sets the accessible name (like Alt, without changing the Driver.Find label). | A11yLabel(s string) *ImageW |
Alt | Alt sets the accessible label (also used by Driver.Find). | Alt(s string) *ImageW |
CloseOnEscape | CloseOnEscape(false) keeps the preview open on Escape (Element close-on-press-escape, default true). | CloseOnEscape(on bool) *ImageW |
Failed | Failed shows the error fallback (broken picture / "加载失败"). | Failed(on bool) *ImageW |
Fallback | Fallback sets the image shown when loading failed (Ant `fallback`). | Fallback(img image.Image) *ImageW |
Fit | Fit sets object-fit: "fill" (default), "contain", "cover", "scale-down". | Fit(f string) *ImageW |
Height | Height(px float32) *ImageW | |
ID | ID returns the widget ID. | ID() ID |
Infinite | Infinite sets whether the group preview wraps from the last image to the first (Element infinite, default true; Ant stops at the ends). | Infinite(on bool) *ImageW |
Layout | Layout applies layout options. | Layout(o ...layout.Option) *ImageW |
Loading | Loading shows the placeholder instead of the image. | Loading(on bool) *ImageW |
MaskClosable | MaskClosable sets whether a click on the preview backdrop closes it (Ant mask.closable, default true; Element hide-on-click-modal, default false; the theme picks the default). | MaskClosable(on bool) *ImageW |
MaxScale | MaxScale(k float32) *ImageW | |
MinScale | MinScale and MaxScale bound the preview zoom (Ant minScale 1 / maxScale 50; Element min-scale 0.2 / max-scale 7; the theme picks the default). | MinScale(k float32) *ImageW |
Movable | Movable(false) stops dragging the preview image around (Ant preview.movable, default true). | Movable(on bool) *ImageW |
OpenPreview | OpenPreview opens the preview now (Ant preview.open); call it once per action, like Rotate. | OpenPreview() *ImageW |
Preview | Preview enables click-to-preview. | Preview(on bool) *ImageW |
PreviewAngle | PreviewAngle returns the preview rotation in degrees (clockwise). | PreviewAngle() float64 |
PreviewGroup | PreviewGroup previews imgs (this image is imgs[index]) with prev/next navigation and a counter. | PreviewGroup(imgs []image.Image, index int) *ImageW |
PreviewOpen | PreviewOpen reports whether the preview overlay is open. | PreviewOpen() bool |
PreviewScale | PreviewScale returns the preview zoom factor (1 = fitted size). | PreviewScale() float32 |
PreviewSrc | PreviewSrc shows img in the preview instead of the image itself (Ant preview.src: a larger original behind a thumbnail). | PreviewSrc(img image.Image) *ImageW |
Radius | Radius rounds the corners. | Radius(px float32) *ImageW |
Rotate | Rotate turns the open preview by deg degrees (clockwise, any angle; relative to its current rotation). | Rotate(deg float64) *ImageW |
ScaleStep | ScaleStep sets the zoom step of the preview buttons: each step scales by 1+step (Ant scaleStep 0.5; Element zoom-rate 1.2 = step 0.2). | ScaleStep(step float32) *ImageW |
ShowProgress | ShowProgress shows the "2 / 5" counter of a group preview (Ant always, Element show-progress, default false; the theme picks the default). | ShowProgress(on bool) *ImageW |
Size | Size fixes both sides. | Size(wpx, hpx float32) *ImageW |
Width | Width / Height fix the box size in px (the other side follows the image aspect ratio when only one is set). | Width(px float32) *ImageW |