round-fab.vue 9.7 KB

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