uni-fab.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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. this.$emit('trigger', {
  208. index,
  209. item
  210. })
  211. },
  212. /**
  213. * 获取 位置信息
  214. */
  215. getPosition(types, paramA, paramB) {
  216. if (types === 0) {
  217. return this.horizontal === paramA && this.vertical === paramB
  218. } else if (types === 1) {
  219. return this.direction === paramA && this.vertical === paramB
  220. } else if (types === 2) {
  221. return this.direction === paramA && this.horizontal === paramB
  222. } else {
  223. return this.isShow && this.direction === paramA ? this.contentWidth : this.contentWidthMin
  224. }
  225. }
  226. }
  227. }
  228. </script>
  229. <style lang="scss" >
  230. $uni-shadow-base:0 1px 5px 2px rgba($color: #000000, $alpha: 0.3) !default;
  231. .uni-fab {
  232. position: fixed;
  233. /* #ifndef APP-NVUE */
  234. display: flex;
  235. /* #endif */
  236. justify-content: center;
  237. align-items: center;
  238. z-index: 10;
  239. border-radius: 45px;
  240. box-shadow: $uni-shadow-base;
  241. }
  242. .uni-cursor-point {
  243. /* #ifdef H5 */
  244. cursor: pointer;
  245. /* #endif */
  246. }
  247. .uni-fab--active {
  248. opacity: 1;
  249. }
  250. .uni-fab--leftBottom {
  251. left: 15px;
  252. bottom: 30px;
  253. /* #ifdef H5 */
  254. left: calc(15px + var(--window-left));
  255. bottom: calc(30px + var(--window-bottom));
  256. /* #endif */
  257. // padding: 10px;
  258. }
  259. .uni-fab--leftTop {
  260. left: 15px;
  261. top: 30px;
  262. /* #ifdef H5 */
  263. left: calc(15px + var(--window-left));
  264. top: calc(30px + var(--window-top));
  265. /* #endif */
  266. // padding: 10px;
  267. }
  268. .uni-fab--rightBottom {
  269. right: 15px;
  270. bottom: 30px;
  271. /* #ifdef H5 */
  272. right: calc(15px + var(--window-right));
  273. bottom: calc(30px + var(--window-bottom));
  274. /* #endif */
  275. // padding: 10px;
  276. }
  277. .uni-fab--rightTop {
  278. right: 15px;
  279. top: 30px;
  280. /* #ifdef H5 */
  281. right: calc(15px + var(--window-right));
  282. top: calc(30px + var(--window-top));
  283. /* #endif */
  284. // padding: 10px;
  285. }
  286. .uni-fab__circle {
  287. position: fixed;
  288. /* #ifndef APP-NVUE */
  289. display: flex;
  290. /* #endif */
  291. justify-content: center;
  292. align-items: center;
  293. width: 55px;
  294. height: 55px;
  295. background-color: #3c3e49;
  296. border-radius: 45px;
  297. z-index: 11;
  298. // box-shadow: $uni-shadow-base;
  299. }
  300. .uni-fab__circle--leftBottom {
  301. left: 15px;
  302. bottom: 30px;
  303. /* #ifdef H5 */
  304. left: calc(15px + var(--window-left));
  305. bottom: calc(30px + var(--window-bottom));
  306. /* #endif */
  307. }
  308. .uni-fab__circle--leftTop {
  309. left: 15px;
  310. top: 30px;
  311. /* #ifdef H5 */
  312. left: calc(15px + var(--window-left));
  313. top: calc(30px + var(--window-top));
  314. /* #endif */
  315. }
  316. .uni-fab__circle--rightBottom {
  317. right: 15px;
  318. bottom: 30px;
  319. /* #ifdef H5 */
  320. right: calc(15px + var(--window-right));
  321. bottom: calc(30px + var(--window-bottom));
  322. /* #endif */
  323. }
  324. .uni-fab__circle--rightTop {
  325. right: 15px;
  326. top: 30px;
  327. /* #ifdef H5 */
  328. right: calc(15px + var(--window-right));
  329. top: calc(30px + var(--window-top));
  330. /* #endif */
  331. }
  332. .uni-fab__circle--left {
  333. left: 0;
  334. }
  335. .uni-fab__circle--right {
  336. right: 0;
  337. }
  338. .uni-fab__circle--top {
  339. top: 0;
  340. }
  341. .uni-fab__circle--bottom {
  342. bottom: 0;
  343. }
  344. .uni-fab__plus {
  345. font-weight: bold;
  346. }
  347. // .fab-circle-v {
  348. // position: absolute;
  349. // width: 2px;
  350. // height: 24px;
  351. // left: 0;
  352. // top: 0;
  353. // right: 0;
  354. // bottom: 0;
  355. // /* #ifndef APP-NVUE */
  356. // margin: auto;
  357. // /* #endif */
  358. // background-color: white;
  359. // transform: rotate(0deg);
  360. // transition: transform 0.3s;
  361. // }
  362. // .fab-circle-h {
  363. // position: absolute;
  364. // width: 24px;
  365. // height: 2px;
  366. // left: 0;
  367. // top: 0;
  368. // right: 0;
  369. // bottom: 0;
  370. // /* #ifndef APP-NVUE */
  371. // margin: auto;
  372. // /* #endif */
  373. // background-color: white;
  374. // transform: rotate(0deg);
  375. // transition: transform 0.3s;
  376. // }
  377. .fab-circle-icon {
  378. transform: rotate(0deg);
  379. transition: transform 0.3s;
  380. font-weight: 200;
  381. }
  382. .uni-fab__plus--active {
  383. transform: rotate(135deg);
  384. }
  385. .uni-fab__content {
  386. /* #ifndef APP-NVUE */
  387. box-sizing: border-box;
  388. display: flex;
  389. /* #endif */
  390. flex-direction: row;
  391. border-radius: 55px;
  392. overflow: hidden;
  393. transition-property: width, height;
  394. transition-duration: 0.2s;
  395. width: 55px;
  396. border-color: #DDDDDD;
  397. border-width: 1rpx;
  398. border-style: solid;
  399. }
  400. .uni-fab__content--other-platform {
  401. border-width: 0px;
  402. box-shadow: $uni-shadow-base;
  403. }
  404. .uni-fab__content--left {
  405. justify-content: flex-start;
  406. }
  407. .uni-fab__content--right {
  408. justify-content: flex-end;
  409. }
  410. .uni-fab__content--flexDirection {
  411. flex-direction: column;
  412. justify-content: flex-end;
  413. }
  414. .uni-fab__content--flexDirectionStart {
  415. flex-direction: column;
  416. justify-content: flex-start;
  417. }
  418. .uni-fab__content--flexDirectionEnd {
  419. flex-direction: column;
  420. justify-content: flex-end;
  421. }
  422. .uni-fab__item {
  423. /* #ifndef APP-NVUE */
  424. display: flex;
  425. /* #endif */
  426. flex-direction: column;
  427. justify-content: center;
  428. align-items: center;
  429. width: 55px;
  430. height: 55px;
  431. opacity: 0;
  432. transition: opacity 0.2s;
  433. }
  434. .uni-fab__item--active {
  435. opacity: 1;
  436. }
  437. .uni-fab__item-image {
  438. width: 20px;
  439. height: 20px;
  440. margin-bottom: 4px;
  441. }
  442. .uni-fab__item-text {
  443. color: #FFFFFF;
  444. font-size: 12px;
  445. line-height: 12px;
  446. margin-top: 2px;
  447. }
  448. .uni-fab__item--first {
  449. width: 55px;
  450. }
  451. </style>