uni-fab.vue 11 KB

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