popups.vue 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. <template>
  2. <view class="shadow" :class="!show?'':'shadow-show'" :style="{backgroundColor:show?maskBg:'rgba(0,0,0,0)'}" @tap="tapMask">
  3. <view class="popups" :class="[theme]" :style="{top: popupsTop ,left: popupsLeft,flexDirection:direction}">
  4. <text :class="dynPlace" :style="{width:'0px',height:'0px'}" v-if="triangle"></text>
  5. <view v-for="(item,index) in popData" :key="index" @tap.stop="tapItem(item)" class="itemChild view" :class="[direction=='row'?'solid-right':'solid-bottom',item.disabled?'disabledColor':'']">
  6. <u-icon size="35" :name="item.icon" v-if="item.icon"></u-icon><span class="title">{{item.title}}</span>
  7. </view>
  8. <slot></slot>
  9. </view>
  10. </view>
  11. </template>
  12. <script>
  13. export default {
  14. props: {
  15. maskBg: {
  16. type: String,
  17. default: "rgba(0,0,0,0)",
  18. },
  19. placement: {
  20. type: String,
  21. default: "default", //default top-start top-end bottom-start bottom-end
  22. },
  23. direction: {
  24. type: String,
  25. default: "column", //column row
  26. },
  27. x: {
  28. type: Number,
  29. default: 0,
  30. },
  31. y: {
  32. type: Number,
  33. default: 0,
  34. },
  35. value: {
  36. type: Boolean,
  37. default: false,
  38. },
  39. popData: {
  40. type: Array,
  41. default: () => [],
  42. },
  43. theme: {
  44. type: String,
  45. default: "light", //light dark
  46. },
  47. dynamic: {
  48. type: Boolean,
  49. default: false,
  50. },
  51. gap: {
  52. type: Number,
  53. default: 20,
  54. },
  55. triangle: {
  56. type: Boolean,
  57. default: true,
  58. },
  59. },
  60. data() {
  61. return {
  62. popupsTop: "0rpx",
  63. popupsLeft: "0rpx",
  64. show: false,
  65. dynPlace: "",
  66. };
  67. },
  68. mounted() {
  69. this.popupsPosition();
  70. },
  71. methods: {
  72. tapMask() {
  73. this.$emit("input", !this.value);
  74. },
  75. tapItem(item) {
  76. if (item.disabled) return;
  77. this.$emit("tapPopup", item);
  78. this.$emit("input", !this.value);
  79. },
  80. getStatusBar() {
  81. let promise = new Promise((resolve, reject) => {
  82. uni.getSystemInfo({
  83. success: function (e) {
  84. let customBar;
  85. // #ifdef H5
  86. customBar = e.statusBarHeight + e.windowTop;
  87. // #endif
  88. resolve(customBar);
  89. },
  90. });
  91. });
  92. return promise;
  93. },
  94. async popupsPosition() {
  95. let statusBar = await this.getStatusBar();
  96. let promise = new Promise((resolve, reject) => {
  97. let popupsDom = uni.createSelectorQuery().in(this).select(".popups");
  98. popupsDom
  99. .fields(
  100. {
  101. size: true,
  102. },
  103. (data) => {
  104. let width = data.width;
  105. let height = data.height;
  106. let y = this.dynamic
  107. ? this.dynamicGetY(this.y, this.gap)
  108. : this.transformRpx(this.y);
  109. let x = this.dynamic
  110. ? this.dynamicGetX(this.x, this.gap)
  111. : this.transformRpx(this.x);
  112. // #ifdef H5
  113. y = this.dynamic
  114. ? this.y + statusBar
  115. : this.transformRpx(this.y + statusBar);
  116. // #endif
  117. this.dynPlace =
  118. this.placement == "default"
  119. ? this.getPlacement(x, y)
  120. : this.placement;
  121. switch (this.dynPlace) {
  122. case "top-start":
  123. this.popupsTop = `${y + 9}rpx`;
  124. this.popupsLeft = `${x - 15}rpx`;
  125. break;
  126. case "top-end":
  127. this.popupsTop = `${y + 9}rpx`;
  128. this.popupsLeft = `${x + 15 - width}rpx`;
  129. break;
  130. case "bottom-start":
  131. this.popupsTop = `${y - 18 - height}rpx`;
  132. this.popupsLeft = `${x - 15}rpx`;
  133. break;
  134. case "bottom-end":
  135. this.popupsTop = `${y - 9 - height}rpx`;
  136. this.popupsLeft = `${x + 15 - width}rpx`;
  137. break;
  138. }
  139. resolve();
  140. }
  141. )
  142. .exec();
  143. });
  144. return promise;
  145. },
  146. getPlacement(x, y) {
  147. let width = uni.getSystemInfoSync().windowWidth;
  148. let height = uni.getSystemInfoSync().windowHeight;
  149. if (x > width / 2 && y > height / 2) {
  150. return "bottom-end";
  151. } else if (x < width / 2 && y < height / 2) {
  152. return "top-start";
  153. } else if (x > width / 2 && y < height / 2) {
  154. return "top-end";
  155. } else if (x < width / 2 && y > height / 2) {
  156. return "bottom-start";
  157. } else if (x > width / 2) {
  158. return "top-end";
  159. } else {
  160. return "top-start";
  161. }
  162. },
  163. dynamicGetY(y, gap) {
  164. let height = uni.getSystemInfoSync().windowHeight;
  165. y = y < gap ? gap : y;
  166. y = height - y < gap ? height - gap : y;
  167. return y;
  168. },
  169. dynamicGetX(x, gap) {
  170. let width = uni.getSystemInfoSync().windowWidth;
  171. x = x < gap ? gap : x;
  172. x = width - x < gap ? width - gap : x;
  173. return x;
  174. },
  175. transformRpx(params) {
  176. return (params * uni.getSystemInfoSync().screenWidth) / 375;
  177. },
  178. },
  179. watch: {
  180. value: {
  181. immediate: true,
  182. handler: async function (newVal, oldVal) {
  183. if (newVal) await this.popupsPosition();
  184. this.show = newVal;
  185. },
  186. },
  187. placement: {
  188. immediate: true,
  189. handler(newVal, oldVal) {
  190. this.dynPlace = newVal;
  191. },
  192. },
  193. },
  194. };
  195. </script>
  196. <style lang="scss" scoped>
  197. .title {
  198. margin-left: 20rpx;
  199. }
  200. .shadow {
  201. position: fixed;
  202. top: 0;
  203. right: 0;
  204. bottom: 0;
  205. left: 0;
  206. height: 100%;
  207. z-index: 9999;
  208. transition: background 0.3s ease-in-out;
  209. visibility: hidden;
  210. &.shadow-show {
  211. visibility: visible;
  212. }
  213. }
  214. .popups {
  215. position: absolute;
  216. padding: 20rpx;
  217. border-radius: 5px;
  218. display: flex;
  219. .view {
  220. display: flex;
  221. align-items: center;
  222. padding: 15rpx 10rpx;
  223. font-size: 25rpx;
  224. }
  225. .image {
  226. display: inline-block;
  227. vertical-align: middle;
  228. width: 40rpx;
  229. height: 40rpx;
  230. margin-right: 20rpx;
  231. }
  232. }
  233. .dark {
  234. background-color: #4c4c4c;
  235. color: #fff;
  236. .top-start:after {
  237. content: "";
  238. position: absolute;
  239. top: -18rpx;
  240. left: 10rpx;
  241. border-width: 0 20rpx 20rpx;
  242. border-style: solid;
  243. border-color: transparent transparent #4c4c4c;
  244. }
  245. .top-end:after {
  246. content: "";
  247. position: absolute;
  248. top: -18rpx;
  249. right: 10rpx;
  250. border-width: 0 20rpx 20rpx;
  251. border-style: solid;
  252. border-color: transparent transparent #4c4c4c;
  253. }
  254. .bottom-start:after {
  255. content: "";
  256. position: absolute;
  257. bottom: -18rpx;
  258. left: 10rpx;
  259. border-width: 20rpx 20rpx 0;
  260. border-style: solid;
  261. border-color: #4c4c4c transparent transparent;
  262. }
  263. .bottom-end:after {
  264. content: "";
  265. position: absolute;
  266. bottom: -18rpx;
  267. right: 10rpx;
  268. border-width: 20rpx 20rpx 0;
  269. border-style: solid;
  270. border-color: #4c4c4c transparent transparent;
  271. }
  272. .disabledColor {
  273. color: #c5c8ce;
  274. }
  275. }
  276. .light {
  277. color: #515a6e;
  278. box-shadow: 0upx 0upx 30upx rgba(0, 0, 0, 0.2);
  279. background: #fff;
  280. .top-start:after {
  281. content: "";
  282. position: absolute;
  283. top: -18rpx;
  284. left: 10rpx;
  285. border-width: 0 20rpx 20rpx;
  286. border-style: solid;
  287. border-color: transparent transparent #fff;
  288. }
  289. .top-end:after {
  290. content: "";
  291. position: absolute;
  292. top: -18rpx;
  293. right: 10rpx;
  294. border-width: 0 20rpx 20rpx;
  295. border-style: solid;
  296. border-color: transparent transparent #fff;
  297. }
  298. .bottom-start:after {
  299. content: "";
  300. position: absolute;
  301. bottom: -18rpx;
  302. left: 10rpx;
  303. border-width: 20rpx 20rpx 0;
  304. border-style: solid;
  305. border-color: #fff transparent transparent;
  306. }
  307. .bottom-end:after {
  308. content: "";
  309. position: absolute;
  310. bottom: -18rpx;
  311. right: 10rpx;
  312. border-width: 20rpx 20rpx 0;
  313. border-style: solid;
  314. border-color: #fff transparent transparent;
  315. }
  316. .disabledColor {
  317. color: #c5c8ce;
  318. }
  319. }
  320. .solid-bottom {
  321. border-bottom: 1px solid #f3f5f7;
  322. }
  323. .solid-right {
  324. border-right: 1px solid #ccc;
  325. }
  326. .popups .itemChild:last-child {
  327. border: none;
  328. }
  329. </style>