uni-fab.vue 9.8 KB

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