Skip to content

按钮 Button

  • 一个基础的按钮组件
  • 支持防抖功能(默认开启)
  • 支持按钮图标
  • 支持 loading

提示

为了组件简单设计原则,另外在实际开发中,不同项目对按钮要求的样式差异较大,按钮组件未内置主题,你可以通过设置 --pure-button-theme-color 变量来设置按钮的主题。建议在实际项目开发中,拿到设计稿后,根据设计稿的样式来将不同主题的按钮通过在 App.vue 中定义全局主题 class 的形式定好按钮主题,在使用时直接给按钮组件添加对应 class 来使用主题按钮。

提示

另外,为了方便使用,我们还设计了一些简单的辅助类,具体使用参考下方示例。

插件市场地址

https://ext.dcloud.net.cn/plugin?name=pure-button

基础使用

通过 text 属性设置按钮文本。

vue
<pure-button text="默认按钮"></pure-button>

带图标的按钮

通过 icon 属性设置按钮图标名称即可使用。

vue
<pure-button text="图标按钮" icon="__gouwudai"></pure-button>

只显示图标

如果需要只显示图标,可将 text 属性设置为空字符串或不设置 text 属性。

vue
<pure-button icon="__shebei"></pure-button>

加载状态

通过 loading 属性设置按钮的加载状态。

vue
<pure-button text="加载按钮" loading></pure-button>
<pure-button text="加载按钮" loading loadingText="加载中..."></pure-button>

防抖

  • 通过 debounce 属性可以开启或关闭防抖功能
  • 防抖间隔默认为 800ms
  • 通过 debounceTime 属性可以设置防抖间隔,单位为 ms
  • 通过 debounceTip 属性可以设置是否显示防抖提示
  • 通过 debounceTipText 属性可以设置防抖提示文本
vue
<pure-button text="防抖按钮" icon="__shandian"></pure-button>
<pure-button text="开启防抖提示" icon="__shandian" debounceTip></pure-button>
<pure-button text="自定义防抖提示" icon="__shandian" debounceTip debounceTipText="点击太快啦!!!"></pure-button>

带副文本的按钮

  • 为了方便使用,按钮组件支持带副文本的按钮
  • 通过 subtext 属性设置副文本即可
  • 副文本颜色通过 opacity 属性设置为先对于住文本色的浅色形式,默认值为 0.7
vue
<pure-button text="带副文本的按钮" subtext="我是副文本"></pure-button>

关于主题

通过 --pure-button-theme-color 可以设置简单的主题按钮。

vue
<view style="--pure-button-theme-color: #1989fa;">
    <pure-button text="主要按钮"></pure-button>
</view>

<!-- 如果将 `style` 写到组件上,需设置 `mergeVirtualHostAttributes`,合并小程序组件虚拟节点外层属性 -->
<pure-button text="成功按钮" style="--pure-button-theme-color: #07c160;"></pure-button>
<pure-button text="危险按钮" style="--pure-button-theme-color: #ee0a24;"></pure-button>
<pure-button text="警告按钮" style="--pure-button-theme-color: #ff976a;"></pure-button>
<pure-button text="信息按钮" style="--pure-button-theme-color: #999999;"></pure-button>

提示

如果你的项目设置了 mergeVirtualHostAttributes 合并由 Vue 组件编译而成的小程序组件虚拟节点外层属性功能,可以直接将 style 写到组件上,否则需要写到组件的父元素或组件元素上。

辅助类

提示

需在 App.vue 中引入 pure-styles 辅助类文件。

css
@import "@/uni_modules/pure-styles/index.css";
vue
<view class="pure-button-round">
    <pure-button text="圆角按钮" @click="handleClick"></pure-button>
</view>

<!-- 如果将 `class` 写到组件上,需设置 `mergeVirtualHostAttributes`,合并小程序组件虚拟节点外层属性 -->
<pure-button text="方形按钮" class="pure-button-square"></pure-button>
<pure-button text="朴素按钮" class="pure-button-plain"></pure-button>
<pure-button text="块级按钮" class="pure-button-block"></pure-button>

