uni-fab.vue 9.8 KB

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