tm-flotbutton.vue 12 KB

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