提示

如果你的项目设置了 mergeVirtualHostAttributes 合并由 Vue 组件编译而成的小程序组件虚拟节点外层属性功能,可以直接将 class 写到组件上,否则需要写到组件的父元素或组件元素上。

Props

属性名说明类型默认值可选值版本
hoverClassviewhover-classString"pure-button-hover-class"-+1.0.0
text按钮主文本String""-+1.0.0
subtext按钮副文本String""-+1.0.0
icon按钮图标名称,同图标组件(Icon)的 name 属性,参考String""-+1.0.0
iconMode图片图标的裁剪模式,同图标组件(Icon)的 mode 属性,参考Stringundefined-+1.0.0
disabled禁用状态Booleanfalsetrue+1.0.0
loading加载状态Booleanfalsetrue+1.0.0
loadingText加载提示文本String""-+1.0.0
loadingIcon加载图标名称,同图标组件(Icon)的 name 属性,参考String__loading-+1.0.0
debounce是否开启防抖功能Booleantruefalse+1.0.0
debounceTime防抖时间间隔[String, Number]800-+1.0.0
debounceTip是否显示防抖提示Booleanfalsetrue+1.0.0
debounceTipText防抖提示文本String操作过快,请放缓操作节奏-+1.0.0
<button> 组件的原生属性参考---+1.0.0

Event

事件名说明回调参数版本
click点击事件-1.0.0
<button> 组件的原生事件参考-1.0.0

样式变量

变量名默认值版本
--pure-button-theme-color#1989fa+1.0.0
--pure-button-displayinline-flex 或 flex+1.0.0
--pure-button-flex-directionrow+1.0.0
--pure-button-align-itemscenter+1.0.0
--pure-button-justify-contentcenter+1.0.0
--pure-button-backgroundvar(--pure-button-theme-color, #1989fa) 或 #ffffff+1.0.0
--pure-button-height44px 或 56px+1.0.0
--pure-button-border-radius4px 或 0px 或 1000px+1.0.0
--pure-button-bordernone+1.0.0
--pure-button-border-width1px+1.0.0
--pure-button-border-stylesolid+1.0.0
--pure-button-border-colorvar(--pure-button-theme-color, #1989fa)+1.0.0
--pure-button-padding0 16px+1.0.0
--pure-button-text-aligncenter+1.0.0
--pure-button-color#ffffff 或 var(--pure-button-theme-color, #1989fa)+1.0.0
--pure-button-hover-opacity0.8+1.0.0
--pure-button-disabled-opacity0.5+1.0.0
--pure-button-content-displayflex+1.0.0
--pure-button-content-flex-directionrow+1.0.0
--pure-button-content-align-itemscenter+1.0.0
--pure-button-content-justify-content5px+1.0.0
--pure-button-content-gap4px+1.0.0
--pure-button-text-displayflex+1.0.0
--pure-button-text-flex-directioncolumn+1.0.0
--pure-button-text-align-itemscenter+1.0.0
--pure-button-text-justify-contentcenter+1.0.0
--pure-button-text-gap0px+1.0.0
--pure-button-subtext-font-size0.7em+1.0.0
--pure-button-subtext-color-+1.0.0
--pure-button-subtext-opacity0.7+1.0.0
--pure-button-loading-displayflex+1.0.0
--pure-button-loading-flex-directionrow+1.0.0
--pure-button-loading-align-itemscenter+1.0.0
--pure-button-loading-justify-contentcenter+1.0.0
--pure-button-loading-gap4px+1.0.0
--pure-button-loading-animation-+1.0.0
--pure-button-loading-animation-duration1.2s+1.0.0
--pure-button-loading-animation-timing-functionlinear+1.0.0
--pure-button-loading-animation-iteration-countinfinite+1.0.0

基于 MIT 许可发布