uni-fab.vue.bak 7.8 KB

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