round-fab.vue 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. <template>
  2. <view>
  3. <view v-if="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. <view v-if="flexDirectionStart || horizontalLeft" class="uni-fab__item uni-fab__item--first" />
  20. <view v-for="(item, index) in content" :key="index" :class="{ 'uni-fab__item--active': isShow }" class="uni-fab__item"
  21. @click="_onItemClick(index, item)">
  22. <image :src="item.active ? item.selectedIconPath : item.iconPath" class="uni-fab__item-image" mode="widthFix" />
  23. <text class="uni-fab__item-text" :style="{ color: item.active ? styles.selectedColor : styles.color }">{{ item.text }}</text>
  24. </view>
  25. <view v-if="flexDirectionEnd || horizontalRight" class="uni-fab__item uni-fab__item--first" />
  26. </view>
  27. <view :class="{
  28. 'uni-fab__circle--left': horizontal === 'left' && direction === 'horizontal',
  29. 'uni-fab__circle--top': vertical === 'top' && direction === 'vertical',
  30. 'uni-fab__circle--bottom': vertical === 'bottom' && direction === 'vertical',
  31. 'uni-fab__circle--right': horizontal === 'right' && direction === 'horizontal',
  32. 'uni-fab__plus--active': isShow,
  33. 'uni-fab__content--other-platform': !isAndroidNvue
  34. }"
  35. class="uni-fab__circle uni-fab__plus" :style="{ 'background-color': styles.buttonColor }" @click="_onClick">
  36. <view class="fab-circle-v"></view>
  37. <view class="fab-circle-h"></view>
  38. </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. export default {
  48. name: 'UniFab',
  49. props: {
  50. pattern: {
  51. type: Object,
  52. default () {
  53. return {}
  54. }
  55. },
  56. horizontal: {
  57. type: String,
  58. default: 'left'
  59. },
  60. vertical: {
  61. type: String,
  62. default: 'bottom'
  63. },
  64. direction: {
  65. type: String,
  66. default: 'horizontal'
  67. },
  68. content: {
  69. type: Array,
  70. default () {
  71. return []
  72. }
  73. },
  74. show: {
  75. type: Boolean,
  76. default: false
  77. }
  78. },
  79. data() {
  80. return {
  81. fabShow: false,
  82. isShow: false,
  83. isAndroidNvue: platform === 'android',
  84. styles: {
  85. color: '#3c3e49',
  86. selectedColor: '#007AFF',
  87. backgroundColor: '#fff',
  88. buttonColor: '#3c3e49'
  89. }
  90. }
  91. },
  92. computed: {
  93. contentWidth(e) {
  94. return (this.content.length + 1) * 55 + 10 + 'px'
  95. },
  96. contentWidthMin() {
  97. return 55 + 'px'
  98. },
  99. // 动态计算宽度
  100. boxWidth() {
  101. return this.getPosition(3, 'horizontal')
  102. },
  103. // 动态计算高度
  104. boxHeight() {
  105. return this.getPosition(3, 'vertical')
  106. },
  107. // 计算左下位置
  108. leftBottom() {
  109. return this.getPosition(0, 'left', 'bottom')
  110. },
  111. // 计算右下位置
  112. rightBottom() {
  113. return this.getPosition(0, 'right', 'bottom')
  114. },
  115. // 计算左上位置
  116. leftTop() {
  117. return this.getPosition(0, 'left', 'top')
  118. },
  119. rightTop() {
  120. return this.getPosition(0, 'right', 'top')
  121. },
  122. flexDirectionStart() {
  123. return this.getPosition(1, 'vertical', 'top')
  124. },
  125. flexDirectionEnd() {
  126. return this.getPosition(1, 'vertical', 'bottom')
  127. },
  128. horizontalLeft() {
  129. return this.getPosition(2, 'horizontal', 'left')
  130. },
  131. horizontalRight() {
  132. return this.getPosition(2, 'horizontal', 'right')
  133. }
  134. },
  135. watch: {
  136. pattern(newValue, oldValue) {
  137. //console.log(JSON.stringify(newValue))
  138. this.styles = Object.assign({}, this.styles, newValue)
  139. }
  140. },
  141. created() {
  142. this.isShow = this.show
  143. if (this.top === 0) {
  144. this.fabShow = true
  145. }
  146. // 初始化样式
  147. this.styles = Object.assign({}, this.styles, this.pattern)
  148. },
  149. methods: {
  150. _onClick() {
  151. this.isShow = !this.isShow
  152. },
  153. open() {
  154. this.isShow = true
  155. },
  156. close() {
  157. this.isShow = false
  158. },
  159. /**
  160. * 按钮点击事件
  161. */
  162. _onItemClick(index, item) {
  163. this.$emit('trigger', {
  164. index,
  165. item
  166. })
  167. },
  168. /**
  169. * 获取 位置信息
  170. */
  171. getPosition(types, paramA, paramB) {
  172. if (types === 0) {
  173. return this.horizontal === paramA && this.vertical === paramB
  174. } else if (types === 1) {
  175. return this.direction === paramA && this.vertical === paramB
  176. } else if (types === 2) {
  177. return this.direction === paramA && this.horizontal === paramB
  178. } else {
  179. return this.isShow && this.direction === paramA ? this.contentWidth : this.contentWidthMin
  180. }
  181. }
  182. }
  183. }
  184. </script>
  185. <style scoped>
  186. .uni-fab {
  187. position: fixed;
  188. /* #ifndef APP-NVUE */
  189. display: flex;
  190. /* #endif */
  191. justify-content: center;
  192. align-items: center;
  193. z-index: 10;
  194. }
  195. .uni-fab--top {
  196. width: 30px;
  197. height: 30px;
  198. right: 15px;
  199. bottom: 30px;
  200. border-style: solid;
  201. border-width: 1px;
  202. border-color: #5989b9;
  203. border-radius: 5px;
  204. transition: opacity 0.3;
  205. opacity: 0;
  206. }
  207. .uni-fab--active {
  208. opacity: 1;
  209. }
  210. .uni-fab--leftBottom {
  211. left: 15px;
  212. bottom: 30px;
  213. }
  214. .uni-fab--leftTop {
  215. left: 15px;
  216. top: 40px;
  217. /* #ifdef H5 */
  218. top: calc(40px + var(--window-top));
  219. /* #endif */
  220. }
  221. .uni-fab--rightBottom {
  222. right: 15px;
  223. bottom: 30px;
  224. }
  225. .uni-fab--rightTop {
  226. right: 15px;
  227. top: 40px;
  228. /* #ifdef H5 */
  229. top: calc(40px + var(--window-top));
  230. /* #endif */
  231. }
  232. .uni-fab__circle {
  233. /* #ifndef APP-NVUE */
  234. display: flex;
  235. /* #endif */
  236. justify-content: center;
  237. align-items: center;
  238. position: absolute;
  239. width: 55px;
  240. height: 55px;
  241. background-color: #3c3e49;
  242. border-radius: 55px;
  243. z-index: 11;
  244. }
  245. .uni-fab__circle--left {
  246. left: 0;
  247. }
  248. .uni-fab__circle--right {
  249. right: 0;
  250. }
  251. .uni-fab__circle--top {
  252. top: 0;
  253. }
  254. .uni-fab__circle--bottom {
  255. bottom: 0;
  256. }
  257. .uni-fab__plus {
  258. transform: rotate(0deg);
  259. transition: transform 0.3s;
  260. font-weight: bold;
  261. }
  262. .uni-fab__plus--active {
  263. transform: rotate(135deg);
  264. }
  265. .fab-circle-v {
  266. position: absolute;
  267. width: 3px;
  268. height: 31px;
  269. left: 26px;
  270. top: 12px;
  271. background-color: white;
  272. }
  273. .fab-circle-h {
  274. position: absolute;
  275. width: 31px;
  276. height: 3px;
  277. left: 12px;
  278. top: 26px;
  279. background-color: white;
  280. }
  281. .uni-fab__content {
  282. /* #ifndef APP-NVUE */
  283. box-sizing: border-box;
  284. display: flex;
  285. /* #endif */
  286. flex-direction: row;
  287. border-radius: 55px;
  288. overflow: hidden;
  289. transition-property: width, height;
  290. transition-duration: 0.2s;
  291. width: 55px;
  292. border-color: #DDDDDD;
  293. border-width: 1rpx;
  294. border-style: solid;
  295. }
  296. .uni-fab__content--other-platform {
  297. border-width: 0px;
  298. box-shadow: 0 0 5px 2px rgba(0, 0, 0, 0.2);
  299. }
  300. .uni-fab__content--left {
  301. justify-content: flex-start;
  302. }
  303. .uni-fab__content--right {
  304. justify-content: flex-end;
  305. }
  306. .uni-fab__content--flexDirection {
  307. flex-direction: column;
  308. justify-content: flex-end;
  309. }
  310. .uni-fab__content--flexDirectionStart {
  311. flex-direction: column;
  312. justify-content: flex-start;
  313. }
  314. .uni-fab__content--flexDirectionEnd {
  315. flex-direction: column;
  316. justify-content: flex-end;
  317. }
  318. .uni-fab__item {
  319. /* #ifndef APP-NVUE */
  320. display: flex;
  321. /* #endif */
  322. flex-direction: column;
  323. justify-content: center;
  324. align-items: center;
  325. width: 55px;
  326. height: 55px;
  327. opacity: 0;
  328. transition: opacity 0.2s;
  329. }
  330. .uni-fab__item--active {
  331. opacity: 1;
  332. }
  333. .uni-fab__item-image {
  334. width: 25px;
  335. height: 25px;
  336. margin-bottom: 3px;
  337. }
  338. .uni-fab__item-text {
  339. color: #FFFFFF;
  340. font-size: 12px;
  341. }
  342. .uni-fab__item--first {
  343. width: 55px;
  344. }
  345. </style>