uni-fab.vue 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. <template>
  2. <view>
  3. <view v-if="popMenu && (leftBottom||rightBottom||leftTop||rightTop)" :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}"></view>
  38. <view class="fab-circle-h" :class="{'uni-fab__plus--active': isShow}"></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: '#3c3e49'
  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-fab--active {
  223. opacity: 1;
  224. }
  225. .uni-fab--leftBottom {
  226. left: 5px;
  227. bottom: 20px;
  228. /* #ifdef H5 */
  229. bottom: calc(20px + var(--window-bottom));
  230. /* #endif */
  231. padding: 10px;
  232. }
  233. .uni-fab--leftTop {
  234. left: 5px;
  235. top: 30px;
  236. /* #ifdef H5 */
  237. top: calc(30px + var(--window-top));
  238. /* #endif */
  239. padding: 10px;
  240. }
  241. .uni-fab--rightBottom {
  242. right: 5px;
  243. bottom: 20px;
  244. /* #ifdef H5 */
  245. bottom: calc(20px + var(--window-bottom));
  246. /* #endif */
  247. padding: 10px;
  248. }
  249. .uni-fab--rightTop {
  250. right: 5px;
  251. top: 30px;
  252. /* #ifdef H5 */
  253. top: calc(30px + var(--window-top));
  254. /* #endif */
  255. padding: 10px;
  256. }
  257. .uni-fab__circle {
  258. position: fixed;
  259. /* #ifndef APP-NVUE */
  260. display: flex;
  261. /* #endif */
  262. justify-content: center;
  263. align-items: center;
  264. width: 55px;
  265. height: 55px;
  266. background-color: #3c3e49;
  267. border-radius: 55px;
  268. z-index: 11;
  269. }
  270. .uni-fab__circle--leftBottom {
  271. left: 15px;
  272. bottom: 30px;
  273. /* #ifdef H5 */
  274. bottom: calc(30px + var(--window-bottom));
  275. /* #endif */
  276. }
  277. .uni-fab__circle--leftTop {
  278. left: 15px;
  279. top: 40px;
  280. /* #ifdef H5 */
  281. top: calc(40px + var(--window-top));
  282. /* #endif */
  283. }
  284. .uni-fab__circle--rightBottom {
  285. right: 15px;
  286. bottom: 30px;
  287. /* #ifdef H5 */
  288. bottom: calc(30px + var(--window-bottom));
  289. /* #endif */
  290. }
  291. .uni-fab__circle--rightTop {
  292. right: 15px;
  293. top: 40px;
  294. /* #ifdef H5 */
  295. top: calc(40px + var(--window-top));
  296. /* #endif */
  297. }
  298. .uni-fab__circle--left {
  299. left: 0;
  300. }
  301. .uni-fab__circle--right {
  302. right: 0;
  303. }
  304. .uni-fab__circle--top {
  305. top: 0;
  306. }
  307. .uni-fab__circle--bottom {
  308. bottom: 0;
  309. }
  310. .uni-fab__plus {
  311. font-weight: bold;
  312. }
  313. .fab-circle-v {
  314. position: absolute;
  315. width: 3px;
  316. height: 31px;
  317. left: 26px;
  318. top: 12px;
  319. background-color: white;
  320. transform: rotate(0deg);
  321. transition: transform 0.3s;
  322. }
  323. .fab-circle-h {
  324. position: absolute;
  325. width: 31px;
  326. height: 3px;
  327. left: 12px;
  328. top: 26px;
  329. background-color: white;
  330. transform: rotate(0deg);
  331. transition: transform 0.3s;
  332. }
  333. .uni-fab__plus--active {
  334. transform: rotate(135deg);
  335. }
  336. .uni-fab__content {
  337. /* #ifndef APP-NVUE */
  338. box-sizing: border-box;
  339. display: flex;
  340. /* #endif */
  341. flex-direction: row;
  342. border-radius: 55px;
  343. overflow: hidden;
  344. transition-property: width, height;
  345. transition-duration: 0.2s;
  346. width: 55px;
  347. border-color: #DDDDDD;
  348. border-width: 1rpx;
  349. border-style: solid;
  350. }
  351. .uni-fab__content--other-platform {
  352. border-width: 0px;
  353. box-shadow: 0 0 5px 2px rgba(0, 0, 0, 0.2);
  354. }
  355. .uni-fab__content--left {
  356. justify-content: flex-start;
  357. }
  358. .uni-fab__content--right {
  359. justify-content: flex-end;
  360. }
  361. .uni-fab__content--flexDirection {
  362. flex-direction: column;
  363. justify-content: flex-end;
  364. }
  365. .uni-fab__content--flexDirectionStart {
  366. flex-direction: column;
  367. justify-content: flex-start;
  368. }
  369. .uni-fab__content--flexDirectionEnd {
  370. flex-direction: column;
  371. justify-content: flex-end;
  372. }
  373. .uni-fab__item {
  374. /* #ifndef APP-NVUE */
  375. display: flex;
  376. /* #endif */
  377. flex-direction: column;
  378. justify-content: center;
  379. align-items: center;
  380. width: 55px;
  381. height: 55px;
  382. opacity: 0;
  383. transition: opacity 0.2s;
  384. }
  385. .uni-fab__item--active {
  386. opacity: 1;
  387. }
  388. .uni-fab__item-image {
  389. width: 25px;
  390. height: 25px;
  391. margin-bottom: 3px;
  392. }
  393. .uni-fab__item-text {
  394. color: #FFFFFF;
  395. font-size: 12px;
  396. }
  397. .uni-fab__item--first {
  398. width: 55px;
  399. }
  400. </style>