阅读(823) (0)

Vant Button 按钮

2020-02-05 09:43:33 更新

引入

import Vue from 'vue';
import { Button } from 'vant';

Vue.use(Button);

代码演示

按钮类型

支持default、primary、info、warning、danger五种类型,默认为default

<van-button type="default">默认按钮</van-button>
<van-button type="primary">主要按钮</van-button>
<van-button type="info">信息按钮</van-button>
<van-button type="warning">警告按钮</van-button>
<van-button type="danger">危险按钮</van-button>

朴素按钮

通过plain属性将按钮设置为朴素按钮,朴素按钮的文字为按钮颜色,背景为白色。

<van-button plain type="primary">朴素按钮</van-button>
<van-button plain type="info">朴素按钮</van-button>

细边框

设置hairline属性可以开启 0.5px 边框,基于伪类实现

<van-button plain hairline type="primary">细边框按钮</van-button>
<van-button plain hairline type="info">细边框按钮</van-button>

禁用状态

通过disabled属性来禁用按钮,禁用状态下按钮不可点击

<van-button disabled type="primary">禁用状态</van-button>
<van-button disabled type="info">禁用状态</van-button>

加载状态

通过loading属性设置按钮为加载状态,加载状态下默认会隐藏按钮文字,可以通过loading-text设置加载状态下的文字

<van-button loading type="primary" />
<van-button loading type="primary" loading-type="spinner" />
<van-button loading type="info" loading-text="加载中..." />

按钮形状

通过square设置方形按钮,通过round设置圆形按钮

<van-button square type="primary">方形按钮</van-button>
<van-button round type="info">圆形按钮</van-button>

图标按钮

通过icon属性设置按钮图标,支持 Icon 组件里的所有图标,也可以传入图标 URL

<van-button icon="star-o" type="primary" />
<van-button icon="star-o" type="primary">按钮</van-button>
<van-button icon="https://img.yuanmabao.com/zijie/pic/vantlesson/logo.png" type="info">按钮</van-button>

按钮尺寸

支持large、normal、small、mini四种尺寸,默认为normal

<van-button type="primary" size="large">大号按钮</van-button>
<van-button type="primary" size="normal">普通按钮</van-button>
<van-button type="primary" size="small">小型按钮</van-button>
<van-button type="primary" size="mini">迷你按钮</van-button>

块级元素

按钮在默认情况下为行内块级元素,通过block属性可以将按钮的元素类型设置为块级元素

<van-button type="primary" block>块级元素</van-button>

页面导航

可以通过url属性进行 URL 跳转,或通过to属性进行路由跳转

<van-button type="primary" url="/vant/mobile.html">URL 跳转</van-button>
<van-button type="primary" to="index">路由跳转</van-button>

自定义颜色

通过color属性可以自定义按钮的颜色

<van-button color="#7232dd">单色按钮</van-button>
<van-button color="#7232dd" plain>单色按钮</van-button>
<van-button color="linear-gradient(to right, #4bb0ff, #6149f6)">渐变色按钮</van-button>

API

Props

参数说明类型默认值
type类型,可选值为 primary info warning dangerstringdefault
size尺寸,可选值为 large small ministringnormal
text按钮文字string-
color v2.1.8按钮颜色,支持传入linear-gradient渐变色string-
icon左侧 图标名称 或图片链接string-
tagHTML 标签stringbutton
native-type原生 button 标签 type 属性string-
block是否为块级元素booleanfalse
plain是否为朴素按钮booleanfalse
square是否为方形按钮booleanfalse
round是否为圆形按钮booleanfalse
disabled是否禁用按钮booleanfalse
hairline是否使用 0.5px 边框booleanfalse
loading是否显示为加载状态booleanfalse
loading-text加载状态提示文字string-
loading-type加载图标类型,可选值为spinnerstringcircular
loading-size加载图标大小string20px
url点击后跳转的链接地址string-
to点击后跳转的目标路由对象,同 vue-router 的 to 属性string | object-
replace是否在跳转时替换当前页面历史booleanfalse

Events

事件名说明回调参数
click点击按钮,且按钮状态不为加载或禁用时触发event: Event
touchstart开始触摸按钮时触发event: TouchEvent

以下是一个简单示例:

<!DOCTYPE html>
<html lang="zh">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <meta http-equiv="X-UA-Compatible" content="ie=edge" />
  <title>W3Cschool(zijiebao.com)</title>
  <!-- 引入样式 -->
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/vant@2.10/lib/index.css" rel="external nofollow" target="_blank"  />
  <!-- 引入组件 -->
  <script src="https://cdn.jsdelivr.net/npm/vue@2.6/dist/vue.min.js" rel="external nofollow" ></script>
  <script src="https://cdn.jsdelivr.net/npm/vant@2.10/lib/vant.min.js" rel="external nofollow" ></script>
</head>
<body>
  <div id="app">
    <div>
      <p>按钮类型</p>
      <van-button type="primary">主要按钮</van-button>
      <van-button type="info">信息按钮</van-button>
      <van-button type="default">默认按钮</van-button>
      <van-button type="danger">危险按钮</van-button>
      <van-button type="warning">警告按钮</van-button>
    </div>
    <div>
      <p>朴素按钮</p>
      <van-button plain type="primary">朴素按钮</van-button>
      <van-button plain type="info">朴素按钮</van-button>
    </div>
    <div>
      <p>细边框</p>
      <van-button plain hairline type="primary">细边框按钮</van-button>
      <van-button plain hairline type="info">细边框按钮</van-button>
    </div>
    <div>
      <p>禁用状态</p>
      <van-button disabled type="primary">禁用状态</van-button>
      <van-button disabled type="info">禁用状态</van-button>
    </div>
    <div>
      <p>加载状态</p>
      <van-button loading type="primary" />
      <van-button loading type="primary" loading-type="spinner" />
      <van-button loading type="info" loading-text="加载中..." />
    </div>
    <div>
      <p>按钮形状</p>
      <van-button square type="primary">方形按钮</van-button>
      <van-button round type="info">圆形按钮</van-button>
    </div>
  </div>
</body>
<script>
  var app = new Vue({
    el: '#app',
    data: {
    },
    methods: {
    },
    created() {
    },
  })
</script>
</html>


实例演示