Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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