Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write the component code of selecting date in infinite sliding cycle with Vue

2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "how to write the component code for Vue to achieve infinite sliding week selection date". In daily operation, I believe that many people have doubts about how to write the component code for Vue to achieve infinite sliding week selection date. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for everyone to answer the doubt that "Vue realizes infinite sliding week selection date". Next, please follow the editor to study!

Train of thought

According to the date passed by the user (no default today), get the corresponding date of the previous week, the current week and the next week and put it in the array dates

Let vm = this this.dates.push ({date: moment (vm.defaultDate) .subtract (7,'d'). Format ('YYYY-MM-DD'),}, {date: vm.defaultDate,}, {date: moment (vm.defaultDate) .add (7,' d'). Format ('YYYY-MM-DD'),})

Generate the corresponding date for each week according to datas

GetDaies (date) {let vm = this, arr = [] let weekOfDate = Number (moment (date) .format ('E')) let weeks = ['Day', 'one', 'two', 'three', 'four', 'five', 'six'] let today = moment () let defaultDay = moment (vm.defaultDate) for (var I = 0; I

< 7; i++) { let _theDate = moment(date).subtract(weekOfDate - i, 'd') arr.push({ date: _theDate.format('YYYY-MM-DD'), week: weeks[i], isToday: _theDate.format('YYYY-MM-DD') === today.format('YYYY-MM-DD'), isDay: _theDate.format('E') === defaultDay.format('E') }) } return arr } 生成每一个滑动单元的style getTransform (index) { let vm = this let style = {} if (index === vm.activeIndex) { style['transform'] = 'translateX('+ vm.distan.x +'px)' } if (index < vm.activeIndex) { style['transform'] = 'translateX(-100%)' } if (index >

Vm.activeIndex) {style ['transform'] =' translateX'} style ['transition'] = vm.isAnimation? 'transform 0.5s ease-out':' transform 0s ease-out' return style}

Then it's time to handle the touchstart touchend touchmove event. Instead of posting the code here, let's talk about the logic:

Ouchstart records the position of the sliding start point

Touchmove gets sliding distance assignment to vm.distan.x to get current week transform in real time

Touchend changes the value of activeIndex, of course, it is not enough to change the value of activeIndex. To achieve infinite sliding, you need to operate dates. If you left-slide to delete the first element of dates and push the corresponding date next week into dates, if you right-slide to delete the last element in front of the grid-connected array, the corresponding date of the week before unshift.

At this point, the study on "how to write the component code for selecting a date in an infinite sliding cycle with Vue" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report