select-timer.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <template>
  2. <view class="actions" v-if="visible">
  3. <view class="mask" @click="visibleChange()"></view>
  4. <view class="tabBar">
  5. <view class="left">
  6. <text @click="visibleChange()">取消</text>
  7. </view>
  8. <view class="right">
  9. <text @click="visibleChange()">确认</text>
  10. </view>
  11. </view>
  12. <picker-view :indicator-style="indicatorStyle" :value="value" @change="bindChange">
  13. <picker-view-column>
  14. <view class="item" v-for="(item,index) in years" :key="index">{{item}}年</view>
  15. </picker-view-column>
  16. <picker-view-column>
  17. <view class="item" v-for="(item,index) in months" :key="index">{{item}}月</view>
  18. </picker-view-column>
  19. <picker-view-column>
  20. <view class="item" v-for="(item,index) in days" :key="index">{{item}}日</view>
  21. </picker-view-column>
  22. <picker-view-column>
  23. <view class="item" v-for="(item,index) in hours" :key="index">{{item}}时</view>
  24. </picker-view-column>
  25. <picker-view-column>
  26. <view class="item" v-for="(item,index) in minutes" :key="index">{{item}}分</view>
  27. </picker-view-column>
  28. <picker-view-column>
  29. <view class="item" v-for="(item,index) in seconds" :key="index">{{item}}秒</view>
  30. </picker-view-column>
  31. </picker-view>
  32. </view>
  33. </template>
  34. <script>
  35. export default {
  36. data(){
  37. return {
  38. value: [0,0,0,0,0,0],
  39. years: [],
  40. months: [],
  41. days: [],
  42. hours: [],
  43. minutes: [],
  44. seconds: [],
  45. indicatorStyle: `height: ${Math.round(uni.getSystemInfoSync().screenWidth/(750/100))}px;`
  46. }
  47. },
  48. props:{
  49. visible: {
  50. type:Boolean,
  51. default: false
  52. }
  53. },
  54. created() {
  55. //用最古老的方式做最有效的事
  56. //记录一下当前时间
  57. let data = new Date();
  58. // 初始化选择器中的年份
  59. for(let i=1970;i<=2099;i++){
  60. this.years.push(i)
  61. }
  62. // 初始化选择器中的月份
  63. for(let j=1;j<=12;j++){
  64. this.months.push(j)
  65. }
  66. // 初始化选择器中的天数
  67. this.init(data.getFullYear(),data.getMonth())
  68. // 初始化选择器选中的时间
  69. //定义到当前时间
  70. this.value[0] = data.getFullYear() - 1970;
  71. this.value[1] = data.getMonth();
  72. this.value[2] = data.getDate()-1;
  73. this.value[3] = data.getHours();
  74. this.value[4] = data.getMinutes();
  75. this.value[5] = data.getSeconds();
  76. this.$emit('changeTime',this.value)
  77. },
  78. methods:{
  79. visibleChange(){
  80. this.$emit('timeCofirm',this.visible)
  81. },
  82. bindChange(e){
  83. this.init(
  84. this.years[e.detail.value[0]],
  85. this.months[e.detail.value[1]] - 1
  86. )
  87. this.value = e.detail.value;
  88. this.$emit('changeTime',this.value)
  89. },
  90. // 初始化选择器中的天数
  91. init(inYear,inMonth,inDay,inHours,inMinutes,inSeconds){
  92. this.days = [];
  93. this.hours = [];
  94. this.minutes = [];
  95. this.seconds = [];
  96. // 当前月数为2月
  97. if(inMonth == 1){
  98. if(inYear %4 ==0){
  99. // 为闰年为29天
  100. for(let s=1;s<=29;s++){
  101. this.days.push(s)
  102. }
  103. }else {
  104. // 否则为28天
  105. for(let s=1;s<=28;s++){
  106. this.days.push(s)
  107. }
  108. }
  109. }else if(
  110. inMonth == 0 ||
  111. inMonth == 2 ||
  112. inMonth == 4 ||
  113. inMonth == 6 ||
  114. inMonth == 7 ||
  115. inMonth == 9 ||
  116. inMonth == 11
  117. ){
  118. // 如果为1,3,5,7,8,10,12月份为31天
  119. for(let s=1;s<=31;s++){
  120. this.days.push(s)
  121. }
  122. }else {
  123. // 其他月份为30天
  124. for(let s=1;s<=30;s++){
  125. this.days.push(s)
  126. }
  127. }
  128. }
  129. }
  130. }
  131. </script>
  132. <style lang="scss">
  133. .actions{
  134. width: 100%;
  135. position: absolute;
  136. background-color: rgba(0,0,0,0.1);
  137. bottom: 0;
  138. /* #ifdef H5 */
  139. min-height: calc(100vh - 44px);
  140. /* #endif */
  141. /* #ifndef H5 */
  142. min-height: 100vh;
  143. /* #endif */
  144. z-index: 10076;
  145. }
  146. .mask{
  147. width: 100%;
  148. /* #ifdef H5 */
  149. height: calc(100vh - 640rpx - 44px);
  150. /* #endif */
  151. /* #ifndef H5 */
  152. height: calc(100vh - 640rpx);
  153. /* #endif */
  154. }
  155. .tabBar{
  156. z-index: 10076;
  157. width: 100%;
  158. padding: 2% 5%;
  159. background-color: #FFFFFF;
  160. display: flex;
  161. height: 80rpx;
  162. line-height: 40rpx;
  163. view{
  164. width: 50%;
  165. }
  166. .left{
  167. color: #14ADF9;
  168. }
  169. .right{
  170. text-align: right;
  171. color: #09BB07;
  172. }
  173. }
  174. picker-view {
  175. background-color: #FFFFFF;
  176. width: 100%;
  177. height: 600rpx;
  178. picker-view-column{
  179. width: 100%;
  180. text-align: center;
  181. }
  182. }
  183. </style>