uni-fab.vue.bak 7.7 KB

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