wanghexu-timeslot.vue 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <template>
  2. <view>
  3. <view v-if="isShow" class="time_mask" :class="{'uni-timer-mask-show':timeMaskShow}"></view>
  4. <view v-if="isShow" class="yx_time_slot" :class="{'fadelogIn1':timeMaskShow}">
  5. <view class="time_top_box">
  6. <view class="time_close" @click="close">取消</view>
  7. <view class="time_text">{{title}}</view>
  8. <view class="time_comfirm" @click="confirm">确认</view>
  9. </view>
  10. <view class="typelist">
  11. <view class="typeobj" :class="{'typeobj_hover':typeIndex==index}" v-for="(item,index) in typeList" :key="index" @click="handleType(index)">
  12. <view class="text">{{item}}</view>
  13. <view class="line"></view>
  14. </view>
  15. </view>
  16. <!-- 时间选择 -->
  17. <view class="yx_timer_sel">
  18. <swiper class="sel_swiper" :current="typeIndex">
  19. <swiper-item>
  20. <view>
  21. <picker-view :value="startvalue" :indicator-style="indicatorStyle" @change="bindstartChange" class="sel_swiper-item">
  22. <picker-view-column>
  23. <view class="item" v-for="(item,index) in timeHour" :key="index">{{item}}时</view>
  24. </picker-view-column>
  25. <picker-view-column>
  26. <view class="item" v-for="(item,index) in timeMin" :key="index">{{item}}分</view>
  27. </picker-view-column>
  28. </picker-view>
  29. </view>
  30. </swiper-item>
  31. <swiper-item>
  32. <view>
  33. <picker-view :value="endvalue" :indicator-style="indicatorStyle" @change="bindendChange" class="sel_swiper-item">
  34. <picker-view-column>
  35. <view class="item" v-for="(item,index) in timeHour" :key="index">{{item}}时</view>
  36. </picker-view-column>
  37. <picker-view-column>
  38. <view class="item" v-for="(item,index) in timeMin" :key="index">{{item}}分</view>
  39. </picker-view-column>
  40. </picker-view>
  41. </view>
  42. </swiper-item>
  43. </swiper>
  44. </view>
  45. </view>
  46. </view>
  47. </template>
  48. <script>
  49. export default {
  50. name:"time-slot",
  51. props: {
  52. title: {
  53. type: String,
  54. default: () => {
  55. return "选择时间段";
  56. }
  57. },
  58. },
  59. data() {
  60. return {
  61. timeMaskShow:false,//遮罩
  62. isShow:false,//显示
  63. typeList:["开始时间","结束时间"],
  64. typeIndex:0,//下标
  65. startvalue:[14,0],//默认下标,14时 00分
  66. endvalue:[15,0],//默认下标,15时 00分
  67. indicatorStyle: 'height: 50px;',
  68. // timeList:[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23],
  69. timeHour:[],
  70. timeMin:[],//分钟可选项
  71. };
  72. },
  73. mounted() {
  74. //获取时分
  75. let timeHour = []
  76. let timeMin = []
  77. for (let i = 0; i < 24; i++) {
  78. timeHour.push(this.formatNumber(i))
  79. }
  80. for (let i = 0; i < 60; i++) {
  81. timeMin.push(this.formatNumber(i))
  82. }
  83. this.timeHour = timeHour
  84. this.timeMin = timeMin
  85. },
  86. methods: {
  87. //看看是不是有两位数
  88. formatNumber(n) {
  89. n = n.toString()
  90. return n[1] ? n : '0' + n
  91. },
  92. //开始选择
  93. bindstartChange(e){
  94. console.log(e)
  95. this.startvalue = e.detail.value
  96. },
  97. //结束选择
  98. bindendChange(e){
  99. console.log(e)
  100. this.endvalue = e.detail.value
  101. },
  102. /**
  103. * 关闭弹窗
  104. */
  105. close() {
  106. this.timeMaskShow = false
  107. this.$nextTick(() => {
  108. setTimeout(() => {
  109. this.isShow = false
  110. this.$emit('close')
  111. }, 300)
  112. })
  113. },
  114. /**
  115. * 确认按钮
  116. */
  117. confirm() {
  118. if(this.typeIndex==0){
  119. this.endvalue = [this.startvalue[0]+1,0]
  120. this.typeIndex = 1
  121. }else{
  122. if(this.startvalue[0]<this.endvalue[0] || (this.startvalue[0]==this.endvalue[0]&&this.startvalue[1]<this.endvalue[1])){
  123. var obj = {
  124. start:{
  125. hour:this.timeHour[this.startvalue[0]],
  126. min:this.timeMin[this.startvalue[1]]
  127. },
  128. end:{
  129. hour:this.timeHour[this.endvalue[0]],
  130. min:this.timeMin[this.endvalue[1]]
  131. }
  132. }
  133. this.$emit('confirm',obj)
  134. this.close()
  135. }else{
  136. uni.showToast({
  137. icon:"error",
  138. title:"结束时间要大于开始时间"
  139. })
  140. }
  141. }
  142. },
  143. /**
  144. * 打开日历弹窗
  145. */
  146. open() {
  147. this.typeIndex = 0
  148. this.isShow = true
  149. this.$nextTick(() => {
  150. setTimeout(() => {
  151. this.timeMaskShow = true
  152. }, 50)
  153. })
  154. },
  155. //栏目选择
  156. handleType(index){
  157. if(index!=this.typeIndex){
  158. if(index==1){
  159. this.endvalue = [this.startvalue[0]+1,0]
  160. }
  161. this.typeIndex = index
  162. }
  163. }
  164. }
  165. };
  166. </script>
  167. <style lang="scss" scoped>
  168. .time_mask {
  169. position: fixed;
  170. bottom: 0;
  171. top: 0;
  172. left: 0;
  173. right: 0;
  174. background-color: $uni-bg-color-mask;
  175. transition-property: opacity;
  176. transition-duration: 0.3s;
  177. opacity: 0;
  178. /* #ifndef APP-NVUE */
  179. z-index: 99;
  180. /* #endif */
  181. }
  182. .yx_time_slot{
  183. background-color: #FFFFFF;
  184. width: 100%;
  185. height: 750rpx;
  186. position: fixed;
  187. bottom: calc(var(--window-bottom));
  188. left: 0;
  189. right: 0;
  190. z-index: 999;
  191. transition-property: transform;
  192. transition-duration: 0.3s;
  193. transform: translateY(460px);
  194. .time_top_box{
  195. width: 100%;
  196. height: 80rpx;
  197. display: flex;
  198. align-items: center;
  199. justify-content: space-between;
  200. .time_close,.time_comfirm{
  201. width: 100rpx;
  202. color: #999999;
  203. font-size: 28rpx;
  204. font-weight: 400;
  205. text-align: center;
  206. }
  207. .time_comfirm{
  208. color: #4360F7;
  209. }
  210. .time_text{
  211. flex: 1;
  212. font-size: 30rpx;
  213. font-weight: 800;
  214. text-align: center;
  215. }
  216. }
  217. }
  218. .uni-timer-mask-show{
  219. opacity: 1;
  220. }
  221. /* 从下往上弹窗动画 */
  222. .fadelogIn1 {
  223. // -webkit-animation: fadelogIn 0.5s;
  224. // animation: fadelogIn 0.5s;
  225. transform: translateY(0);
  226. }
  227. .typelist{
  228. width: 100%;
  229. height: 70rpx;
  230. display: flex;
  231. align-items: center;
  232. .typeobj{
  233. width: 158rpx;
  234. display: flex;
  235. flex-direction: column;
  236. justify-content: center;
  237. align-items: center;
  238. .text{
  239. height: 65rpx;
  240. line-height: 65rpx;
  241. font-size: 28rpx;
  242. color: #333333;
  243. }
  244. .line{
  245. width: 1rpx;
  246. height: 5rpx;
  247. }
  248. }
  249. .typeobj_hover{
  250. .text{
  251. font-weight: 600;
  252. }
  253. .line{
  254. width: 88rpx;
  255. transition: width .5s;
  256. background-color: #4360F7;
  257. }
  258. }
  259. }
  260. .yx_timer_sel{
  261. width: 100%;
  262. margin-top: 38rpx;
  263. .sel_swiper{
  264. // width: 80%;
  265. // margin: 0 auto;
  266. height: 500rpx;
  267. }
  268. .sel_swiper-item{
  269. height: 500rpx;
  270. .item {
  271. height: 50px;
  272. display: flex;
  273. align-items: center;
  274. justify-content: center;
  275. text-align: center;
  276. }
  277. }
  278. }
  279. </style>