chunLei-popups.vue 7.0 KB

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