u-button.vue 15 KB

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