This article mainly introduces the Vue implementation of ios native picker effect and the analysis of implementation ideas. This article introduces you in very detail and has reference value. Friends in need can refer to it.
was the first to implement one before There is a similar time selection plug-in, but its scope of application is too narrow. I simply want to release this implementation recently and rewrite a highly reusable vue component.
Support Android 4.0 or above, Safari 7 or above
Effect preview
gitHub
The main wheel part dom structure
<template data-filtered="filtered"> <p class="pd-select-item"> <p class="pd-select-line"></p> <ul class="pd-select-list"> <li class="pd-select-list-item">1</li> </ul> <ul class="pd-select-wheel"> <li class="pd-select-wheel-item">1</li> </ul> </p> </template> props props: { data: { type: Array, required: true }, type: { type: String, default: 'cycle' }, value: {} }
Set the css style to center it vertically
.pd-select-line, .pd-select-list, .pd-select-wheel { position: absolute; left: 0; right: 0; top: 50%; transform: translateY(-50%); } .pd-select-list { overflow: hidden; }
Scroller 3d style setting
/* 滚轮盒子 */ .pd-select-wheel { transform-style: preserve-3d; height: 30px; } /* 滚轮单项 */ .pd-select-wheel-item { white-space: nowrap; text-overflow: ellipsis; backface-visibility: hidden; position: absolute; top: 0px; width: 100%; overflow: hidden; }
Mainly pay attention to the two attributes transform-style: preserve-3d; backface-visibility: hidden;
The first one is the 3D layout, which makes the interface 3D, and the second one is the The wheel is automatically hidden behind it (the red part in the picture above, the dom node on the back will be automatically hidden)
How to implement the 3D wheel
The main sentence of the box is this css transform: rotate3d(1 , 0, 0, x deg);
item mainly uses this css transform: rotate3d(1, 0, 0, xdeg) translate3d(0px, 0px, [x]px);
The above two pictures show the effect of translate3d(0px, 0px, [x]px); [x ] is the radius of the circle
#As you can see from the above picture, we only need to rotate each dom itself, and then use translate3d(0px, 0px, [x]px); Expand each dom
to form a ring. α is the angle of rotation of each dom. Because only 0 to 180° is used here, a box is used to contain these dom
Line height and angle calculation
#Calculate the length of the third side if the two sides and the angle are known~=34px
http ://tool.520101.com/calculator/sanjiaoxingjiaodu/
Infinite scroll wheel implementation
/* 滚轮展示大小限定 */ spin: {start: 0, end: 9, branch: 9} /* 获取spin 数据 */ getSpinData (index) { index = index % this.listData.length return this.listData[index >= 0 ? index : index + this.listData.length] } /* 模运算 获取数组有的索引 这样就构成 圆环了 */
touchend does special processing
Set the setCSS type in touchend to round the scrolling data, so that when it stops, it will
accurately rotate one grid at a time
// other code .... /* 计算touchEnd移动的整数距离 */ let endMove = margin let endDeg = Math.round(updateDeg / deg) * deg if (type === 'end') { this.setListTransform(endMove, margin) this.setWheelDeg(endDeg) } else { this.setListTransform(updateMove, margin) this.setWheelDeg(updateDeg) } // other code .... 惯性缓动 // other code .... setWheelDeg (updateDeg, type, time = 1000) { if (type === 'end') { this.$refs.wheel.style.webkitTransition = `transform ${time}ms cubic-bezier(0.19, 1, 0.22, 1)` this.$refs.wheel.style.webkitTransform = `rotate3d(1, 0, 0, ${updateDeg}deg)` } else { this.$refs.wheel.style.webkitTransition = '' this.$refs.wheel.style.webkitTransform = `rotate3d(1, 0, 0, ${updateDeg}deg)` } } setListTransform (translateY = 0, marginTop = 0, type, time = 1000) { if (type === 'end') { this.$refs.list.style.webkitTransition = `transform ${time}ms cubic-bezier(0.19, 1, 0.22, 1)` this.$refs.list.style.webkitTransform = `translateY(${translateY - this.spin.branch * 34}px)` this.$refs.list.style.marginTop = `${-marginTop}px` this.$refs.list.setAttribute('scroll', translateY) console.log('end') } else { this.$refs.list.style.webkitTransition = '' this.$refs.list.style.webkitTransform = `translateY(${translateY - this.spin.branch * 34}px)` this.$refs.list.style.marginTop = `${-marginTop}px` this.$refs.list.setAttribute('scroll', translateY) } } // other code ....
Get the current Selected value
/* 在设置完css后获取值 */ setStyle (move, type, time) { // ...other code /* 设置$emit 延迟 */ setTimeout(() => this.getPickValue(endMove), 1000) // ...other code } /* 获取选中值 */ getPickValue (move) { let index = Math.abs(move / 34) let pickValue = this.getSpinData(index) this.$emit('input', pickValue) }
Initialization settings
mounted () { /* 事件绑定 */ this.$el.addEventListener('touchstart', this.itemTouchStart) this.$el.addEventListener('touchmove', this.itemTouchMove) this.$el.addEventListener('touchend', this.itemTouchEnd) /* 初始化状态 */ let index = this.listData.indexOf(this.value) if (index === -1) { console.warn('当前初始值不存在,请检查后listData范围!!') this.setListTransform() this.getPickValue(0) } else { let move = index * 34 /* 因为往上滑动所以是负 */ this.setStyle(-move) this.setListTransform(-move, -move) }
When displayed as a non-infinite scroll wheel
Here we are very A good judgment is that the scrolling distance cannot exceed the array length of the original number * 34, and cannot be less than 0 (direction involved in the actual code)
/* 根据滚轮类型 line or cycle 判断 updateMove最大距离 */ if (this.type === 'line') { if (updateMove > 0) { updateMove = 0 } if (updateMove < -(this.listData.length - 1) * singleHeight) { updateMove = -(this.listData.length - 1) * singleHeight } } /* 根据type 控制滚轮显示效果 */ setHidden (index) { if (this.type === 'line') { return index < 0 || index > this.listData.length - 1 } else { return false } },
dom structure also adds a corresponding response
<p class="pd-select-item"> <p class="pd-select-line"></p> <p class="pd-select-list"> <ul class="pd-select-ul" ref="list"> <li class="pd-select-list-item" v-for="el,index in renderData " :class="{'hidden':setHidden(el.index)}" :key="index">{{el.value}}</li> </ul> </p> <ul class="pd-select-wheel" ref="wheel"> <li class="pd-select-wheel-item" :class="{'hidden':setHidden(el.index)}" :style="setWheelItemDeg(el.index)" :index="el.index" v-for="el,index in renderData " :key="index">{{el.value}}</li> </ul> </p>
The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.
Related articles:
JavaScript module optimization
How to extract third-party libraries using webpack
How to determine the client type using JS
The above is the detailed content of How to implement picker effect in vue. For more information, please follow other related articles on the PHP Chinese website!

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version
God-level code editing software (SublimeText3)