tm-flotbutton.vue 12 KB

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