u-button.vue 17 KB

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