翼度科技»论坛 编程开发 JavaScript 查看内容

vue2.0+elementui实现一个上门取件时间组件

13

主题

13

帖子

39

积分

新手上路

Rank: 1

积分
39
本文使用vue2.0+elementui 制作一个上门取件时间组件,类似顺丰,样式如下:

大概功能:点击期望上门时间,下面出现一个弹框可以选择时间:
首先我们定义一些需要的数据:
  1.   data() {
  2.         return {
  3.             isDropdown: false,
  4.             dayList: [],
  5.             listArray: [
  6.                 "08:00~09:00",
  7.                 "09:00~10:00",
  8.                 "10:00~11:00",
  9.                 "11:00~12:00",
  10.                 "12:00~13:00",
  11.                 "13:00~14:00",
  12.                 "14:00~15:00",
  13.                 "15:00~16:00",
  14.                 "16:00~17:00",
  15.                 "17:00~18:00",
  16.                 "18:00~19:00",
  17.                 "19:00~19:30",
  18.             ],
  19.             timeToList: {
  20.             },
  21.             timeValue: "今天",
  22.             clickValue: "一小时内",
  23.             clickDay: "今天",
  24.             time: "",
  25.         }
  26.     },
复制代码
接着我们画一个期望上门时间的长框,点击可以出现弹窗,点击外部弹窗消失,这中间我们使用了import Clickoutside from 'element-ui/src/utils/clickoutside' 这一组件,来帮助我们达到这个目的
  1. <template>
  2.     <div class="time-picker" @click="openDown" v-clickoutside="clickoutside">
  3.         <div class="content-first">
  4.             <div class="redSpan">*</div>
  5.             <div>期望上门时间</div>
  6.         </div>
  7.         <div class="content-first">
  8.             <div>
  9.                 {{ time }}
  10.             </div>
  11.             <i class="el-icon-s-order"></i>
  12.         </div>
  13. </template>
复制代码
接下来画一个弹出页面,弹出页面顶部是一个tab组件,这里通过daylist循环获得
  1.    <div class="time">
  2.                 <div v-for="item in dayList" class="item" :class="timeValue == item.lable ? 'active' : ''"
  3.                     @click="dayChange(item)">
  4.                     <div>{{ item.lable }}</div>
  5.                     <div>{{ item.ymd }}</div>
  6.                 </div>
  7.             </div>
复制代码
tab组件中的内容,是下单时间的按钮集合,通过timeToList 这个结构体 ,先获取数组再循环生成
  1.            <div class="timeList">
  2.                 <div v-for="item  in  timeToList[timeValue]" @click="timeChange(item)" class="timeBox"
  3.                     :class="clickDay == item.day && clickValue == item.lable ? 'active' : ''">
  4.                     {{ item.lable }}
  5.                 </div>
  6.             </div>
复制代码
页面写好了我们开始写逻辑代码,先需要一些工具函数获取小时、分钟、年月日,一个用来判定点击了哪个按钮的list(由于是双层结构tab+button集,所以需要两个值来判定),一个获取今天按钮列表的函数:
  1.         getHours() {
  2.             const now = new Date();
  3.             return now.getHours();
  4.         },

  5.         getMinute() {
  6.             const now = new Date();
  7.             return now.getMinutes();
  8.         },

  9.         formatDate(date) {
  10.             const year = date.getFullYear();
  11.             const month = String(date.getMonth() + 1).padStart(2, '0');
  12.             const day = String(date.getDate()).padStart(2, '0');
  13.             return `${year}-${month}-${day}`;
  14.         },
  15.         
  16.         transTime(arr, day) {
  17.             let temp = []
  18.             arr.forEach((item) => {
  19.                 temp.push({
  20.                     lable: item,
  21.                     day: day
  22.                 })
  23.             })
  24.             return temp
  25.         },

  26.         getTodayList(arr) {
  27.             let minute = this.getMinute()
  28.             let hour = this.getHours()
  29.             if (hour < 8)
  30.                 return arr
  31.             if (hour >= 19 && minute > 30)
  32.                 return []
  33.             arr = arr.slice(hour - 7)
  34.             arr = ['一小时内', ...arr]
  35.             return arr
  36.         }
复制代码
然后我们需要先初始化数据
  1.      initial() {
  2.             let minute = this.getMinute()
  3.             let hour = this.getHours()
  4.             if (hour < 8) {
  5.                 this.clickValue = "08:00~09:00"
  6.                 this.clickDay = "今天"
  7.                 return
  8.             }
  9.             if (hour >= 19 && minute > 30) {
  10.                 this.clickValue = "08:00~09:00"
  11.                 this.clickDay = "明天"
  12.                 return
  13.             }
  14.         },
