u-button.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  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. @chooseavatar="chooseavatar"
  34. :style="[customStyle, {
  35. overflow: ripple ? 'hidden' : 'visible'
  36. }]"
  37. @tap.stop="click($event)"
  38. :hover-class="getHoverClass"
  39. :loading="loading"
  40. >
  41. <slot></slot>
  42. <view
  43. v-if="ripple"
  44. class="u-wave-ripple"
  45. :class="[waveActive ? 'u-wave-active' : '']"
  46. :style="{
  47. top: rippleTop + 'px',
  48. left: rippleLeft + 'px',
  49. width: fields.targetWidth + 'px',
  50. height: fields.targetWidth + 'px',
  51. 'background-color': rippleBgColor || 'rgba(0, 0, 0, 0.15)'
  52. }"
  53. ></view>
  54. </button>
  55. </template>
  56. <script>
  57. /**
  58. * button 按钮
  59. * @description Button 按钮
  60. * @tutorial https://www.uviewui.com/components/button.html
  61. * @property {String} size 按钮的大小
  62. * @property {Boolean} ripple 是否开启点击水波纹效果
  63. * @property {String} ripple-bg-color 水波纹的背景色,ripple为true时有效
  64. * @property {String} type 按钮的样式类型
  65. * @property {Boolean} plain 按钮是否镂空,背景色透明
  66. * @property {Boolean} disabled 是否禁用
  67. * @property {Boolean} hair-line 是否显示按钮的细边框(默认true)
  68. * @property {Boolean} shape 按钮外观形状,见文档说明
  69. * @property {Boolean} loading 按钮名称前是否带 loading 图标(App-nvue 平台,在 ios 上为雪花,Android上为圆圈)
  70. * @property {String} form-type 用于 <form> 组件,点击分别会触发 <form> 组件的 submit/reset 事件
  71. * @property {String} open-type 开放能力
  72. * @property {String} data-name 额外传参参数,用于小程序的data-xxx属性,通过target.dataset.name获取
  73. * @property {String} hover-class 指定按钮按下去的样式类。当 hover-class="none" 时,没有点击态效果(App-nvue 平台暂不支持)
  74. * @property {Number} hover-start-time 按住后多久出现点击态,单位毫秒
  75. * @property {Number} hover-stay-time 手指松开后点击态保留时间,单位毫秒
  76. * @property {Object} custom-style 对按钮的自定义样式,对象形式,见文档说明
  77. * @event {Function} click 按钮点击
  78. * @event {Function} getphonenumber open-type="getPhoneNumber"时有效
  79. * @event {Function} getuserinfo 用户点击该按钮时,会返回获取到的用户信息,从返回参数的detail中获取到的值同uni.getUserInfo
  80. * @event {Function} error 当使用开放能力时,发生错误的回调
  81. * @event {Function} opensetting 在打开授权设置页并关闭后回调
  82. * @event {Function} launchapp 打开 APP 成功的回调
  83. * @event {Function} chooseavatar 获取用户头像,可以从@chooseavatar回调中获取到头像信息,open-type="chooseAvatar"时有效
  84. * @example <u-button>月落</u-button>
  85. */
  86. export default {
  87. name: 'u-button',
  88. emits: ["click", "getphonenumber", "getuserinfo", "error", "opensetting", "launchapp", "chooseavatar"],
  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: 500
  218. },
  219. // 按住后多久出现点击态,单位毫秒
  220. hoverStartTime: {
  221. type: [String, Number],
  222. default: 20
  223. },
  224. // 手指松开后点击态保留时间,单位毫秒
  225. hoverStayTime: {
  226. type: [String, Number],
  227. default: 150
  228. },
  229. timerId: {
  230. type: [String, Number]
  231. },
  232. },
  233. computed: {
  234. // 当没有传bgColor变量时,按钮按下去的颜色类名
  235. getHoverClass() {
  236. // 如果开启水波纹效果,则不启用hover-class效果
  237. if (this.loading || this.disabled || this.ripple || this.hoverClass) return '';
  238. let hoverClass = '';
  239. hoverClass = this.plain ? 'u-' + this.type + '-plain-hover' : 'u-' + this.type + '-hover';
  240. return hoverClass;
  241. },
  242. // 在'primary', 'success', 'error', 'warning'类型下,不显示边框,否则会造成四角有毛刺现象
  243. showHairLineBorder() {
  244. if (['primary', 'success', 'error', 'warning'].indexOf(this.type) >= 0 && !this.plain) {
  245. return '';
  246. } else {
  247. return 'u-hairline-border';
  248. }
  249. }
  250. },
  251. data() {
  252. let btnTimerId = this.timerId || "button_" + Math.floor(Math.random() * 100000000 + 0);
  253. return {
  254. btnTimerId,
  255. rippleTop: 0, // 水波纹的起点Y坐标到按钮上边界的距离
  256. rippleLeft: 0, // 水波纹起点X坐标到按钮左边界的距离
  257. fields: {}, // 波纹按钮节点信息
  258. waveActive: false // 激活水波纹
  259. };
  260. },
  261. methods: {
  262. // 按钮点击
  263. click(e) {
  264. // 进行节流控制,每this.throttle毫秒内,只在开始处执行
  265. this.$u.throttle(() => {
  266. // 如果按钮时disabled和loading状态,不触发水波纹效果
  267. if (this.loading === true || this.disabled === true) return;
  268. // 是否开启水波纹效果
  269. if (this.ripple) {
  270. // 每次点击时,移除上一次的类,再次添加,才能触发动画效果
  271. this.waveActive = false;
  272. this.$nextTick(function() {
  273. this.getWaveQuery(e);
  274. });
  275. }
  276. this.$emit('click', e);
  277. }, this.throttleTime, true, this.btnTimerId);
  278. },
  279. // 查询按钮的节点信息
  280. getWaveQuery(e) {
  281. this.getElQuery().then(res => {
  282. // 查询返回的是一个数组节点
  283. let data = res[0];
  284. // 查询不到节点信息,不操作
  285. if (!data.width || !data.width) return;
  286. // 水波纹的最终形态是一个正方形(通过border-radius让其变为一个圆形),这里要保证正方形的边长等于按钮的最长边
  287. // 最终的方形(变换后的圆形)才能覆盖整个按钮
  288. data.targetWidth = data.height > data.width ? data.height : data.width;
  289. if (!data.targetWidth) return;
  290. this.fields = data;
  291. let touchesX = '',
  292. touchesY = '';
  293. // #ifdef MP-BAIDU
  294. touchesX = e.changedTouches[0].clientX;
  295. touchesY = e.changedTouches[0].clientY;
  296. // #endif
  297. // #ifdef MP-ALIPAY
  298. touchesX = e.detail.clientX;
  299. touchesY = e.detail.clientY;
  300. // #endif
  301. // #ifndef MP-BAIDU || MP-ALIPAY
  302. touchesX = e.touches[0].clientX;
  303. touchesY = e.touches[0].clientY;
  304. // #endif
  305. // 获取触摸点相对于按钮上边和左边的x和y坐标,原理是通过屏幕的触摸点(touchesY),减去按钮的上边界data.top
  306. // 但是由于`transform-origin`默认是center,所以这里再减去半径才是水波纹view应该的位置
  307. // 总的来说,就是把水波纹的矩形(变换后的圆形)的中心点,移动到我们的触摸点位置
  308. this.rippleTop = touchesY - data.top - data.targetWidth / 2;
  309. this.rippleLeft = touchesX - data.left - data.targetWidth / 2;
  310. this.$nextTick(() => {
  311. this.waveActive = true;
  312. });
  313. });
  314. },
  315. // 获取节点信息
  316. getElQuery() {
  317. return new Promise(resolve => {
  318. let queryInfo = '';
  319. // 获取元素节点信息,请查看uniapp相关文档
  320. // https://uniapp.dcloud.io/api/ui/nodes-info?id=nodesrefboundingclientrect
  321. queryInfo = uni.createSelectorQuery().in(this);
  322. //#ifdef MP-ALIPAY
  323. queryInfo = uni.createSelectorQuery();
  324. //#endif
  325. queryInfo.select('.u-btn').boundingClientRect();
  326. queryInfo.exec(data => {
  327. resolve(data);
  328. });
  329. });
  330. },
  331. // 下面为对接uniapp官方按钮开放能力事件回调的对接
  332. getphonenumber(res) {
  333. this.$emit('getphonenumber', res);
  334. },
  335. getuserinfo(res) {
  336. this.$emit('getuserinfo', res);
  337. },
  338. error(res) {
  339. this.$emit('error', res);
  340. },
  341. opensetting(res) {
  342. this.$emit('opensetting', res);
  343. },
  344. launchapp(res) {
  345. this.$emit('launchapp', res);
  346. },
  347. chooseavatar(res) {
  348. this.$emit('chooseavatar', res);
  349. }
  350. }
  351. };
  352. </script>
  353. <style scoped lang="scss">
  354. @import '../../libs/css/style.components.scss';
  355. .u-btn::after {
  356. border: none;
  357. }
  358. .u-btn {
  359. position: relative;
  360. border: 0;
  361. //border-radius: 10rpx;
  362. /* #ifndef APP-NVUE */
  363. display: inline-flex;
  364. /* #endif */
  365. // 避免边框某些场景可能被“裁剪”,不能设置为hidden
  366. overflow: visible;
  367. line-height: 1;
  368. @include vue-flex;
  369. align-items: center;
  370. justify-content: center;
  371. cursor: pointer;
  372. padding: 0 40rpx;
  373. z-index: 1;
  374. box-sizing: border-box;
  375. transition: all 0.15s;
  376. &--bold-border {
  377. border: 1px solid #ffffff;
  378. }
  379. &--default {
  380. color: $u-content-color;
  381. border-color: #c0c4cc;
  382. background-color: #ffffff;
  383. }
  384. &--primary {
  385. color: #ffffff;
  386. border-color: $u-type-primary;
  387. background-color: $u-type-primary;
  388. }
  389. &--success {
  390. color: #ffffff;
  391. border-color: $u-type-success;
  392. background-color: $u-type-success;
  393. }
  394. &--error {
  395. color: #ffffff;
  396. border-color: $u-type-error;
  397. background-color: $u-type-error;
  398. }
  399. &--warning {
  400. color: #ffffff;
  401. border-color: $u-type-warning;
  402. background-color: $u-type-warning;
  403. }
  404. &--default--disabled {
  405. color: #ffffff;
  406. border-color: #e4e7ed;
  407. background-color: #ffffff;
  408. }
  409. &--primary--disabled {
  410. color: #ffffff!important;
  411. border-color: $u-type-primary-disabled!important;
  412. background-color: $u-type-primary-disabled!important;
  413. }
  414. &--success--disabled {
  415. color: #ffffff!important;
  416. border-color: $u-type-success-disabled!important;
  417. background-color: $u-type-success-disabled!important;
  418. }
  419. &--error--disabled {
  420. color: #ffffff!important;
  421. border-color: $u-type-error-disabled!important;
  422. background-color: $u-type-error-disabled!important;
  423. }
  424. &--warning--disabled {
  425. color: #ffffff!important;
  426. border-color: $u-type-warning-disabled!important;
  427. background-color: $u-type-warning-disabled!important;
  428. }
  429. &--primary--plain {
  430. color: $u-type-primary!important;
  431. border-color: $u-type-primary-disabled!important;
  432. background-color: $u-type-primary-light!important;
  433. }
  434. &--success--plain {
  435. color: $u-type-success!important;
  436. border-color: $u-type-success-disabled!important;
  437. background-color: $u-type-success-light!important;
  438. }
  439. &--error--plain {
  440. color: $u-type-error!important;
  441. border-color: $u-type-error-disabled!important;
  442. background-color: $u-type-error-light!important;
  443. }
  444. &--warning--plain {
  445. color: $u-type-warning!important;
  446. border-color: $u-type-warning-disabled!important;
  447. background-color: $u-type-warning-light!important;
  448. }
  449. }
  450. .u-hairline-border:after {
  451. content: ' ';
  452. position: absolute;
  453. pointer-events: none;
  454. // 设置为border-box,意味着下面的scale缩小为0.5,实际上缩小的是伪元素的内容(border-box意味着内容不含border)
  455. box-sizing: border-box;
  456. // 中心点作为变形(scale())的原点
  457. -webkit-transform-origin: 0 0;
  458. transform-origin: 0 0;
  459. left: 0;
  460. top: 0;
  461. width: 199.8%;
  462. height: 199.7%;
  463. -webkit-transform: scale(0.5, 0.5);
  464. transform: scale(0.5, 0.5);
  465. border: 1px solid currentColor;
  466. z-index: 1;
  467. }
  468. .u-wave-ripple {
  469. z-index: 0;
  470. position: absolute;
  471. border-radius: 100%;
  472. background-clip: padding-box;
  473. pointer-events: none;
  474. user-select: none;
  475. transform: scale(0);
  476. opacity: 1;
  477. transform-origin: center;
  478. }
  479. .u-wave-ripple.u-wave-active {
  480. opacity: 0;
  481. transform: scale(2);
  482. transition: opacity 1s linear, transform 0.4s linear;
  483. }
  484. .u-round-circle {
  485. border-radius: 100rpx;
  486. }
  487. .u-round-circle::after {
  488. border-radius: 100rpx;
  489. }
  490. .u-loading::after {
  491. background-color: hsla(0, 0%, 100%, 0.35);
  492. }
  493. .u-size-default {
  494. font-size: 30rpx;
  495. height: 80rpx;
  496. line-height: 80rpx;
  497. }
  498. .u-size-medium {
  499. /* #ifndef APP-NVUE */
  500. display: inline-flex;
  501. /* #endif */
  502. width: auto;
  503. font-size: 26rpx;
  504. height: 70rpx;
  505. line-height: 70rpx;
  506. padding: 0 80rpx;
  507. }
  508. .u-size-mini {
  509. /* #ifndef APP-NVUE */
  510. display: inline-flex;
  511. /* #endif */
  512. width: auto;
  513. font-size: 22rpx;
  514. padding-top: 1px;
  515. height: 50rpx;
  516. line-height: 50rpx;
  517. padding: 0 20rpx;
  518. }
  519. .u-primary-plain-hover {
  520. color: #ffffff !important;
  521. background: $u-type-primary-dark !important;
  522. }
  523. .u-default-plain-hover {
  524. color: $u-type-primary-dark !important;
  525. background: $u-type-primary-light !important;
  526. }
  527. .u-success-plain-hover {
  528. color: #ffffff !important;
  529. background: $u-type-success-dark !important;
  530. }
  531. .u-warning-plain-hover {
  532. color: #ffffff !important;
  533. background: $u-type-warning-dark !important;
  534. }
  535. .u-error-plain-hover {
  536. color: #ffffff !important;
  537. background: $u-type-error-dark !important;
  538. }
  539. .u-info-plain-hover {
  540. color: #ffffff !important;
  541. background: $u-type-info-dark !important;
  542. }
  543. .u-default-hover {
  544. color: $u-type-primary-dark !important;
  545. border-color: $u-type-primary-dark !important;
  546. background-color: $u-type-primary-light !important;
  547. }
  548. .u-primary-hover {
  549. background: $u-type-primary-dark !important;
  550. color: #fff;
  551. }
  552. .u-success-hover {
  553. background: $u-type-success-dark !important;
  554. color: #fff;
  555. }
  556. .u-info-hover {
  557. background: $u-type-info-dark !important;
  558. color: #fff;
  559. }
  560. .u-warning-hover {
  561. background: $u-type-warning-dark !important;
  562. color: #fff;
  563. }
  564. .u-error-hover {
  565. background: $u-type-error-dark !important;
  566. color: #fff;
  567. }
  568. </style>