u-float-button.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <template>
  2. <view
  3. class="u-float-button" :style="{
  4. position: 'fixed',
  5. top: top,
  6. bottom: bottom,
  7. right: right,
  8. }">
  9. <view class="u-float-button__main" @click="clickHandler" :style="{
  10. backgroundColor: backgroundColor,
  11. color: color,
  12. display: 'flex',
  13. flexDirection: 'row',
  14. justifyContent: 'center',
  15. alignItems: 'center',
  16. width: width,
  17. height: height,
  18. borderRadius: '50%',
  19. borderColor: borderColor,
  20. }">
  21. <slot :showList="showList">
  22. <up-icon class="cursor-pointer" :class="{'show-list': showList}" name="plus" :color="color"></up-icon>
  23. </slot>
  24. <view v-if="showList" class="u-float-button__list" :style="{
  25. bottom: height
  26. }">
  27. <slot name="list">
  28. <template v-for="item in list">
  29. <view class="u-float-button__item" :style="{
  30. backgroundColor: item?.backgroundColor ? item?.backgroundColor : backgroundColor,
  31. color: item?.color ? item?.color : color,
  32. display: 'flex',
  33. flexDirection: 'row',
  34. justifyContent: 'center',
  35. alignItems: 'center',
  36. width: width,
  37. height: height,
  38. borderRadius: '50%',
  39. borderColor: item?.borderColor ? item?.borderColor : borderColor,
  40. }" @click="itemClick(item, index)">
  41. <up-icon :name="item.name" :color="item?.color ? item?.color : color"></up-icon>
  42. </view>
  43. </template>
  44. </slot>
  45. </view>
  46. </view>
  47. </view>
  48. </template>
  49. <script>
  50. import { mpMixin } from '../../libs/mixin/mpMixin';
  51. import { mixin } from '../../libs/mixin/mixin';
  52. import { addStyle, addUnit, deepMerge } from '../../libs/function/index';
  53. /**
  54. * FloatButton 悬浮按钮
  55. * @description 悬浮按钮常用于屏幕右下角点击展开的操作菜单
  56. * @tutorial https://ijry.github.io/uview-plus/components/floatButton.html
  57. * @property {String} backgroundColor 背景颜色
  58. * @event {Function} click 点击触发事件
  59. * @example <up-float-button></up-float-button>
  60. */
  61. export default {
  62. name: 'u-float-button',
  63. // #ifdef MP
  64. mixins: [mpMixin, mixin],
  65. // #endif
  66. // #ifndef MP
  67. mixins: [mpMixin, mixin],
  68. // #endif
  69. emits: ['click', 'item-click'],
  70. computed: {
  71. },
  72. props: {
  73. // 背景颜色
  74. backgroundColor: {
  75. type: String,
  76. default: '#2979ff'
  77. },
  78. // 文字颜色
  79. color: {
  80. type: String,
  81. default: '#fff'
  82. },
  83. // 宽度
  84. width: {
  85. type: String,
  86. default: '50px'
  87. },
  88. // 高度
  89. height: {
  90. type: String,
  91. default: '50px'
  92. },
  93. // 边框颜色,默认为空字符串表示无边框
  94. borderColor: {
  95. type: String,
  96. default: ''
  97. },
  98. // 右侧偏移量
  99. right: {
  100. type: [String, Number],
  101. default: '30px'
  102. },
  103. // 顶部偏移量,未提供默认值,可能需要根据具体情况设置
  104. top: {
  105. type: [String, Number],
  106. default: '',
  107. },
  108. // 底部偏移量
  109. bottom: {
  110. type: String,
  111. default: ''
  112. },
  113. // 是否为菜单项
  114. isMenu: {
  115. type: Boolean,
  116. default: false
  117. },
  118. list: {
  119. type: Array,
  120. default: () => {
  121. return []
  122. }
  123. }
  124. },
  125. data() {
  126. return {
  127. showList: false
  128. }
  129. },
  130. methods: {
  131. addStyle,
  132. clickHandler(e) {
  133. if (this.isMenu) {
  134. this.showList = !this.showList
  135. this.$emit('click', e)
  136. } else {
  137. this.$emit('click', e)
  138. }
  139. },
  140. itemClick(item, index) {
  141. this.$emit('item-click', {
  142. ...item,
  143. index
  144. })
  145. }
  146. }
  147. }
  148. </script>
  149. <style lang="scss" scoped>
  150. @import '../../libs/css/components.scss';
  151. .u-float-button {
  152. z-index: 999;
  153. .show-list {
  154. transform: rotate(45deg);
  155. }
  156. &__list {
  157. position: absolute;
  158. bottom: 0px;
  159. display: flex;
  160. flex-direction: column;
  161. >view {
  162. margin: 5px 0px;
  163. }
  164. }
  165. }
  166. </style>