index.vue 7.7 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3
<script>
import touchtrack from 'uni-mixins/touchtrack'
import scroller from 'uni-mixins/scroller/index'
4 5
import { Friction } from 'uni-mixins/scroller/Friction'
import { Spring } from 'uni-mixins/scroller/Spring'
fxy060608's avatar
fxy060608 已提交
6

7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
function onClick (dom, callback) {
  const MAX_MOVE = 20
  const hasTouchSupport = navigator.maxTouchPoints
  let x = 0
  let y = 0
  dom.addEventListener(hasTouchSupport ? 'touchstart' : 'mousedown', (event) => {
    const info = hasTouchSupport ? event.changedTouches[0] : event
    x = info.clientX
    y = info.clientY
  })
  dom.addEventListener(hasTouchSupport ? 'touchend' : 'mouseup', (event) => {
    const info = hasTouchSupport ? event.changedTouches[0] : event
    if (Math.abs(info.clientX - x) < MAX_MOVE && Math.abs(info.clientY - y) < MAX_MOVE) {
      callback(info)
    }
  })
}

fxy060608's avatar
fxy060608 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
export default {
  name: 'PickerViewColumn',
  mixins: [touchtrack, scroller],
  data () {
    return {
      scope: `picker-view-column-${Date.now()}`,
      inited: false,
      indicatorStyle: '',
      indicatorClass: '',
      indicatorHeight: 34,
      maskStyle: '',
      maskClass: '',
      current: this.$parent.getItemValue(this),
      length: 0
    }
  },
  computed: {
42 43 44
    height () {
      return this.$parent.height
    },
fxy060608's avatar
fxy060608 已提交
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
    maskSize () {
      return (this.height - this.indicatorHeight) / 2
    }
  },
  watch: {
    indicatorHeight (val) {
      this._setItemHeight(val)
      if (this.inited) {
        this.update()
      }
    },
    current (val) {
      this.$parent.setItemValue(this, val)
    },
    length (val) {
      if (this.inited) {
        this.update(val)
      }
    }
  },
  created: function () {
    var $parent = this.$parent
    this.indicatorStyle = $parent.indicatorStyle
    this.indicatorClass = $parent.indicatorClass
    this.maskStyle = $parent.maskStyle
    this.maskClass = $parent.maskClass
71
    this.deltaY = 0
fxy060608's avatar
fxy060608 已提交
72 73 74 75 76 77 78 79
  },
  mounted: function () {
    this.touchtrack(this.$refs.main, '_handleTrack', true)
    this.setCurrent(this.current)
    this.$nextTick(() => {
      this.init()
      this.update()
    })
80
    onClick(this.$el, this._handleTap.bind(this))
fxy060608's avatar
fxy060608 已提交
81 82 83 84
  },
  methods: {
    _setItemHeight (height) {
      var style = document.createElement('style')
85
      style.innerText = `.uni-picker-view-content.${this.scope}>*{height: ${height}px;overflow: hidden;}`
fxy060608's avatar
fxy060608 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
      document.head.appendChild(style)
    },
    _handleTrack: function (e) {
      if (this._scroller) {
        switch (e.detail.state) {
          case 'start':
            this._handleTouchStart(e)
            break
          case 'move':
            this._handleTouchMove(e)
            break
          case 'end':
          case 'cancel':
            this._handleTouchEnd(e)
        }
      }
    },
103 104 105 106
    _handleTap: function ({ clientY }) {
      if (!this._scroller.isScrolling()) {
        var rect = this.$el.getBoundingClientRect()
        var r = clientY - rect.top - this.height / 2
fxy060608's avatar
fxy060608 已提交
107 108 109 110
        var o = this.indicatorHeight / 2
        if (!(Math.abs(r) <= o)) {
          var a = Math.ceil((Math.abs(r) - o) / this.indicatorHeight)
          var s = r < 0 ? -a : a
111 112 113
          var current = Math.min(this.current + s, this.length - 1)
          this.current = current = Math.max(current, 0)
          this._scroller.scrollTo(current * this.indicatorHeight)
fxy060608's avatar
fxy060608 已提交
114 115 116
        }
      }
    },
117 118 119 120 121 122 123 124 125 126 127 128
    _handleWheel ($event) {
      const deltaY = this.deltaY + $event.deltaY
      if (Math.abs(deltaY) > 10) {
        this.deltaY = 0
        var current = Math.min(this.current + (deltaY < 0 ? -1 : 1), this.length - 1)
        this.current = current = Math.max(current, 0)
        this._scroller.scrollTo(current * this.indicatorHeight)
      } else {
        this.deltaY = deltaY
      }
      $event.preventDefault()
    },
fxy060608's avatar
fxy060608 已提交
129 130 131 132 133 134 135 136 137 138 139 140 141 142
    setCurrent: function (current) {
      if (current !== this.current) {
        this.current = current
        if (this.inited) {
          this.update()
        }
      }
    },
    init: function () {
      this.initScroller(this.$refs.content, {
        enableY: true,
        enableX: false,
        enableSnap: true,
        itemSize: this.indicatorHeight,
143 144
        friction: new Friction(0.0001),
        spring: new Spring(2, 90, 20),
fxy060608's avatar
fxy060608 已提交
145 146 147 148 149 150 151 152 153 154
        onSnap: (index) => {
          if ((!isNaN(index)) && index !== this.current) {
            this.current = index
          }
        }
      })
      this.inited = true
    },
    update: function () {
      this.$nextTick(() => {
155 156
        var current = Math.min(this.current, this.length - 1)
        current = Math.max(current, 0)
fxy060608's avatar
fxy060608 已提交
157 158
        this._scroller.update(current * this.indicatorHeight, undefined, this.indicatorHeight)
      })
159 160 161 162 163
    },
    _resize ({
      height
    }) {
      this.indicatorHeight = height
fxy060608's avatar
fxy060608 已提交
164 165 166 167
    }
  },
  render (createElement) {
    this.length = (this.$slots.default && this.$slots.default.length) || 0
168 169 170 171 172
    return createElement('uni-picker-view-column', {
      on: {
        wheel: this._handleWheel
      }
    }, [
fxy060608's avatar
fxy060608 已提交
173 174
      createElement('div', {
        ref: 'main',
175
        staticClass: 'uni-picker-view-group'
fxy060608's avatar
fxy060608 已提交
176 177 178 179
      },
      [
        createElement('div', {
          ref: 'mask',
180
          staticClass: 'uni-picker-view-mask',
fxy060608's avatar
fxy060608 已提交
181 182 183 184 185
          class: this.maskClass,
          style: `background-size: 100% ${this.maskSize}px;${this.maskStyle}`
        }),
        createElement('div', {
          ref: 'indicator',
186
          staticClass: 'uni-picker-view-indicator',
fxy060608's avatar
fxy060608 已提交
187 188
          class: this.indicatorClass,
          style: this.indicatorStyle
189 190 191 192 193 194 195 196
        }, [createElement('v-uni-resize-sensor', {
          attrs: {
            initial: true
          },
          on: {
            resize: this._resize
          }
        })]),
fxy060608's avatar
fxy060608 已提交
197 198
        createElement('div', {
          ref: 'content',
199
          staticClass: 'uni-picker-view-content',
fxy060608's avatar
fxy060608 已提交
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
          class: this.scope,
          style: `padding: ${this.maskSize}px 0;`
        },
        [this.$slots.default]
        )
      ])
    ]
    )
  }
}
</script>
<style>
uni-picker-view-column {
  -webkit-flex: 1;
  flex: 1;
  position: relative;
  height: 100%;
  overflow: hidden;
}

