u-button.vue 15 KB

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