u-float-button.vue 4.6 KB

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