u-button.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. <template>
  2. <button
  3. id="u-wave-btn"
  4. class="u-btn u-line-1 u-fix-ios-appearance"
  5. :class="[
  6. 'u-size-' + size,
  7. plain ? 'u-btn--' + type + '--plain' : '',
  8. loading ? 'u-loading' : '',
  9. shape == 'circle' ? 'u-round-circle' : '',
  10. hairLine ? showHairLineBorder : 'u-btn--bold-border',
  11. 'u-btn--' + type,
  12. disabled ? `u-btn--${type}--disabled` : '',
  13. ]"
  14. :hover-start-time="Number(hoverStartTime)"
  15. :hover-stay-time="Number(hoverStayTime)"
  16. :disabled="disabled"
  17. :form-type="formType"
  18. :open-type="openType"
  19. :app-parameter="appParameter"
  20. :hover-stop-propagation="hoverStopPropagation"
  21. :send-message-title="sendMessageTitle"
  22. send-message-path="sendMessagePath"
  23. :lang="lang"
  24. :data-name="dataName"
  25. :session-from="sessionFrom"
  26. :send-message-img="sendMessageImg"
  27. :show-message-card="showMessageCard"
  28. @getphonenumber="getphonenumber"
  29. @getuserinfo="getuserinfo"
  30. @error="error"
  31. @opensetting="opensetting"
  32. @launchapp="launchapp"
  33. :style="[customStyle, {
  34. overflow: ripple ? 'hidden' : 'visible'
  35. }]"
  36. @tap.stop="click($event)"
  37. :hover-class="getHoverClass"
  38. :loading="loading"
  39. >
  40. <slot></slot>
  41. <view
  42. v-if="ripple"
  43. class="u-wave-ripple"
  44. :class="[waveActive ? 'u-wave-active' : '']"
  45. :style="{
  46. top: rippleTop + 'px',
  47. left: rippleLeft + 'px',
  48. width: fields.targetWidth + 'px',
  49. height: fields.targetWidth + 'px',
  50. 'background-color': rippleBgColor || 'rgba(0, 0, 0, 0.15)'
  51. }"
  52. ></view>
  53. </button>
  54. </template>
  55. <script>
  56. /**
  57. * button 按钮
  58. * @description Button 按钮
  59. * @tutorial https://www.uviewui.com/components/button.html
  60. * @property {String} size 按钮的大小
  61. * @property {Boolean} ripple 是否开启点击水波纹效果
  62. * @property {String} ripple-bg-color 水波纹的背景色,ripple为true时有效
  63. * @property {String} type 按钮的样式类型
  64. * @property {Boolean} plain 按钮是否镂空,背景色透明
  65. * @property {Boolean} disabled 是否禁用
  66. * @property {Boolean} hair-line 是否显示按钮的细边框(默认true)
  67. * @property {Boolean} shape 按钮外观形状,见文档说明
  68. * @property {Boolean} loading 按钮名称前是否带 loading 图标(App-nvue 平台,在 ios 上为雪花,Android上为圆圈)
  69. * @property {String} form-type 用于 <form> 组件,点击分别会触发 <form> 组件的 submit/reset 事件
  70. * @property {String} open-type 开放能力
  71. * @property {String} data-name 额外传参参数,用于小程序的data-xxx属性,通过target.dataset.name获取
  72. * @property {String} hover-class 指定按钮按下去的样式类。当 hover-class="none" 时,没有点击态效果(App-nvue 平台暂不支持)
  73. * @property {Number} hover-start-time 按住后多久出现点击态,单位毫秒
  74. * @property {Number} hover-stay-time 手指松开后点击态保留时间,单位毫秒
  75. * @property {Object} custom-style 对按钮的自定义样式,对象形式,见文档说明
  76. * @event {Function} click 按钮点击
  77. * @event {Function} getphonenumber open-type="getPhoneNumber"时有效
  78. * @event {Function} getuserinfo 用户点击该按钮时,会返回获取到的用户信息,从返回参数的detail中获取到的值同uni.getUserInfo
  79. * @event {Function} error 当使用开放能力时,发生错误的回调
  80. * @event {Function} opensetting 在打开授权设置页并关闭后回调
  81. * @event {Function} launchapp 打开 APP 成功的回调
  82. * @example <u-button>月落</u-button>
  83. */
  84. export default {
  85. name: 'u-button',
  86. props: {
  87. // 是否细边框
  88. hairLine: {
  89. type: Boolean,
  90. default: true
  91. },
  92. // 按钮的预置样式,default,primary,error,warning,success
  93. type: {
  94. type: String,
  95. default: 'default'
  96. },
  97. // 按钮尺寸,default,medium,mini
  98. size: {
  99. type: String,
  100. default: 'default'
  101. },
  102. // 按钮形状,circle(两边为半圆),square(带圆角)
  103. shape: {
  104. type: String,
  105. default: 'square'
  106. },
  107. // 按钮是否镂空
  108. plain: {
  109. type: Boolean,
  110. default: false
  111. },
  112. // 是否禁止状态
  113. disabled: {
  114. type: Boolean,
  115. default: false
  116. },
  117. // 是否加载中
  118. loading: {
  119. type: Boolean,
  120. default: false
  121. },
  122. // 开放能力,具体请看uniapp稳定关于button组件部分说明
  123. // https://uniapp.dcloud.io/component/button
  124. openType: {
  125. type: String,
  126. default: ''
  127. },
  128. // 用于 <form> 组件,点击分别会触发 <form> 组件的 submit/reset 事件
  129. // 取值为submit(提交表单),reset(重置表单)
  130. formType: {
  131. type: String,
  132. default: ''
  133. },
  134. // 打开 APP 时,向 APP 传递的参数,open-type=launchApp时有效
  135. // 只微信小程序、QQ小程序有效
  136. appParameter: {
  137. type: String,
  138. default: ''
  139. },
  140. // 指定是否阻止本节点的祖先节点出现点击态,微信小程序有效
  141. hoverStopPropagation: {
  142. type: Boolean,
  143. default: false
  144. },
  145. // 指定返回用户信息的语言,zh_CN 简体中文,zh_TW 繁体中文,en 英文。只微信小程序有效
  146. lang: {
  147. type: String,
  148. default: 'en'
  149. },
  150. // 会话来源,open-type="contact"时有效。只微信小程序有效
  151. sessionFrom: {
  152. type: String,
  153. default: ''
  154. },
  155. // 会话内消息卡片标题,open-type="contact"时有效
  156. // 默认当前标题,只微信小程序有效
  157. sendMessageTitle: {
  158. type: String,
  159. default: ''
  160. },
  161. // 会话内消息卡片点击跳转小程序路径,open-type="contact"时有效
  162. // 默认当前分享路径,只微信小程序有效
  163. sendMessagePath: {
  164. type: String,
  165. default: ''
  166. },
  167. // 会话内消息卡片图片,open-type="contact"时有效
  168. // 默认当前页面截图,只微信小程序有效
  169. sendMessageImg: {
  170. type: String,
  171. default: ''
  172. },
  173. // 是否显示会话内消息卡片,设置此参数为 true,用户进入客服会话会在右下角显示"可能要发送的小程序"提示,
  174. // 用户点击后可以快速发送小程序消息,open-type="contact"时有效
  175. showMessageCard: {
  176. type: Boolean,
  177. default: false
  178. },
  179. // 手指按(触摸)按钮时按钮时的背景颜色
  180. hoverBgColor: {
  181. type: String,
  182. default: ''
  183. },
  184. // 水波纹的背景颜色
  185. rippleBgColor: {
  186. type: String,
  187. default: ''
  188. },
  189. // 是否开启水波纹效果
  190. ripple: {
  191. type: Boolean,
  192. default: false
  193. },
  194. // 按下的类名
  195. hoverClass: {
  196. type: String,
  197. default: ''
  198. },
  199. // 自定义样式,对象形式
  200. customStyle: {
  201. type: Object,
  202. default() {
  203. return {};
  204. }
  205. },
  206. // 额外传参参数,用于小程序的data-xxx属性,通过target.dataset.name获取
  207. dataName: {
  208. type: String,
  209. default: ''
  210. },
  211. // 节流,一定时间内只能触发一次
  212. throttleTime: {
  213. type: [String, Number],
  214. default: 1000
  215. },
  216. // 按住后多久出现点击态,单位毫秒
  217. hoverStartTime: {
  218. type: [String, Number],
  219. default: 20
  220. },
  221. // 手指松开后点击态保留时间,单位毫秒
  222. hoverStayTime: {
  223. type: [String, Number],
  224. default: 150
  225. },
  226. },
  227. computed: {
  228. // 当没有传bgColor变量时,按钮按下去的颜色类名
  229. getHoverClass() {
  230. // 如果开启水波纹效果,则不启用hover-class效果
  231. if (this.loading || this.disabled || this.ripple || this.hoverClass) return '';
  232. let hoverClass = '';
  233. hoverClass = this.plain ? 'u-' + this.type + '-plain-hover' : 'u-' + this.type + '-hover';
  234. return hoverClass;
  235. },
  236. // 在'primary', 'success', 'error', 'warning'类型下,不显示边框,否则会造成四角有毛刺现象
  237. showHairLineBorder() {
  238. if (['primary', 'success', 'error', 'warning'].indexOf(this.type) >= 0 && !this.plain) {
  239. return '';
  240. } else {
  241. return 'u-hairline-border';
  242. }
  243. }
  244. },
  245. data() {
  246. return {
  247. rippleTop: 0, // 水波纹的起点Y坐标到按钮上边界的距离
  248. rippleLeft: 0, // 水波纹起点X坐标到按钮左边界的距离
  249. fields: {}, // 波纹按钮节点信息
  250. waveActive: false // 激活水波纹
  251. };
  252. },
  253. methods: {
  254. // 按钮点击
  255. click(e) {
  256. // 进行节流控制,每this.throttle毫秒内,只在开始处执行
  257. this.$u.throttle(() => {
  258. // 如果按钮时disabled和loading状态,不触发水波纹效果
  259. if (this.loading === true || this.disabled === true) return;
  260. // 是否开启水波纹效果
  261. if (this.ripple) {
  262. // 每次点击时,移除上一次的类,再次添加,才能触发动画效果
  263. this.waveActive = false;
  264. this.$nextTick(function() {
  265. this.getWaveQuery(e);
  266. });
  267. }
  268. this.$emit('click', e);
  269. }, this.throttleTime);
  270. },
  271. // 查询按钮的节点信息
  272. getWaveQuery(e) {
  273. this.getElQuery().then(res => {
  274. // 查询返回的是一个数组节点
  275. let data = res[0];
  276. // 查询不到节点信息,不操作
  277. if (!data.width || !data.width) return;
  278. // 水波纹的最终形态是一个正方形(通过border-radius让其变为一个圆形),这里要保证正方形的边长等于按钮的最长边
  279. // 最终的方形(变换后的圆形)才能覆盖整个按钮
  280. data.targetWidth = data.height > data.width ? data.height : data.width;
  281. if (!data.targetWidth) return;
  282. this.fields = data;
  283. let touchesX = '',
  284. touchesY = '';
  285. // #ifdef MP-BAIDU
  286. touchesX = e.changedTouches[0].clientX;
  287. touchesY = e.changedTouches[0].clientY;
  288. // #endif
  289. // #ifdef MP-ALIPAY
  290. touchesX = e.detail.clientX;
  291. touchesY = e.detail.clientY;
  292. // #endif
  293. // #ifndef MP-BAIDU || MP-ALIPAY
  294. touchesX = e.touches[0].clientX;
  295. touchesY = e.touches[0].clientY;
  296. // #endif
  297. // 获取触摸点相对于按钮上边和左边的x和y坐标,原理是通过屏幕的触摸点(touchesY),减去按钮的上边界data.top
  298. // 但是由于`transform-origin`默认是center,所以这里再减去半径才是水波纹view应该的位置
  299. // 总的来说,就是把水波纹的矩形(变换后的圆形)的中心点,移动到我们的触摸点位置
  300. this.rippleTop = touchesY - data.top - data.targetWidth / 2;
  301. this.rippleLeft = touchesX - data.left - data.targetWidth / 2;
  302. this.$nextTick(() => {
  303. this.waveActive = true;
  304. });
  305. });
  306. },
  307. // 获取节点信息
  308. getElQuery() {
  309. return new Promise(resolve => {
  310. let queryInfo = '';
  311. // 获取元素节点信息,请查看uniapp相关文档
  312. // https://uniapp.dcloud.io/api/ui/nodes-info?id=nodesrefboundingclientrect
  313. queryInfo = uni.createSelectorQuery().in(this);
  314. //#ifdef MP-ALIPAY
  315. queryInfo = uni.createSelectorQuery();
  316. //#endif
  317. queryInfo.select('.u-btn').boundingClientRect();
  318. queryInfo.exec(data => {
  319. resolve(data);
  320. });
  321. });
  322. },
  323. // 下面为对接uniapp官方按钮开放能力事件回调的对接
  324. getphonenumber(res) {
  325. this.$emit('getphonenumber', res);
  326. },
  327. getuserinfo(res) {
  328. this.$emit('getuserinfo', res);
  329. },
  330. error(res) {
  331. this.$emit('error', res);
  332. },
  333. opensetting(res) {
  334. this.$emit('opensetting', res);
  335. },
  336. launchapp(res) {
  337. this.$emit('launchapp', res);
  338. }
  339. }
  340. };
  341. </script>
  342. <style scoped lang="scss">
  343. @import '../../libs/css/style.components.scss';
  344. .u-btn::after {
  345. border: none;
  346. }
  347. .u-btn {
  348. position: relative;
  349. border: 0;
  350. //border-radius: 10rpx;
  351. /* #ifndef APP-NVUE */
  352. display: inline-flex;
  353. /* #endif */
  354. // 避免边框某些场景可能被“裁剪”,不能设置为hidden
  355. overflow: visible;
  356. line-height: 1;
  357. @include vue-flex;
  358. align-items: center;
  359. justify-content: center;
  360. cursor: pointer;
  361. padding: 0 40rpx;
  362. z-index: 1;
  363. box-sizing: border-box;
  364. transition: all 0.15s;
  365. &--bold-border {
  366. border: 1px solid #ffffff;
  367. }
  368. &--default {
  369. color: $u-content-color;
  370. border-color: #c0c4cc;
  371. background-color: #ffffff;
  372. }
  373. &--primary {
  374. color: #ffffff;
  375. border-color: $u-type-primary;
  376. background-color: $u-type-primary;
  377. }
  378. &--success {
  379. color: #ffffff;
  380. border-color: $u-type-success;
  381. background-color: $u-type-success;
  382. }
  383. &--error {
  384. color: #ffffff;
  385. border-color: $u-type-error;
  386. background-color: $u-type-error;
  387. }
  388. &--warning {
  389. color: #ffffff;
  390. border-color: $u-type-warning;
  391. background-color: $u-type-warning;
  392. }
  393. &--default--disabled {
  394. color: #ffffff;
  395. border-color: #e4e7ed;
  396. background-color: #ffffff;
  397. }
  398. &--primary--disabled {
  399. color: #ffffff!important;
  400. border-color: $u-type-primary-disabled!important;
  401. background-color: $u-type-primary-disabled!important;
  402. }
  403. &--success--disabled {
  404. color: #ffffff!important;
  405. border-color: $u-type-success-disabled!important;
  406. background-color: $u-type-success-disabled!important;
  407. }
  408. &--error--disabled {
  409. color: #ffffff!important;
  410. border-color: $u-type-error-disabled!important;
  411. background-color: $u-type-error-disabled!important;
  412. }
  413. &--warning--disabled {
  414. color: #ffffff!important;
  415. border-color: $u-type-warning-disabled!important;
  416. background-color: $u-type-warning-disabled!important;
  417. }
  418. &--primary--plain {
  419. color: $u-type-primary!important;
  420. border-color: $u-type-primary-disabled!important;
  421. background-color: $u-type-primary-light!important;
  422. }
  423. &--success--plain {
  424. color: $u-type-success!important;
  425. border-color: $u-type-success-disabled!important;
  426. background-color: $u-type-success-light!important;
  427. }
  428. &--error--plain {
  429. color: $u-type-error!important;
  430. border-color: $u-type-error-disabled!important;
  431. background-color: $u-type-error-light!important;
  432. }
  433. &--warning--plain {
  434. color: $u-type-warning!important;
  435. border-color: $u-type-warning-disabled!important;
  436. background-color: $u-type-warning-light!important;
  437. }
  438. }
  439. .u-hairline-border:after {
  440. content: ' ';
  441. position: absolute;
  442. pointer-events: none;
  443. // 设置为border-box,意味着下面的scale缩小为0.5,实际上缩小的是伪元素的内容(border-box意味着内容不含border)
  444. box-sizing: border-box;
  445. // 中心点作为变形(scale())的原点
  446. -webkit-transform-origin: 0 0;
  447. transform-origin: 0 0;
  448. left: 0;
  449. top: 0;
  450. width: 199.8%;
  451. height: 199.7%;
  452. -webkit-transform: scale(0.5, 0.5);
  453. transform: scale(0.5, 0.5);
  454. border: 1px solid currentColor;
  455. z-index: 1;
  456. }
  457. .u-wave-ripple {
  458. z-index: 0;
  459. position: absolute;
  460. border-radius: 100%;
  461. background-clip: padding-box;
  462. pointer-events: none;
  463. user-select: none;
  464. transform: scale(0);
  465. opacity: 1;
  466. transform-origin: center;
  467. }
  468. .u-wave-ripple.u-wave-active {
  469. opacity: 0;
  470. transform: scale(2);
  471. transition: opacity 1s linear, transform 0.4s linear;
  472. }
  473. .u-round-circle {
  474. border-radius: 100rpx;
  475. }
  476. .u-round-circle::after {
  477. border-radius: 100rpx;
  478. }
  479. .u-loading::after {
  480. background-color: hsla(0, 0%, 100%, 0.35);
  481. }
  482. .u-size-default {
  483. font-size: 30rpx;
  484. height: 80rpx;
  485. line-height: 80rpx;
  486. }
  487. .u-size-medium {
  488. /* #ifndef APP-NVUE */
  489. display: inline-flex;
  490. /* #endif */
  491. width: auto;
  492. font-size: 26rpx;
  493. height: 70rpx;
  494. line-height: 70rpx;
  495. padding: 0 80rpx;
  496. }
  497. .u-size-mini {
  498. /* #ifndef APP-NVUE */
  499. display: inline-flex;
  500. /* #endif */
  501. width: auto;
  502. font-size: 22rpx;
  503. padding-top: 1px;
  504. height: 50rpx;
  505. line-height: 50rpx;
  506. padding: 0 20rpx;
  507. }
  508. .u-primary-plain-hover {
  509. color: #ffffff !important;
  510. background: $u-type-primary-dark !important;
  511. }
  512. .u-default-plain-hover {
  513. color: $u-type-primary-dark !important;
  514. background: $u-type-primary-light !important;
  515. }
  516. .u-success-plain-hover {
  517. color: #ffffff !important;
  518. background: $u-type-success-dark !important;
  519. }
  520. .u-warning-plain-hover {
  521. color: #ffffff !important;
  522. background: $u-type-warning-dark !important;
  523. }
  524. .u-error-plain-hover {
  525. color: #ffffff !important;
  526. background: $u-type-error-dark !important;
  527. }
  528. .u-info-plain-hover {
  529. color: #ffffff !important;
  530. background: $u-type-info-dark !important;
  531. }
  532. .u-default-hover {
  533. color: $u-type-primary-dark !important;
  534. border-color: $u-type-primary-dark !important;
  535. background-color: $u-type-primary-light !important;
  536. }
  537. .u-primary-hover {
  538. background: $u-type-primary-dark !important;
  539. color: #fff;
  540. }
  541. .u-success-hover {
  542. background: $u-type-success-dark !important;
  543. color: #fff;
  544. }
  545. .u-info-hover {
  546. background: $u-type-info-dark !important;
  547. color: #fff;
  548. }
  549. .u-warning-hover {
  550. background: $u-type-warning-dark !important;
  551. color: #fff;
  552. }
  553. .u-error-hover {
  554. background: $u-type-error-dark !important;
  555. color: #fff;
  556. }
  557. </style>