220 221 222 223
uni-picker-view-column[hidden] {
  display: none;
}

224
.uni-picker-view-group {
fxy060608's avatar
fxy060608 已提交
225 226 227
  height: 100%;
}

228
.uni-picker-view-mask {
fxy060608's avatar
fxy060608 已提交
229 230 231 232
  transform: translateZ(0);
  -webkit-transform: translateZ(0);
}

233 234
.uni-picker-view-indicator,
.uni-picker-view-mask {
fxy060608's avatar
fxy060608 已提交
235 236 237 238 239 240
  position: absolute;
  left: 0;
  width: 100%;
  z-index: 3;
}

241
.uni-picker-view-mask {
fxy060608's avatar
fxy060608 已提交
242 243 244 245 246 247 248 249 250 251 252 253 254 255
  top: 0;
  height: 100%;
  margin: 0 auto;
  background: linear-gradient(
      180deg,
      hsla(0, 0%, 100%, 0.95),
      hsla(0, 0%, 100%, 0.6)
    ),
    linear-gradient(0deg, hsla(0, 0%, 100%, 0.95), hsla(0, 0%, 100%, 0.6));
  background-position: top, bottom;
  background-size: 100% 102px;
  background-repeat: no-repeat;
}

256
.uni-picker-view-indicator {
fxy060608's avatar
fxy060608 已提交
257 258 259 260 261 262
  height: 34px;
  /* top: 102px; */
  top: 50%;
  transform: translateY(-50%);
}

263 264
.uni-picker-view-indicator,
.uni-picker-view-mask {
fxy060608's avatar
fxy060608 已提交
265 266 267 268 269 270 271
  position: absolute;
  left: 0;
  width: 100%;
  z-index: 3;
  pointer-events: none;
}

272
.uni-picker-view-content {
fxy060608's avatar
fxy060608 已提交
273 274 275 276 277 278 279 280
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  will-change: transform;
  padding: 102px 0;
}

281
.uni-picker-view-content > * {
fxy060608's avatar
fxy060608 已提交
282 283 284 285
  height: 34px;
  overflow: hidden;
}

286 287
.uni-picker-view-indicator:after,
.uni-picker-view-indicator:before {
fxy060608's avatar
fxy060608 已提交
288 289 290 291 292 293 294 295
  content: " ";
  position: absolute;
  left: 0;
  right: 0;
  height: 1px;
  color: #e5e5e5;
}

296
.uni-picker-view-indicator:before {
fxy060608's avatar
fxy060608 已提交
297 298 299 300 301 302 303 304
  top: 0;
  border-top: 1px solid #e5e5e5;
  -webkit-transform-origin: 0 0;
  transform-origin: 0 0;
  -webkit-transform: scaleY(0.5);
  transform: scaleY(0.5);
}

305
.uni-picker-view-indicator:after {
fxy060608's avatar
fxy060608 已提交
306 307 308 309 310 311 312 313
  bottom: 0;
  border-bottom: 1px solid #e5e5e5;
  -webkit-transform-origin: 0 100%;
  transform-origin: 0 100%;
  -webkit-transform: scaleY(0.5);
  transform: scaleY(0.5);
}

314 315
.uni-picker-view-indicator:after,
.uni-picker-view-indicator:before {
fxy060608's avatar
fxy060608 已提交
316 317 318 319 320 321 322 323
  content: " ";
  position: absolute;
  left: 0;
  right: 0;
  height: 1px;
  color: #e5e5e5;
}
</style>