uni-fab.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. <template>
  2. <view class="uni-cursor-point" v-if="dotCount>0 || showFabBtn">
  3. <view v-if="popMenu && (leftBottom || rightBottom || leftTop || rightTop) && content.length > 0" :class="{
  4. 'uni-fab--leftBottom': leftBottom,
  5. 'uni-fab--rightBottom': rightBottom,
  6. 'uni-fab--leftTop': leftTop,
  7. 'uni-fab--rightTop': rightTop
  8. }" class="uni-fab" :style="nvueBottom">
  9. <view :class="{
  10. 'uni-fab__content--left': horizontal === 'left',
  11. 'uni-fab__content--right': horizontal === 'right',
  12. 'uni-fab__content--flexDirection': direction === 'vertical',
  13. 'uni-fab__content--flexDirectionStart': flexDirectionStart,
  14. 'uni-fab__content--flexDirectionEnd': flexDirectionEnd,
  15. 'uni-fab__content--other-platform': !isAndroidNvue
  16. }" :style="{ width: boxWidth, height: boxHeight, backgroundColor: styles.backgroundColor }"
  17. class="uni-fab__content" elevation="5">
  18. <view v-if="flexDirectionStart || horizontalLeft" class="uni-fab__item uni-fab__item--first" />
  19. <slot />
  20. <!-- <view :class="{ 'uni-fab__item--active': isShow }" >
  21. <slot />
  22. </view> -->
  23. <!-- <view v-for="(item, index) in content" :key="index" :class="{ 'uni-fab__item--active': isShow }"
  24. class="uni-fab__item" @click="_onItemClick(index, item)">
  25. <image :src="item.active ? item.selectedIconPath : item.iconPath" class="uni-fab__item-image"
  26. mode="aspectFit" />
  27. <text class="uni-fab__item-text"
  28. :style="{ color: item.active ? styles.selectedColor : styles.color }">{{ item.text }}</text>
  29. </view> -->
  30. <view v-if="flexDirectionEnd || horizontalRight" class="uni-fab__item uni-fab__item--first" />
  31. </view>
  32. </view>
  33. <view :class="{
  34. 'uni-fab__circle--leftBottom': leftBottom,
  35. 'uni-fab__circle--rightBottom': rightBottom,
  36. 'uni-fab__circle--leftTop': leftTop,
  37. 'uni-fab__circle--rightTop': rightTop,
  38. 'uni-fab__content--other-platform': !isAndroidNvue
  39. }" class="uni-fab__circle uni-fab__plus" :style="{ 'background-color': styles.buttonColor, 'bottom': nvueBottom }"
  40. @click="_onClick">
  41. <uni-badge class="uni-badge-left-margin" :is-dot="isDot" :text="dotCount" absolute="rightTop" size="small">
  42. <uni-icons class="fab-circle-icon" :type="styles.icon" :color="styles.iconColor" size="32"
  43. :class="{ 'uni-fab__plus--active': isShow && content.length > 0 }"></uni-icons>
  44. </uni-badge>
  45. <!-- <view class="fab-circle-v" :class="{'uni-fab__plus--active': isShow && content.length > 0}"></view>
  46. <view class="fab-circle-h" :class="{'uni-fab__plus--active': isShow && content.length > 0}"></view> -->
  47. </view>
  48. </view>
  49. </template>
  50. <script>
  51. let platform = 'other'
  52. // #ifdef APP-NVUE
  53. platform = uni.getSystemInfoSync().platform
  54. // #endif
  55. /**
  56. * Fab 悬浮按钮
  57. * @description 点击可展开一个图形按钮菜单
  58. * @tutorial https://ext.dcloud.net.cn/plugin?id=144
  59. * @property {Object} pattern 可选样式配置项
  60. * @property {Object} horizontal = [left | right] 水平对齐方式
  61. * @value left 左对齐
  62. * @value right 右对齐
  63. * @property {Object} vertical = [bottom | top] 垂直对齐方式
  64. * @value bottom 下对齐
  65. * @value top 上对齐
  66. * @property {Object} direction = [horizontal | vertical] 展开菜单显示方式
  67. * @value horizontal 水平显示
  68. * @value vertical 垂直显示
  69. * @property {Array} content 展开菜单内容配置项
  70. * @property {Boolean} popMenu 是否使用弹出菜单
  71. * @event {Function} trigger 展开菜单点击事件,返回点击信息
  72. * @event {Function} fabClick 悬浮按钮点击事件
  73. */
  74. import uniBadge from '@dcloudio/uni-ui/lib/uni-badge/uni-badge.vue'
  75. export default {
  76. name: 'UniFab',
  77. emits: ['fabClick', 'trigger'],
  78. components: [uniBadge],
  79. props: {
  80. pattern: {
  81. type: Object,
  82. default() {
  83. return {}
  84. }
  85. },
  86. horizontal: {
  87. type: String,
  88. default: 'left'
  89. },
  90. vertical: {
  91. type: String,
  92. default: 'bottom'
  93. },
  94. direction: {
  95. type: String,
  96. default: 'horizontal'
  97. },
  98. content: {
  99. type: Array,
  100. default() {
  101. return []
  102. }
  103. },
  104. show: {
  105. type: Boolean,
  106. default: false
  107. },
  108. popMenu: {
  109. type: Boolean,
  110. default: true
  111. },
  112. isDot:{
  113. type: Boolean,
  114. default: false
  115. },
  116. showFabBtn:{
  117. type: Boolean,
  118. default: false
  119. },
  120. dotCount:{
  121. type: [String, Number],
  122. }
  123. },
  124. data() {
  125. return {
  126. fabShow: false,
  127. isShow: false,
  128. isAndroidNvue: platform === 'android',
  129. styles: {
  130. color: '#3c3e49',
  131. selectedColor: '#007AFF',
  132. backgroundColor: '#fff',
  133. buttonColor: '#007AFF',
  134. iconColor: '#fff',
  135. icon: 'plusempty'
  136. }
  137. }
  138. },
  139. computed: {
  140. contentWidth(e) {
  141. return (this.content.length + 1) * 55 + 15 + 'px'
  142. },
  143. contentWidthMin() {
  144. return '55px'
  145. },
  146. // 动态计算宽度
  147. boxWidth() {
  148. return this.getPosition(3, 'horizontal')
  149. },
  150. // 动态计算高度
  151. boxHeight() {
  152. return this.getPosition(3, 'vertical')
  153. },
  154. // 计算左下位置
  155. leftBottom() {
  156. return this.getPosition(0, 'left', 'bottom')
  157. },
  158. // 计算右下位置
  159. rightBottom() {
  160. return this.getPosition(0, 'right', 'bottom')
  161. },
  162. // 计算左上位置
  163. leftTop() {
  164. return this.getPosition(0, 'left', 'top')
  165. },
  166. rightTop() {
  167. return this.getPosition(0, 'right', 'top')
  168. },
  169. flexDirectionStart() {
  170. return this.getPosition(1, 'vertical', 'top')
  171. },
  172. flexDirectionEnd() {
  173. return this.getPosition(1, 'vertical', 'bottom')
  174. },
  175. horizontalLeft() {
  176. return this.getPosition(2, 'horizontal', 'left')
  177. },
  178. horizontalRight() {
  179. return this.getPosition(2, 'horizontal', 'right')
  180. },
  181. // 计算 nvue bottom
  182. nvueBottom() {
  183. const safeBottom = uni.getSystemInfoSync().windowBottom;
  184. // #ifdef APP-NVUE
  185. return 30 + safeBottom
  186. // #endif
  187. // #ifndef APP-NVUE
  188. return 30
  189. // #endif
  190. }
  191. },
  192. watch: {
  193. pattern: {
  194. handler(val, oldVal) {
  195. this.styles = Object.assign({}, this.styles, val)
  196. },
  197. deep: true
  198. }
  199. },
  200. created() {
  201. this.isShow = this.show
  202. if (this.top === 0) {
  203. this.fabShow = true
  204. }
  205. // 初始化样式
  206. this.styles = Object.assign({}, this.styles, this.pattern)
  207. },
  208. methods: {
  209. _onClick() {
  210. this.$emit('fabClick')
  211. if (!this.popMenu) {
  212. return
  213. }
  214. this.isShow = !this.isShow
  215. },
  216. open() {
  217. this.isShow = true
  218. },
  219. close() {
  220. this.isShow = false
  221. },
  222. /**
  223. * 按钮点击事件
  224. */
  225. _onItemClick(index, item) {
  226. if (!this.isShow) {
  227. return
  228. }
  229. this.$emit('trigger', {
  230. index,
  231. item
  232. })
  233. },
  234. /**
  235. * 获取 位置信息
  236. */
  237. getPosition(types, paramA, paramB) {
  238. if (types === 0) {
  239. return this.horizontal === paramA && this.vertical === paramB
  240. } else if (types === 1) {
  241. return this.direction === paramA && this.vertical === paramB
  242. } else if (types === 2) {
  243. return this.direction === paramA && this.horizontal === paramB
  244. } else {
  245. return this.isShow && this.direction === paramA ? this.contentWidth : this.contentWidthMin
  246. }
  247. }
  248. }
  249. }
  250. </script>
  251. <style lang="scss">
  252. $uni-shadow-base: 0 1px 5px 2px rgba($color: #000000, $alpha: 0.3) !default;
  253. .uni-fab {
  254. position: fixed;
  255. /* #ifndef APP-NVUE */
  256. display: flex;
  257. /* #endif */
  258. justify-content: center;
  259. align-items: center;
  260. z-index: 10;
  261. border-radius: 45px;
  262. box-shadow: $uni-shadow-base;
  263. }
  264. .uni-cursor-point {
  265. /* #ifdef H5 */
  266. cursor: pointer;
  267. /* #endif */
  268. }
  269. .uni-fab--active {
  270. opacity: 1;
  271. }
  272. .uni-fab--leftBottom {
  273. left: 15px;
  274. bottom: 30px;
  275. /* #ifdef H5 */
  276. left: calc(15px + var(--window-left));
  277. bottom: calc(30px + var(--window-bottom));
  278. /* #endif */
  279. // padding: 10px;
  280. }
  281. .uni-fab--leftTop {
  282. left: 15px;
  283. top: 30px;
  284. /* #ifdef H5 */
  285. left: calc(15px + var(--window-left));
  286. top: calc(30px + var(--window-top));
  287. /* #endif */
  288. // padding: 10px;
  289. }
  290. .uni-fab--rightBottom {
  291. right: 15px;
  292. bottom: 30px;
  293. /* #ifdef H5 */
  294. right: calc(15px + var(--window-right));
  295. bottom: calc(30px + var(--window-bottom));
  296. /* #endif */
  297. // padding: 10px;
  298. }
  299. .uni-fab--rightTop {
  300. right: 15px;
  301. top: 30px;
  302. /* #ifdef H5 */
  303. right: calc(15px + var(--window-right));
  304. top: calc(30px + var(--window-top));
  305. /* #endif */
  306. // padding: 10px;
  307. }
  308. .uni-fab__circle {
  309. position: fixed;
  310. /* #ifndef APP-NVUE */
  311. display: flex;
  312. /* #endif */
  313. justify-content: center;
  314. align-items: center;
  315. width: 55px;
  316. height: 55px;
  317. background-color: #3c3e49;
  318. border-radius: 45px;
  319. z-index: 11;
  320. // box-shadow: $uni-shadow-base;
  321. }
  322. .uni-fab__circle--leftBottom {
  323. left: 15px;
  324. bottom: 30px;
  325. /* #ifdef H5 */
  326. left: calc(15px + var(--window-left));
  327. bottom: calc(30px + var(--window-bottom));
  328. /* #endif */
  329. }
  330. .uni-fab__circle--leftTop {
  331. left: 15px;
  332. top: 30px;
  333. /* #ifdef H5 */
  334. left: calc(15px + var(--window-left));
  335. top: calc(30px + var(--window-top));
  336. /* #endif */
  337. }
  338. .uni-fab__circle--rightBottom {
  339. right: 15px;
  340. bottom: 30px;
  341. /* #ifdef H5 */
  342. right: calc(15px + var(--window-right));
  343. bottom: calc(30px + var(--window-bottom));
  344. /* #endif */
  345. }
  346. .uni-fab__circle--rightTop {
  347. right: 15px;
  348. top: 30px;
  349. /* #ifdef H5 */
  350. right: calc(15px + var(--window-right));
  351. top: calc(30px + var(--window-top));
  352. /* #endif */
  353. }
  354. .uni-fab__circle--left {
  355. left: 0;
  356. }
  357. .uni-fab__circle--right {
  358. right: 0;
  359. }
  360. .uni-fab__circle--top {
  361. top: 0;
  362. }
  363. .uni-fab__circle--bottom {
  364. bottom: 0;
  365. }
  366. .uni-fab__plus {
  367. font-weight: bold;
  368. }
  369. // .fab-circle-v {
  370. // position: absolute;
  371. // width: 2px;
  372. // height: 24px;
  373. // left: 0;
  374. // top: 0;
  375. // right: 0;
  376. // bottom: 0;
  377. // /* #ifndef APP-NVUE */
  378. // margin: auto;
  379. // /* #endif */
  380. // background-color: white;
  381. // transform: rotate(0deg);
  382. // transition: transform 0.3s;
  383. // }
  384. // .fab-circle-h {
  385. // position: absolute;
  386. // width: 24px;
  387. // height: 2px;
  388. // left: 0;
  389. // top: 0;
  390. // right: 0;
  391. // bottom: 0;
  392. // /* #ifndef APP-NVUE */
  393. // margin: auto;
  394. // /* #endif */
  395. // background-color: white;
  396. // transform: rotate(0deg);
  397. // transition: transform 0.3s;
  398. // }
  399. .fab-circle-icon {
  400. transform: rotate(0deg);
  401. transition: transform 0.3s;
  402. font-weight: 200;
  403. }
  404. .uni-fab__plus--active {
  405. transform: rotate(135deg);
  406. }
  407. .uni-fab__content {
  408. /* #ifndef APP-NVUE */
  409. box-sizing: border-box;
  410. display: flex;
  411. /* #endif */
  412. flex-direction: row;
  413. border-radius: 55px;
  414. overflow: hidden;
  415. transition-property: width, height;
  416. transition-duration: 0.2s;
  417. width: 55px;
  418. border-color: #DDDDDD;
  419. border-width: 1rpx;
  420. border-style: solid;
  421. }
  422. .uni-fab__content--other-platform {
  423. border-width: 0px;
  424. box-shadow: $uni-shadow-base;
  425. }
  426. .uni-fab__content--left {
  427. justify-content: flex-start;
  428. }
  429. .uni-fab__content--right {
  430. justify-content: flex-end;
  431. }
  432. .uni-fab__content--flexDirection {
  433. flex-direction: column;
  434. justify-content: flex-end;
  435. }
  436. .uni-fab__content--flexDirectionStart {
  437. flex-direction: column;
  438. justify-content: flex-start;
  439. }
  440. .uni-fab__content--flexDirectionEnd {
  441. flex-direction: column;
  442. justify-content: flex-end;
  443. }
  444. .uni-fab__item {
  445. /* #ifndef APP-NVUE */
  446. display: flex;
  447. /* #endif */
  448. flex-direction: column;
  449. justify-content: center;
  450. align-items: center;
  451. width: 55px;
  452. height: 55px;
  453. opacity: 0;
  454. transition: opacity 0.2s;
  455. }
  456. .uni-fab__item--active {
  457. opacity: 1;
  458. }
  459. .uni-fab__item-image {
  460. width: 20px;
  461. height: 20px;
  462. margin-bottom: 4px;
  463. }
  464. .uni-fab__item-text {
  465. color: #FFFFFF;
  466. font-size: 12px;
  467. line-height: 12px;
  468. margin-top: 2px;
  469. }
  470. .uni-fab__item--first {
  471. width: 55px;
  472. }
  473. </style>