复制代码
然后将时间赋值,这里其实可以用computed,但是我还是习惯自己做这部分操作
  1.         setTime() {
  2.             this.time = this.clickDay + ' ' + this.clickValue
  3.         },
复制代码
接下来我们需要生成tab表单dayList,以及每个tab页面下面的时间选项,用了上面的两个工具函数getTodayList(),transTime()
  1.        getDay() {
  2.             const today = new Date()
  3.             const tomorrow = new Date(today)
  4.             tomorrow.setDate(tomorrow.getDate() + 1)
  5.             const afterTomorrow = new Date(today)
  6.             afterTomorrow.setDate(afterTomorrow.getDate() + 2)

  7.             let dayArray = [this.formatDate(today), this.formatDate(tomorrow), this.formatDate(afterTomorrow)]
  8.             let dayName = ['今天', '明天', '后天']
  9.             this.dayList = dayName.map((item, index) => {
  10.                 return {
  11.                     lable: item,
  12.                     ymd: dayArray[index]
  13.                 }
  14.             })
  15.         },

  16.       getTimeToList() {
  17.             this.dayList.forEach((item) => {
  18.                 let arr = JSON.parse(JSON.stringify(this.listArray))
  19.                 if (item.lable === "今天")
  20.                     arr = this.getTodayList(arr)
  21.                 this.timeToList[item.lable] = this.transTime(arr, item.lable)
  22.             })
  23.         },
复制代码
通过上面的初始化函数,可以生成下拉页面的组件内容,函数顺序如下
  1.     mounted() {
  2.         this.initial()
  3.         this.setTime()
  4.         this.getDay()
  5.         this.getTimeToList()
  6.     },
复制代码
最后我们添加一些点击动作,完整代码
  1.         openDown() {//打开下来框
  2.             this.isDropdown = true
  3.         },

  4.         clickoutside(e) {//关闭下拉框
  5.             if (!e) {
  6.                 this.isDropdown = false
  7.                 this.timeValue = this.clickDay
  8.             }
  9.         },

  10.         dayChange(item) {//切换tab页面
  11.             this.timeValue = item.lable
  12.         },

  13.         timeChange(item) {//选择下单时间
  14.             this.clickValue = item.lable
  15.             this.clickDay = item.day
  16.             this.setTime()
  17.         },
复制代码
贴一下css代码
  1. <style lang="scss" scoped>
  2. .time-picker {
  3.     background-color: #f4f5f7;
  4.     width: 336px;
  5.     height: 32px;
  6.     padding: 0 6px;

  7.     display: flex;
  8.     justify-content: space-between;
  9.     cursor: pointer;

  10.     .content-first {
  11.         display: flex;
  12.         align-items: center;
  13.         gap: 3px;

  14.         .redSpan {
  15.             color: red;
  16.         }
  17.     }

  18.     .dropdown {
  19.         position: absolute;
  20.         top: 32px;
  21.         right: 0px;
  22.         z-index: 99;
  23.         width: 100%;
  24.         height: 220px;
  25.         background-color: #fff;
  26.         box-shadow: 0 8px 12px 0 rgba(0, 0, 0, 0.04);
  27.         border-radius: 10px;
  28.         padding: 6px;

  29.         .time {
  30.             display: flex;

  31.             .item {
  32.                 width: 33%;
  33.                 height: 45px;
  34.                 text-align: center;
  35.                 font-size: 14px;
  36.                 line-height: 18px;
  37.                 border-bottom: 1px solid #cccccc;
  38.             }

  39.             .active {
  40.                 color: red;
  41.                 border-bottom: 1px solid red;
  42.             }
  43.         }

  44.         .timeList {
  45.             padding: 10px;
  46.             display: flex;
  47.             align-items: center;
  48.             flex-wrap: wrap;
  49.             gap: 10px;

  50.             .timeBox {
  51.                 width: 93px;
  52.                 height: 29px;
  53.                 background-color: #f7f8fa;
  54.                 text-align: center;
  55.             }

  56.             .timeBox:hover {
  57.                 color: red;
  58.             }

  59.             .active {
  60.                 color: red;
  61.                 background-color: #ffefef;
  62.             }
  63.         }
  64.     }

  65. }
  66. </style>
复制代码
完整代码已经上传github:https://github.com/majinihao123/vue-Component
总结
到此这篇关于vue2.0+elementui实现一个上门取件时间组件的文章就介绍到这了,更多相关Vue上门取件时间组件内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

来源:https://www.jb51.net/javascript/316233p0i.htm
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

举报 回复 使用道具