You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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