经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JS/JS库/框架 » Vue.js » 查看文章
vue日历组件的封装方法
来源:jb51  时间:2022/3/8 12:54:51  对本文有异议

本文实例为大家分享了vue日历组件的封装代码,供大家参考,具体内容如下

图示

封装的组件的代码如下

  1. <template>
  2. ? <div class="calendar">
  3. ? ? <!-- 选择日历的弹出层 -->
  4. ? ? <div class="model_mask" v-show="showtimemask" @click="showmask1()">
  5. ? ? </div>
  6. ? ? <div class="bouncedBox" v-show="showtimemask">
  7. ? ? ? <div class="mobile-top">
  8. ? ? ? ? <div class="sel-time">
  9. ? ? ? ? ? <p>开始时间</p>
  10. ? ? ? ? ? <p class="start-date">{{starttime.substring(0,4)+'-'+starttime.substring(4,6)+'-'+starttime.substring(6,8)}}
  11. ? ? ? ? ? </p>
  12. ? ? ? ? </div>
  13. ? ? ? ? <div class="unsel-time">
  14. ? ? ? ? ? <p>结束时间</p>
  15. ? ? ? ? ? <p class="end-date">
  16. ? ? ? ? ? ? {{endtime==''?'请选择结束日期':endtime.substring(0,4)+'-'+endtime.substring(4,6)+'-'+endtime.substring(6,8)}}</p>
  17. ? ? ? ? </div>
  18. ? ? ? </div>
  19. ?
  20. ? ? ? <div class="title">
  21. ? ? ? ? <div class="btn" @click.stop="last()" :class="(month<=nowmonth)&&(Year<=nowYear)?'noclick':'' ">上一月</div>
  22. ? ? ? ? <div class="text">{{Year}}年{{month}}月</div>
  23. ? ? ? ? <div class="btn" @click.stop="next()">下一月</div>
  24. ? ? ? </div>
  25. ?
  26. ? ? ? <div class="head">
  27. ? ? ? ? <div class="days" v-for="(item,index) in ['星期日','星期一','星期二','星期三','星期四','星期五','星期六']" :key="index">
  28. ? ? ? ? ? {{item}}
  29. ? ? ? ? </div>
  30. ? ? ? </div>
  31. ?
  32. ? ? ? <div class="wrap">
  33. ? ? ? ? <div class="span" v-for="(item,index) in calendarList" :key="index" @click.stop="click(item.count)" :class="item==''?'kong'
  34. ? ? ? :item.count<nowtime?'noclick'
  35. ? ? ? :(item.count>=starttime&&item.count<=endtime)||item.count==starttime?'active':''">
  36. ? ? ? ? ? {{item.value}}
  37. ? ? ? ? </div>
  38. ? ? ? </div>
  39. ?
  40. ? ? ? <div class="bottombtn">
  41. ? ? ? ? <button class="cancle-btn" @click.stop='cancle()'>取消</button>
  42. ? ? ? ? <button class="sure-btn" @click.stop='firm()'>确定</button>
  43. ? ? ? </div>
  44. ?
  45. ? ? </div>
  46. ? </div>
  47. </template>
  48. ?
  49. <script>
  50. ? export default {
  51. ? ? name: "Calendar",
  52. ? ? data() {
  53. ? ? ? return {
  54. ? ? ? ? showtimemask:false,
  55. ? ? ? ? Puton_time: '', //投放日期 ?默认今日 展示
  56. ? ? ? ? Puton_Start:'', ?//为了保存投放开始结束的日期 ?用来点击取消按钮时初始化选中的值
  57. ? ? ? ? Puton_End:'',
  58. ? ? ? ? nowtime: '', //当前日期的时间-----20190203格式 ?用于比较
  59. ? ? ? ? clickitem: '', //保存每次点击的时间-----20190203格式 ?用于比较
  60. ? ? ? ? clickcount: 0, //点击次数-------判断开始时间还是结束时间
  61. ? ? ? ? starttime: '', //开始时间 ?数字 ? 默认当天日期
  62. ? ? ? ? endtime: '', //结束时间 ?数字 ? 默认当天日期
  63. ? ? ? ? Year: new Date().getFullYear(), //日历上的年份 ? ----动态改变的
  64. ? ? ? ? month: new Date().getMonth() + 1, //日历上的月份 ---- ?动态改变的
  65. ? ? ? ? Day: new Date().getDate(), //日历上的天份 ? ? ? ? ----- 动态改变的
  66. ?
  67. ? ? ? ? nowYear: new Date().getFullYear(),
  68. ? ? ? ? nowmonth: new Date().getMonth() + 1,
  69. ? ? ? ? nowDay: new Date().getDate(),
  70. ? ? ? ? calendarList: [],
  71. ? ? ? };
  72. ? ? },
  73. ?
  74. ? ? created() {
  75. ? ? ? //关于日历的操作开始
  76. ? ? ? this.Draw(this.nowYear, this.nowmonth);
  77. ?
  78. ? ? ? let time_month = this.nowmonth; //现在的月份
  79. ? ? ? let time_day = this.nowDay; //现在的天数
  80. ? ? ? if (this.nowmonth < 10) {
  81. ? ? ? ? time_month = 0 + '' + this.nowmonth;
  82. ? ? ? }
  83. ? ? ? if (this.nowDay < 10) {
  84. ? ? ? ? time_day = 0 + '' + this.nowDay;
  85. ? ? ? }
  86. ?
  87. ? ? ? this.nowtime = this.nowYear + '' + time_month + '' + time_day;
  88. ? ? ? this.starttime = this.nowtime;
  89. ? ? ? this.endtime = this.nowtime;
  90. ?
  91. ? ? ? this.Puton_time = this.starttime.substring(0, 4) + '-' + this.starttime.substring(4, 6) + '-' + this.starttime
  92. ? ? ? ? .substring(6, 8) + '至今';
  93. ?
  94. ? ? ? ? this.Puton_Start = this.nowtime,
  95. ? ? ? ? this.Puton_End = this.nowtime,
  96. ?
  97. ?
  98. ? ? ? ? this.$emit('str',this.Puton_time)
  99. ?
  100. ? ? ? //关于日历的操作结束
  101. ? ? },
  102. ? ? mounted() {
  103. ?
  104. ? ? },
  105. ? ? methods: {
  106. ? ? ? showmask1() {
  107. ? ? ? ? if (this.showtimemask == true) {
  108. ? ? ? ? ? // this.showtimemask=false; ? //隐藏弹框
  109. ? ? ? ? ? this.cancle();
  110. ? ? ? ? } else {
  111. ? ? ? ? ? this.showtimemask = true; ?//显示弹框
  112. ? ? ? ? }
  113. ? ? ? },
  114. ?
  115. ? ? ? Draw: function (Year, Month) {
  116. ? ? ? ? //日期列表
  117. ? ? ? ? var calendar = [];
  118. ?
  119. ? ? ? ? //用当月第一天在一周中的日期值作为当月离第一天的天数(获取当月第一天是周几)
  120. ? ? ? ? for (var i = 1, firstDay = new Date(Year, Month - 1, 1).getDay(); i <= firstDay; i++) {
  121. ? ? ? ? ? calendar.push("");
  122. ? ? ? ? }
  123. ?
  124. ? ? ? ? //用当月最后一天在一个月中的日期值作为当月的天数
  125. ? ? ? ? for (var i = 1, monthDay = new Date(Year, Month, 0).getDate(); i <= monthDay; i++) {
  126. ?
  127. ? ? ? ? ? let time_month = Month;
  128. ? ? ? ? ? let time_day = i;
  129. ? ? ? ? ? if (Month < 10) {
  130. ? ? ? ? ? ? time_month = 0 + '' + Month;
  131. ? ? ? ? ? }
  132. ? ? ? ? ? if (i < 10) {
  133. ? ? ? ? ? ? time_day = 0 + '' + i;
  134. ? ? ? ? ? }
  135. ?
  136. ? ? ? ? ? calendar.push({
  137. ? ? ? ? ? ? value: i,
  138. ? ? ? ? ? ? count: Year + '' + time_month + '' + time_day
  139. ? ? ? ? ? })
  140. ? ? ? ? }
  141. ? ? ? ? this.calendarList = calendar;
  142. ? ? ? ? console.log(calendar)
  143. ? ? ? },
  144. ?
  145. ? ? ? last() {
  146. ? ? ? ? this.month--;
  147. ? ? ? ? if (this.month == 0) {
  148. ? ? ? ? ? this.month = 12;
  149. ? ? ? ? ? this.Year--;
  150. ? ? ? ? }
  151. ?
  152. ? ? ? ? this.Draw(this.Year, this.month);
  153. ? ? ? },
  154. ?
  155. ? ? ? next() {
  156. ? ? ? ? this.month++;
  157. ? ? ? ? if (this.month == 13) {
  158. ? ? ? ? ? this.month = 1;
  159. ? ? ? ? ? this.Year++;
  160. ? ? ? ? }
  161. ?
  162. ? ? ? ? this.Draw(this.Year, this.month);
  163. ? ? ? },
  164. ?
  165. ? ? ? click(item) {
  166. ? ? ? ? this.clickcount++;
  167. ? ? ? ? this.clickitem = item;
  168. ? ? ? ? //开始日期
  169. ? ? ? ? if (this.clickcount % 2 == 1) {
  170. ? ? ? ? ? this.starttime = this.clickitem;
  171. ? ? ? ? ? this.endtime = ''
  172. ? ? ? ? } else {
  173. ? ? ? ? ? this.endtime = this.clickitem;
  174. ? ? ? ? ? if (this.starttime > this.endtime) {
  175. ? ? ? ? ? ? this.endtime = this.starttime;
  176. ? ? ? ? ? ? this.starttime = this.clickitem;
  177. ? ? ? ? ? }
  178. ? ? ? ? }
  179. ? ? ? },
  180. ?
  181. ? ? ? firm() {
  182. ? ? ? ? this.showtimemask = false;
  183. ? ? ? ? //当选择的开始时间与结束时间相同时 ? 显示为2019-07-19当天
  184. ? ? ? ? if (this.starttime == this.endtime) {
  185. ? ? ? ? ? this.Puton_Start = this.starttime,
  186. ? ? ? ? ? this.Puton_End = this.endtime,
  187. ?
  188. ? ? ? ? ? this.Puton_time = this.starttime.substring(0, 4) + '-' + this.starttime.substring(4, 6) + '-' + this.starttime
  189. ? ? ? ? ? ? .substring(6, 8) + '当天';
  190. ?
  191. ? ? ? ? ? ? this.$emit('str',this.Puton_time);
  192. ?
  193. ? ? ? ? ? ? //否则显示xxx 至 ? xxx
  194. ? ? ? ? } else {
  195. ?
  196. ? ? ? ? ? this.Puton_Start = this.starttime,
  197. ? ? ? ? ? this.Puton_End = this.endtime,
  198. ? ? ? ? ? this.Puton_time =
  199. ? ? ? ? ? ? this.starttime.substring(0, 4) + '-' + this.starttime.substring(4, 6) + '-' + this.starttime.substring(6,
  200. ? ? ? ? ? ? 8) +
  201. ? ? ? ? ? ? '至' + this.endtime.substring(0, 4) + '-' + this.endtime.substring(4, 6) + '-' + this.endtime.substring(6, 8);
  202. ?
  203. ? ? ? ? ? ? ?
  204. ?
  205. ? ? ? ? ? ? this.$emit('str',this.Puton_time)
  206. ? ? ? ? }
  207. ?
  208. ? ? ? },
  209. ? ? ? // 取消按钮
  210. ? ? ? cancle() {
  211. ? ? ? ? this.showtimemask = false;?
  212. ? ? ? ? //当按取消按钮时 ? 弹框中选中的区域等于上一次选中的区域
  213. ? ? ? ? this.starttime = this.Puton_Start;
  214. ? ? ? ? this.endtime = this.Puton_End;
  215. ? ? ? ? // this.Puton_time = this.starttime.substring(0, 4) + '-' + this.starttime.substring(4, 6) + '-' + this.starttime
  216. ? ? ? ? // ? .substring(6, 8) + '至今';
  217. ?
  218. ? ? ? ? // ? this.$emit('str',this.Puton_time)
  219. ? ? ? }
  220. ? ? }
  221. ? };
  222. ?
  223. </script>
  224. ?
  225. <style scoped lang="scss">
  226. ? @import "../common/common.css";
  227. ?
  228. ? // 日历的样式
  229. ? .model_mask {
  230. ? ? position: fixed;
  231. ? ? top: 0;
  232. ? ? bottom: 0;
  233. ? ? left: 0;
  234. ? ? right: 0;
  235. ? ? background: rgba($color: #000000, $alpha: 0.5);
  236. ? }
  237. ?
  238. ? .bouncedBox {
  239. ? ? position: fixed;
  240. ? ? background: #fff;
  241. ? ? bottom: 0;
  242. ? ? left: 0;
  243. ? ? right: 0;
  244. ?
  245. ? ? //开始结束日期的显示
  246. ? ? .mobile-top {
  247. ? ? ? display: flex;
  248. ? ? ? flex-wrap: nowrap;
  249. ? ? ? background: #fff;
  250. ? ? ? padding: 0.1rem 0;
  251. ?
  252. ? ? ? .sel-time {
  253. ? ? ? ? text-align: center;
  254. ? ? ? ? width: 50%;
  255. ?
  256. ? ? ? ? // border-bottom: solid 2px #2a81e8;
  257. ? ? ? ? .start-date {
  258. ? ? ? ? ? color: #b1b1b1;
  259. ? ? ? ? ? margin-top: 0.05rem;
  260. ? ? ? ? }
  261. ? ? ? }
  262. ?
  263. ? ? ? .unsel-time {
  264. ? ? ? ? text-align: center;
  265. ? ? ? ? width: 50%;
  266. ?
  267. ? ? ? ? .end-date {
  268. ? ? ? ? ? color: #b1b1b1;
  269. ? ? ? ? ? margin-top: 0.05rem;
  270. ? ? ? ? }
  271. ? ? ? }
  272. ? ? }
  273. ?
  274. ? ? // 左右选择月份 ?显示当前年月
  275. ? ? .title {
  276. ? ? ? width: 100%;
  277. ? ? ? height: 40px;
  278. ? ? ? background-color: #60a7e8;
  279. ? ? ? display: flex;
  280. ? ? ? flex-wrap: nowrap;
  281. ? ? ? text-align: center;
  282. ? ? ? color: #fff;
  283. ? ? ? font-weight: bold;
  284. ? ? ? line-height: 40px;
  285. ?
  286. ? ? ? .btn {
  287. ? ? ? ? width: 1.2rem;
  288. ?
  289. ? ? ? ? &.noclick {
  290. ? ? ? ? ? pointer-events: none;
  291. ? ? ? ? ? background: #ccc;
  292. ? ? ? ? }
  293. ? ? ? }
  294. ?
  295. ? ? ? .text {
  296. ? ? ? ? flex: 1;
  297. ? ? ? }
  298. ? ? }
  299. ?
  300. ? ? //表头 ?周1到周天的显示
  301. ? ? .head {
  302. ? ? ? display: flex;
  303. ? ? ? flex-wrap: nowrap;
  304. ? ? ? text-align: center;
  305. ? ? ? height: 40px;
  306. ? ? ? line-height: 40px;
  307. ?
  308. ? ? ? .days {
  309. ? ? ? ? flex: 1;
  310. ? ? ? }
  311. ? ? }
  312. ?
  313. ? ? //日历表区域
  314. ? ? .wrap {
  315. ? ? ? width: 7.5rem;
  316. ? ? ? height: auto;
  317. ? ? ? overflow: hidden;
  318. ? ? ? padding-bottom: 1rem;
  319. ?
  320. ? ? ? .span {
  321. ? ? ? ? width: 1.07142rem;
  322. ? ? ? ? height: 0.6rem;
  323. ? ? ? ? background: #fff;
  324. ? ? ? ? color: #337ab7;
  325. ? ? ? ? float: left;
  326. ? ? ? ? text-align: center;
  327. ? ? ? ? line-height: 0.6rem;
  328. ?
  329. ? ? ? ? &.active {
  330. ? ? ? ? ? background: #037ef5;
  331. ? ? ? ? ? color: #fff;
  332. ? ? ? ? }
  333. ?
  334. ? ? ? ? &.noclick {
  335. ? ? ? ? ? pointer-events: none;
  336. ? ? ? ? ? background: #ccc;
  337. ? ? ? ? }
  338. ?
  339. ? ? ? ? &.kong {
  340. ? ? ? ? ? background: #fff;
  341. ? ? ? ? ? pointer-events: none;
  342. ? ? ? ? }
  343. ? ? ? }
  344. ? ? }
  345. ?
  346. ? ? //底部按钮区域
  347. ? ? .bottombtn {
  348. ? ? ? height: 40px;
  349. ? ? ? width: 100%;
  350. ? ? ? display: flex;
  351. ? ? ? flex-wrap: nowrap;
  352. ?
  353. ? ? ? button {
  354. ? ? ? ? flex: 1;
  355. ? ? ? }
  356. ?
  357. ? ? ? .sure-btn {
  358. ? ? ? ? background: #037ef5;
  359. ?
  360. ? ? ? ? color: #fff;
  361. ? ? ? }
  362. ? ? }
  363. ?
  364. ? }
  365. ?
  366. </style>

使用方法

main,js引入  全局注册组件

  1. import Calendar from './components/fz_zujian/Calendar.vue' ? ?//日历组件
  2. Vue.component('Calendar',Calendar)

页面使用

  1. <div class="" @click="showmodel()">{{str}}</div>
  2. ?
  3. <Calendar ref="chi1" v-on:str="getChild"></Calendar>
  4. ?
  5. ?data() {
  6. ? ? ? return {
  7. ? ? ? ? str: '',
  8. ? ? ? }
  9. ? }
  10. ?
  11. ?showmodel(){
  12. ? ? ? ? this.$refs.chi1.showmask1()
  13. ? ? ? },
  14. ?
  15. ? ? ? getChild(val) {
  16. ? ? ? ? this.str = val
  17. ? ? ? },

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号