uni-fab.vue 8.7 KB

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