uni-fab.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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="plusempty" :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. }
  118. }
  119. },
  120. computed: {
  121. contentWidth(e) {
  122. return (this.content.length + 1) * 55 + 15 + 'px'
  123. },
  124. contentWidthMin() {
  125. return '55px'
  126. },
  127. // 动态计算宽度
  128. boxWidth() {
  129. return this.getPosition(3, 'horizontal')
  130. },
  131. // 动态计算高度
  132. boxHeight() {
  133. return this.getPosition(3, 'vertical')
  134. },
  135. // 计算左下位置
  136. leftBottom() {
  137. return this.getPosition(0, 'left', 'bottom')
  138. },
  139. // 计算右下位置
  140. rightBottom() {
  141. return this.getPosition(0, 'right', 'bottom')
  142. },
  143. // 计算左上位置
  144. leftTop() {
  145. return this.getPosition(0, 'left', 'top')
  146. },
  147. rightTop() {
  148. return this.getPosition(0, 'right', 'top')
  149. },
  150. flexDirectionStart() {
  151. return this.getPosition(1, 'vertical', 'top')
  152. },
  153. flexDirectionEnd() {
  154. return this.getPosition(1, 'vertical', 'bottom')
  155. },
  156. horizontalLeft() {
  157. return this.getPosition(2, 'horizontal', 'left')
  158. },
  159. horizontalRight() {
  160. return this.getPosition(2, 'horizontal', 'right')
  161. },
  162. // 计算 nvue bottom
  163. nvueBottom() {
  164. const safeBottom = uni.getSystemInfoSync().windowBottom;
  165. // #ifdef APP-NVUE
  166. return 30 + safeBottom
  167. // #endif
  168. // #ifndef APP-NVUE
  169. return 30
  170. // #endif
  171. }
  172. },
  173. watch: {
  174. pattern: {
  175. handler(val, oldVal) {
  176. this.styles = Object.assign({}, this.styles, val)
  177. },
  178. deep: true
  179. }
  180. },
  181. created() {
  182. this.isShow = this.show
  183. if (this.top === 0) {
  184. this.fabShow = true
  185. }
  186. // 初始化样式
  187. this.styles = Object.assign({}, this.styles, this.pattern)
  188. },
  189. methods: {
  190. _onClick() {
  191. this.$emit('fabClick')
  192. if (!this.popMenu) {
  193. return
  194. }
  195. this.isShow = !this.isShow
  196. },
  197. open() {
  198. this.isShow = true
  199. },
  200. close() {
  201. this.isShow = false
  202. },
  203. /**
  204. * 按钮点击事件
  205. */
  206. _onItemClick(index, item) {
  207. if (!this.isShow) {
  208. return
  209. }
  210. this.$emit('trigger', {
  211. index,
  212. item
  213. })
  214. },
  215. /**
  216. * 获取 位置信息
  217. */
  218. getPosition(types, paramA, paramB) {
  219. if (types === 0) {
  220. return this.horizontal === paramA && this.vertical === paramB
  221. } else if (types === 1) {
  222. return this.direction === paramA && this.vertical === paramB
  223. } else if (types === 2) {
  224. return this.direction === paramA && this.horizontal === paramB
  225. } else {
  226. return this.isShow && this.direction === paramA ? this.contentWidth : this.contentWidthMin
  227. }
  228. }
  229. }
  230. }
  231. </script>
  232. <style lang="scss" >
  233. $uni-shadow-base:0 1px 5px 2px rgba($color: #000000, $alpha: 0.3) !default;
  234. .uni-fab {
  235. position: fixed;
  236. /* #ifndef APP-NVUE */
  237. display: flex;
  238. /* #endif */
  239. justify-content: center;
  240. align-items: center;
  241. z-index: 10;
  242. border-radius: 45px;
  243. box-shadow: $uni-shadow-base;
  244. }
  245. .uni-cursor-point {
  246. /* #ifdef H5 */
  247. cursor: pointer;
  248. /* #endif */
  249. }
  250. .uni-fab--active {
  251. opacity: 1;
  252. }
  253. .uni-fab--leftBottom {
  254. left: 15px;
  255. bottom: 30px;
  256. /* #ifdef H5 */
  257. left: calc(15px + var(--window-left));
  258. bottom: calc(30px + var(--window-bottom));
  259. /* #endif */
  260. // padding: 10px;
  261. }
  262. .uni-fab--leftTop {
  263. left: 15px;
  264. top: 30px;
  265. /* #ifdef H5 */
  266. left: calc(15px + var(--window-left));
  267. top: calc(30px + var(--window-top));
  268. /* #endif */
  269. // padding: 10px;
  270. }
  271. .uni-fab--rightBottom {
  272. right: 15px;
  273. bottom: 30px;
  274. /* #ifdef H5 */
  275. right: calc(15px + var(--window-right));
  276. bottom: calc(30px + var(--window-bottom));
  277. /* #endif */
  278. // padding: 10px;
  279. }
  280. .uni-fab--rightTop {
  281. right: 15px;
  282. top: 30px;
  283. /* #ifdef H5 */
  284. right: calc(15px + var(--window-right));
  285. top: calc(30px + var(--window-top));
  286. /* #endif */
  287. // padding: 10px;
  288. }
  289. .uni-fab__circle {
  290. position: fixed;
  291. /* #ifndef APP-NVUE */
  292. display: flex;
  293. /* #endif */
  294. justify-content: center;
  295. align-items: center;
  296. width: 55px;
  297. height: 55px;
  298. background-color: #3c3e49;
  299. border-radius: 45px;
  300. z-index: 11;
  301. // box-shadow: $uni-shadow-base;
  302. }
  303. .uni-fab__circle--leftBottom {
  304. left: 15px;
  305. bottom: 30px;
  306. /* #ifdef H5 */
  307. left: calc(15px + var(--window-left));
  308. bottom: calc(30px + var(--window-bottom));
  309. /* #endif */
  310. }
  311. .uni-fab__circle--leftTop {
  312. left: 15px;
  313. top: 30px;
  314. /* #ifdef H5 */
  315. left: calc(15px + var(--window-left));
  316. top: calc(30px + var(--window-top));
  317. /* #endif */
  318. }
  319. .uni-fab__circle--rightBottom {
  320. right: 15px;
  321. bottom: 30px;
  322. /* #ifdef H5 */
  323. right: calc(15px + var(--window-right));
  324. bottom: calc(30px + var(--window-bottom));
  325. /* #endif */
  326. }
  327. .uni-fab__circle--rightTop {
  328. right: 15px;
  329. top: 30px;
  330. /* #ifdef H5 */
  331. right: calc(15px + var(--window-right));
  332. top: calc(30px + var(--window-top));
  333. /* #endif */
  334. }
  335. .uni-fab__circle--left {
  336. left: 0;
  337. }
  338. .uni-fab__circle--right {
  339. right: 0;
  340. }
  341. .uni-fab__circle--top {
  342. top: 0;
  343. }
  344. .uni-fab__circle--bottom {
  345. bottom: 0;
  346. }
  347. .uni-fab__plus {
  348. font-weight: bold;
  349. }
  350. // .fab-circle-v {
  351. // position: absolute;
  352. // width: 2px;
  353. // height: 24px;
  354. // left: 0;
  355. // top: 0;
  356. // right: 0;
  357. // bottom: 0;
  358. // /* #ifndef APP-NVUE */
  359. // margin: auto;
  360. // /* #endif */
  361. // background-color: white;
  362. // transform: rotate(0deg);
  363. // transition: transform 0.3s;
  364. // }
  365. // .fab-circle-h {
  366. // position: absolute;
  367. // width: 24px;
  368. // height: 2px;
  369. // left: 0;
  370. // top: 0;
  371. // right: 0;
  372. // bottom: 0;
  373. // /* #ifndef APP-NVUE */
  374. // margin: auto;
  375. // /* #endif */
  376. // background-color: white;
  377. // transform: rotate(0deg);
  378. // transition: transform 0.3s;
  379. // }
  380. .fab-circle-icon {
  381. transform: rotate(0deg);
  382. transition: transform 0.3s;
  383. font-weight: 200;
  384. }
  385. .uni-fab__plus--active {
  386. transform: rotate(135deg);
  387. }
  388. .uni-fab__content {
  389. /* #ifndef APP-NVUE */
  390. box-sizing: border-box;
  391. display: flex;
  392. /* #endif */
  393. flex-direction: row;
  394. border-radius: 55px;
  395. overflow: hidden;
  396. transition-property: width, height;
  397. transition-duration: 0.2s;
  398. width: 55px;
  399. border-color: #DDDDDD;
  400. border-width: 1rpx;
  401. border-style: solid;
  402. }
  403. .uni-fab__content--other-platform {
  404. border-width: 0px;
  405. box-shadow: $uni-shadow-base;
  406. }
  407. .uni-fab__content--left {
  408. justify-content: flex-start;
  409. }
  410. .uni-fab__content--right {
  411. justify-content: flex-end;
  412. }
  413. .uni-fab__content--flexDirection {
  414. flex-direction: column;
  415. justify-content: flex-end;
  416. }
  417. .uni-fab__content--flexDirectionStart {
  418. flex-direction: column;
  419. justify-content: flex-start;
  420. }
  421. .uni-fab__content--flexDirectionEnd {
  422. flex-direction: column;
  423. justify-content: flex-end;
  424. }
  425. .uni-fab__item {
  426. /* #ifndef APP-NVUE */
  427. display: flex;
  428. /* #endif */
  429. flex-direction: column;
  430. justify-content: center;
  431. align-items: center;
  432. width: 55px;
  433. height: 55px;
  434. opacity: 0;
  435. transition: opacity 0.2s;
  436. }
  437. .uni-fab__item--active {
  438. opacity: 1;
  439. }
  440. .uni-fab__item-image {
  441. width: 20px;
  442. height: 20px;
  443. margin-bottom: 4px;
  444. }
  445. .uni-fab__item-text {
  446. color: #FFFFFF;
  447. font-size: 12px;
  448. line-height: 12px;
  449. margin-top: 2px;
  450. }
  451. .uni-fab__item--first {
  452. width: 55px;
  453. }
  454. </style>