uni-fab.vue 11 KB

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