wanghexu-timeslot.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. setTime(time){
  165. console.log('time',time);
  166. this.startvalue = time.start;
  167. this.endvalue = time.end;
  168. }
  169. }
  170. };
  171. </script>
  172. <style lang="scss" scoped>
  173. .time_mask {
  174. position: fixed;
  175. bottom: 0;
  176. top: 0;
  177. left: 0;
  178. right: 0;
  179. background-color: $uni-bg-color-mask;
  180. transition-property: opacity;
  181. transition-duration: 0.3s;
  182. opacity: 0;
  183. /* #ifndef APP-NVUE */
  184. z-index: 10076;
  185. /* #endif */
  186. }
  187. .yx_time_slot{
  188. background-color: #FFFFFF;
  189. width: 100%;
  190. height: 750rpx;
  191. position: fixed;
  192. bottom: calc(var(--window-bottom));
  193. left: 0;
  194. right: 0;
  195. z-index: 10077;
  196. transition-property: transform;
  197. transition-duration: 0.3s;
  198. transform: translateY(460px);
  199. .time_top_box{
  200. width: 100%;
  201. height: 80rpx;
  202. display: flex;
  203. align-items: center;
  204. justify-content: space-between;
  205. .time_close,.time_comfirm{
  206. width: 100rpx;
  207. color: #999999;
  208. font-size: 28rpx;
  209. font-weight: 400;
  210. text-align: center;
  211. }
  212. .time_comfirm{
  213. color: #4360F7;
  214. }
  215. .time_text{
  216. flex: 1;
  217. font-size: 30rpx;
  218. font-weight: 800;
  219. text-align: center;
  220. }
  221. }
  222. }
  223. .uni-timer-mask-show{
  224. opacity: 1;
  225. }
  226. /* 从下往上弹窗动画 */
  227. .fadelogIn1 {
  228. // -webkit-animation: fadelogIn 0.5s;
  229. // animation: fadelogIn 0.5s;
  230. transform: translateY(0);
  231. }
  232. .typelist{
  233. width: 100%;
  234. height: 70rpx;
  235. display: flex;
  236. align-items: center;
  237. .typeobj{
  238. width: 158rpx;
  239. display: flex;
  240. flex-direction: column;
  241. justify-content: center;
  242. align-items: center;
  243. .text{
  244. height: 65rpx;
  245. line-height: 65rpx;
  246. font-size: 28rpx;
  247. color: #333333;
  248. }
  249. .line{
  250. width: 1rpx;
  251. height: 5rpx;
  252. }
  253. }
  254. .typeobj_hover{
  255. .text{
  256. font-weight: 600;
  257. }
  258. .line{
  259. width: 88rpx;
  260. transition: width .5s;
  261. background-color: #4360F7;
  262. }
  263. }
  264. }
  265. .yx_timer_sel{
  266. width: 100%;
  267. margin-top: 38rpx;
  268. .sel_swiper{
  269. // width: 80%;
  270. // margin: 0 auto;
  271. height: 500rpx;
  272. }
  273. .sel_swiper-item{
  274. height: 500rpx;
  275. .item {
  276. height: 50px;
  277. display: flex;
  278. align-items: center;
  279. justify-content: center;
  280. text-align: center;
  281. }
  282. }
  283. }
  284. </style>