tm-flotbutton.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. <!-- 悬浮按钮 -->
  2. <template>
  3. <view class="nobody fulled"
  4. :class="[absolute?'fulled':'',fixed?'d-inline-block':'',absolute&&position=='bottom'?'flex-end-center height100':'']">
  5. <view
  6. :class="[absolute?'fulled':'',absolute?'relative':'',absolute&&position=='top'?'flex-center':'',absolute&&position=='bottom'?'flex-end-center height100':'',fixed?'d-inline-block':'']">
  7. <view class="flotbtnId" :style="pos" :class="[absolute?'absolute':'',fixed?'fixed':'']">
  8. <slot name="default">
  9. <tm-button
  10. v-if="isRender"
  11. @contact="$emit('contact', $event)"
  12. @error="$emit('error', $event)"
  13. @getphonenumber="$emit('getphonenumber', $event)"
  14. @getuserinfo="$emit('getuserinfo', $event)"
  15. @launchapp="$emit('launchapp', $event)"
  16. @opensetting="$emit('opensetting', $event)"
  17. @longpress="$emit('longpress', $event)"
  18. @touchcancel="$emit('touchcancel', $event)"
  19. :open-type="openType"
  20. @click="click"
  21. :showValue="showText" vertical :label="label" :fontSize="fontSize"
  22. :iconSize="iconSize" :theme="color_tmeme" round="rouned" :font-color="fontColor" :bgcolor="bgcolor"
  23. :size="size" :width="width" :height="width" :icon="icon" fab>
  24. </tm-button>
  25. </slot>
  26. <view v-if="(show || showActions)&&actions.length>0" class="menulistAction" :class="[actionsPos]">
  27. <view class="menulistAction_item" v-for="(item,index) in actions" :key="index">
  28. <!-- <tm-badges :dot="false" :label="item.num"></tm-badges> -->
  29. <text>{{item.num}}</text>
  30. <tm-button :fllowTheme="false" fab @click="itemChange(index)" iconSize="40" :icon="item.icon" :theme="item.color" size="m"></tm-button>
  31. </view>
  32. </view>
  33. </view>
  34. </view>
  35. </view>
  36. </template>
  37. <script>
  38. /**
  39. * 悬浮按钮
  40. * @property {Array} actions = [] 默认:[],悬浮按钮展开的子按钮。格式[{icon,color}]
  41. * @property {String} actions-pos = [top | left | bottom | right] 默认:top,子菜单按钮显示的方向
  42. * @property {String|Boolean} show-actions = [false|true] 默认:false,始终展开子菜单。点击子菜单后不消失.
  43. * @property {String|Boolean} click-actions-hiden = [false|true] 默认:true,点击菜单后是否隐藏所有子菜单。
  44. * @property {String} size = [xs|s|m|n|l|g] 默认:n, 按钮大小。
  45. * @property {String|Number} width = [] 默认:NaN, 自定义按钮大小。
  46. * @property {String|Boolean} show-text = [false|true] 默认:false, 是否显示文字。下下排列结构上图标,下文字。
  47. * @property {String} label = [] 默认:'', 主按钮下方的文字
  48. * @property {String} icon = [] 默认:icon-plus, 默认图标.
  49. * @property {String} bgcolor = [] 默认:"", 自定义-背景颜色
  50. * @property {String|Number} font-size = [22|23|24|26|28] 默认:22, 文字大小
  51. * @property {String} font-color = [] 默认:'', 文字颜色
  52. * @property {String|Number} icon-size = [] 默认:'36', 图标大小。
  53. * @property {String} color = [] 默认:primary,主题颜色
  54. * @property {String|Boolean} absolute = [] 默认:false, 相对父组件定位。
  55. * @property {String|Boolean} fixed = [] 默认:true, 绝对定位,根据屏幕定位。
  56. * @property {String|Boolean} safe = [true|false] 默认:true,// 是否开启底部安全区域。
  57. * @property {String} position = [ topLeft | topRight | bottomRight | bottomLeft|top|bottom|left|right] 默认:bottomRight, 在absolute模式下没有left和right剧中。fixed模式包含所有模式。
  58. * @property {Array} offset = [] 默认: [16, 16], 单位upx,自定义偏移量,left,right
  59. * @property {Function} click 主按钮点击触发的事件。
  60. * @property {Function} change 当有子菜单按钮时,点击子按钮触发事件,并返回顺序Index
  61. * @example <tm-flotbutton label="发文" show-text="true" ></tm-flotbutton>
  62. */
  63. import tmButton from "@/tm-vuetify/components/tm-button/tm-button.vue"
  64. export default {
  65. name:"tm-flotbutton",
  66. components:{tmButton},
  67. props: {
  68. // 子菜单如果有。
  69. actions: {
  70. type: Array,
  71. default: () => {
  72. return [];
  73. }
  74. },
  75. // 同原生btn相同。contact|getPhoneNumber|getUserInfo|launchapp|share|openSetting
  76. openType: {
  77. type: String,
  78. default: ''
  79. },
  80. // 子菜单按钮显示的方向。top | left | bottom | right
  81. actionsPos: {
  82. type: String,
  83. default: 'top'
  84. },
  85. // 始终展开子菜单。
  86. showActions: {
  87. type: String | Boolean,
  88. default: false
  89. },
  90. // 点击菜单后是否隐藏所有子菜单。
  91. clickActionsHiden: {
  92. type: String | Boolean,
  93. default: true
  94. },
  95. // 同button xs,s,m,n,l,g
  96. size: {
  97. type: String,
  98. default: 'n'
  99. },
  100. width: {
  101. type: String | Number,
  102. default: 100
  103. },
  104. height: {
  105. type: String | Number,
  106. default: 100
  107. },
  108. // 是否显示询问文字
  109. showText: {
  110. type: String | Boolean,
  111. default: false
  112. },
  113. // 底部文字。需要和上方同时使用。
  114. label: {
  115. type: String,
  116. default: ''
  117. },
  118. icon: {
  119. type: String,
  120. default: 'icon-plus'
  121. },
  122. // 自定义-背景颜色
  123. bgcolor: {
  124. type: String | Array,
  125. default: ''
  126. },
  127. // 文字大小。
  128. fontSize: {
  129. type: Number | String,
  130. default: 22
  131. },
  132. // 定义文字颜色
  133. fontColor: {
  134. type: String,
  135. default: 'white'
  136. },
  137. // 图标大小。
  138. iconSize: {
  139. type: Number | String,
  140. default: 36
  141. },
  142. // 主题颜色
  143. color: {
  144. type: String,
  145. default: 'primary'
  146. },
  147. // 根据像组件定位四个角和上下居中位置。6个位置。
  148. absolute: {
  149. type: String | Boolean,
  150. default: false
  151. },
  152. //是否开启询问安全距离。如果开启了,会计算底部偏差。
  153. safe:{
  154. type: String | Boolean,
  155. default: true
  156. },
  157. // 要的屏幕的宽高定位四个角和上下左右共8个位置
  158. fixed: {
  159. type: String | Boolean,
  160. default: true
  161. },
  162. // topLeft | topRight | bottomRight | bottomLeft 。在absolute模式下没有left和right剧中。只有top和bottom剧中模式。fixed模式包含所有模式。
  163. position: {
  164. type: String,
  165. default: 'bottomRight'
  166. },
  167. // 单位upx
  168. offset: {
  169. type: Array,
  170. default: () => {
  171. return [16, 16];
  172. }
  173. },
  174. // 跟随主题色的改变而改变。
  175. fllowTheme:{
  176. type:Boolean|String,
  177. default:true
  178. }
  179. },
  180. computed: {
  181. offsets: {
  182. get: function() {
  183. return this.offset;
  184. },
  185. set: function() {
  186. try {
  187. this.offset = [uni.upx2px(this.offset[0]), uni.upx2px(this.offset[1])];
  188. } catch (e) {
  189. this.offset = [0, 0];
  190. }
  191. }
  192. },
  193. color_tmeme:function(){
  194. if(this.$tm.vx.state().tmVuetify.color!==null&&this.$tm.vx.state().tmVuetify.color && this.fllowTheme){
  195. return this.$tm.vx.state().tmVuetify.color;
  196. }
  197. return this.color;
  198. },
  199. pos: {
  200. get: function() {
  201. return this.thisPos;
  202. },
  203. set:function(){
  204. this.thisPos = this.posfun();
  205. }
  206. }
  207. },
  208. data() {
  209. return {
  210. position_info: [],
  211. show: false,
  212. isRender:false,
  213. thisPos:''
  214. };
  215. },
  216. async mounted() {
  217. this.init();
  218. },
  219. methods: {
  220. init(){
  221. let t = this;
  222. this.$nextTick(function(){
  223. t.isRender=true;
  224. t.position_info = [{width:uni.upx2px(this.width),height:uni.upx2px(this.height)}];
  225. t.thisPos = t.posfun();
  226. })
  227. // #ifdef H5
  228. window.addEventListener('scroll',function(){
  229. t.thisPos = t.posfun();
  230. })
  231. // #endif
  232. },
  233. getsafeJl(){
  234. let sy = uni.getSystemInfoSync();
  235. // #ifdef MP
  236. return Math.abs(sy.screenHeight - sy.safeArea.bottom);
  237. // #endif
  238. // #ifdef H5
  239. return Math.abs(sy.windowHeight - sy.safeArea.height);
  240. // #endif
  241. // #ifdef APP
  242. return Math.abs(sy.safeArea.bottom - sy.safeArea.height);
  243. // #endif
  244. return 24;
  245. },
  246. posfun(){
  247. if (this.absolute && !this.fixed) {
  248. if (this.position == 'topLeft') {
  249. return `transform:translateY(${this.offset[1]}px);left:${this.offset[0]}px`;
  250. } else if (this.position == 'topRight') {
  251. return `transform:translateY(${this.offset[1]}px);right:${this.offset[0]}px;`
  252. } else if (this.position == 'bottomRight') {
  253. return `transform:translateY(${this.offset[1]}px);right:${this.offset[0]}px;`;
  254. } else if (this.position == 'bottomLeft') {
  255. return `btransform:translateY(${this.offset[1]}px);left:${this.offset[0]}px`;
  256. } else if (this.position == 'top') {
  257. return `transform:translateX(${this.offset[0]}px) translateY(${this.offset[1]}px)`
  258. } else if (this.position == 'bottom') {
  259. return `transform:translateX(${this.offset[0]}px) translateY(${this.offset[1]}px)`
  260. }
  261. }
  262. if (!this.absolute && this.fixed) {
  263. if (this.position == 'topLeft') {
  264. return `top:${ this.offset[1]}px;left:${this.offset[0]}px`;
  265. } else if (this.position == 'topRight') {
  266. return `top: ${this.offset[1]}px;right: ${this.offset[0]}px`;
  267. } else if (this.position == 'bottomRight') {
  268. let safbo = this.getsafeJl()
  269. if(!this.safe) safbo=0
  270. return `bottom: ${this.offset[1]+safbo}px;right: ${this.offset[0]}px;`;
  271. } else if (this.position == 'bottomLeft') {
  272. let sy = uni.getSystemInfoSync();
  273. let safbo = this.getsafeJl()
  274. if(!this.safe) safbo=0
  275. return `bottom:${ this.offset[1]+safbo}px;left:${this.offset[0]}px;`;
  276. } else if (this.position == 'top') {
  277. let js = uni.getSystemInfoSync();
  278. let left = js.windowWidth;
  279. if (this.position_info.length > 0) {
  280. let w = this.position_info[0].width;
  281. return `transform:translateX(${this.offset[0]}px) translateY(${this.offset[1]}px);top:0;left:${(left-w)/2}px;`;
  282. }
  283. } else if (this.position == 'bottom') {
  284. let safbo = this.getsafeJl()
  285. if(!this.safe) safbo=0
  286. let js = uni.getSystemInfoSync();
  287. let left = js.windowWidth;
  288. if (this.position_info.length > 0) {
  289. let w = this.position_info[0].width;
  290. console.log(w);
  291. return `transform:translateX(${this.offset[0]}px) translateY(${this.offset[1]}px);bottom:${safbo}px;left:${(left-w)/2}px`;
  292. } else if (this.position == 'left') {
  293. let js = uni.getSystemInfoSync();
  294. let left = js.windowHeight;
  295. if (this.position_info.length > 0) {
  296. let w = this.position_info[0].height;
  297. return `transform:translateX(${this.offset[0]}px) translateY(${this.offset[1]}px);left:0;top:${(left-w)/2}px`;
  298. }
  299. } else if (this.position == 'right') {
  300. let js = uni.getSystemInfoSync();
  301. let left = js.windowHeight;
  302. if (this.position_info.length > 0) {
  303. let w = this.position_info[0].height;
  304. return {
  305. transform: `translateX(${this.offset[0]}px) translateY(${this.offset[1]}px)`,
  306. right: 0,
  307. top: (left - w) / 2 + 'px',
  308. };
  309. `transform:translateX(${this.offset[0]}px) translateY(${this.offset[1]}px);right:0;top:{(left-w)/2}px`;
  310. }
  311. }
  312. }
  313. }
  314. },
  315. click(e) {
  316. this.$emit('click', e);
  317. this.show = !this.show;
  318. },
  319. itemChange(index) {
  320. this.$emit('change', index);
  321. if (!this.clickActionsHiden) return;
  322. this.show = !this.show;
  323. }
  324. },
  325. }
  326. </script>
  327. <style lang="scss" scoped>
  328. .nobody {
  329. .menulistAction {
  330. .menulistAction_item {
  331. position: relative;
  332. // #ifdef H5
  333. animation: sscsl 0.5s;
  334. // #endif
  335. }
  336. &.top {
  337. position: absolute;
  338. bottom: 130upx;
  339. left: 10upx;
  340. .menulistAction_item {
  341. margin-bottom: 10px;
  342. }
  343. }
  344. &.left {
  345. position: absolute;
  346. bottom: 10upx;
  347. display: flex;
  348. flex-flow: row;
  349. right: 140upx;
  350. .menulistAction_item {
  351. margin-left: 10px;
  352. }
  353. }
  354. &.bottom {
  355. position: absolute;
  356. top: 140upx;
  357. display: flex;
  358. flex-flow: column;
  359. left: 10upx;
  360. .menulistAction_item {
  361. margin-top: 10px;
  362. }
  363. }
  364. &.right {
  365. position: absolute;
  366. bottom: 10upx;
  367. display: flex;
  368. flex-flow: row;
  369. left: 140upx;
  370. .menulistAction_item {
  371. margin-left: 10px;
  372. }
  373. }
  374. }
  375. }
  376. @keyframes sscsl {
  377. from {
  378. transform: scale(0.5);
  379. }
  380. to {
  381. transform: scale(1);
  382. }
  383. }
  384. .height100 {
  385. height: 100%;
  386. }
  387. .fixed,
  388. .absolute {
  389. z-index: 400;
  390. bottom: 0;
  391. }
  392. </style>