1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
| <template> <div class="ix"> {{pageData}} <div class="picker-bg" ref="bg" @touchstart="pStart" @touchmove="pMove" @touchend="pEnd"> <div class="select-ac"></div> <div class="picker" ref="test"> <div class="box" ref="box"> <div v-for="(item,index) in picData.data" :id="'a' + index" :ref="'a' + index" :key="index" @click="cIndex(index)">{{item}} </div> </div> </div> </div> </div>
</template> <script> export default { name: 'picker', data: function () { return { picData: { default: '1', data: ['1', '2', '3', '4', '5', '6', '7', '1', '2', '3', '4', '5', '6', '7'] }, pageData: { start: '', end: '', move: '' } } }, created: function () { }, // 创建时 beforeMount: function () {}, // 挂载之前 mounted: function () {}, // 挂载之后 beforeUpdate: function () {}, // 数据更新时调用,在渲染之前 updated: function () {}, // 数据更新后,渲染后调用(禁止) beforeDestroy: function () {}, // 实例销毁前调用,解绑中间层的数据传输 destroyed: function () {}, // 实例销毁后调用 methods: { cIndex (i) { console.log('--i--', i) }, pMove (e) { // 获取当前位置 let newHeight = this.$refs.box.style.transform.split('px,')[1] || 0 // 获取上一次的位置 let oldHeight = this.pageData.start // 计算活动距离 this.pageData.move = e.targetTouches[0].pageY let tempHeight = e.targetTouches[0].pageY - this.pageData.start let endHeight = Number(newHeight) + Number(tempHeight * 1.5) console.log('--e--', newHeight, oldHeight, tempHeight, endHeight) this.pageData.start = e.targetTouches[0].pageY this.$refs.box.style.transform = `translate3d(0px, ${endHeight}px, 0px)` e.preventDefault() }, pEnd () { // 当前transform let oldHeight = this.$refs.box.style.transform.split('px,')[1] || 0 this.pageData.end = oldHeight // options 高度,数量 let tempHeight = document.querySelectorAll('.picker .box div')[0].offsetHeight let tempNumber = document.querySelectorAll('.picker .box div').length - 1 let index = Math.round(oldHeight / tempHeight) console.log('--end--', index, tempNumber, tempHeight, this.picData.data.length) if (index < -tempNumber) { index = -tempNumber } if (index > 0) { index = 0 } this.$refs.box.style.transform = `translate3d(0px, ${index * 60}px, 0px)` }, pStart (e) { this.pageData.start = e.targetTouches[0].pageY console.log('--start--', this.pageData.start) } } // 函数 } </script> <style lang='scss'>
.ix { position: fixed; width: 100vw; bottom: 0; z-index: 5; }
$height: 60px; .picker-bg { display: block; position: relative; height: 5 * $height; overflow: hidden; width: 100%; }
$acBg: rgba(86, 86, 86, 0.2); $conBg: rgba(252, 252, 252, 0.70); .select-ac { width: 100%; height: $height; user-select: none; background-color: $acBg; position: absolute; left: 0; z-index: 3; top: 2 * $height; pointer-events: none; &::after, &::before { content: ""; display: block; position: absolute; background-color: $conBg; width: 100%; height: 500%; pointer-events: auto; } &::before { bottom: $height; } &::after { top: $height; } }
.picker { box-sizing: border-box; position: relative; /*overflow-y: auto;*/ &::-webkit-scrollbar { width: 0; } .box { user-select: none; transition: all 0.4s; display: flex; flex-direction: column; justify-content: center; padding-top: 2 * $height; padding-bottom: 2 * $height; div { user-select: none; height: $height; line-height: $height; text-align: center; font-size: 18px; } }
} </style>
|