index.vue 7.2 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 71 72 73 74 75 76 77 78
    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
  },
  mounted: function () {
    this.touchtrack(this.$refs.main, '_handleTrack', true)
    this.setCurrent(this.current)
    this.$nextTick(() => {
      this.init()
      this.update()
    })
79
    onClick(this.$el, this._handleTap.bind(this))
fxy060608's avatar
fxy060608 已提交
80 81 82 83
  },
  methods: {
    _setItemHeight (height) {
      var style = document.createElement('style')
84
      style.innerText = `.uni-picker-view-content.${this.scope}>*{height: ${height}px;overflow: hidden;}`
fxy060608's avatar
fxy060608 已提交
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
      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)
        }
      }
    },
102 103 104 105
    _handleTap: function ({ clientY }) {
      if (!this._scroller.isScrolling()) {
        var rect = this.$el.getBoundingClientRect()
        var r = clientY - rect.top - this.height / 2
fxy060608's avatar
fxy060608 已提交
106 107 108 109
        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
110 111 112
          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 已提交
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
        }
      }
    },
    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,
130 131
        friction: new Friction(0.0001),
        spring: new Spring(2, 90, 20),
fxy060608's avatar
fxy060608 已提交
132 133 134 135 136 137 138 139 140 141
        onSnap: (index) => {
          if ((!isNaN(index)) && index !== this.current) {
            this.current = index
          }
        }
      })
      this.inited = true
    },
    update: function () {
      this.$nextTick(() => {
142 143
        var current = Math.min(this.current, this.length - 1)
        current = Math.max(current, 0)
fxy060608's avatar
fxy060608 已提交
144 145
        this._scroller.update(current * this.indicatorHeight, undefined, this.indicatorHeight)
      })
146 147 148 149 150
    },
    _resize ({
      height
    }) {
      this.indicatorHeight = height
fxy060608's avatar
fxy060608 已提交
151 152 153 154
    }
  },
  render (createElement) {
    this.length = (this.$slots.default && this.$slots.default.length) || 0
155
    return createElement('uni-picker-view-column', {}, [
fxy060608's avatar
fxy060608 已提交
156 157
      createElement('div', {
        ref: 'main',
158
        staticClass: 'uni-picker-view-group'
fxy060608's avatar
fxy060608 已提交
159 160 161 162
      },
      [
        createElement('div', {
          ref: 'mask',
163
          staticClass: 'uni-picker-view-mask',
fxy060608's avatar
fxy060608 已提交
164 165 166 167 168
          class: this.maskClass,
          style: `background-size: 100% ${this.maskSize}px;${this.maskStyle}`
        }),
        createElement('div', {
          ref: 'indicator',
169
          staticClass: 'uni-picker-view-indicator',
fxy060608's avatar
fxy060608 已提交
170 171
          class: this.indicatorClass,
          style: this.indicatorStyle
172 173 174 175 176 177 178 179
        }, [createElement('v-uni-resize-sensor', {
          attrs: {
            initial: true
          },
          on: {
            resize: this._resize
          }
        })]),
fxy060608's avatar
fxy060608 已提交
180 181
        createElement('div', {
          ref: 'content',
182
          staticClass: 'uni-picker-view-content',
fxy060608's avatar
fxy060608 已提交
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
          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;
}

203 204 205 206
uni-picker-view-column[hidden] {
  display: none;
}

207
.uni-picker-view-group {
fxy060608's avatar
fxy060608 已提交
208 209 210
  height: 100%;
}

211
.uni-picker-view-mask {
fxy060608's avatar
fxy060608 已提交
212 213 214 215
  transform: translateZ(0);
  -webkit-transform: translateZ(0);
}

216 217
.uni-picker-view-indicator,
.uni-picker-view-mask {
fxy060608's avatar
fxy060608 已提交
218 219 220 221 222 223
  position: absolute;
  left: 0;
  width: 100%;
  z-index: 3;
}

224
.uni-picker-view-mask {
fxy060608's avatar
fxy060608 已提交
225 226 227 228 229 230 231 232 233 234 235 236 237 238
  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;
}

239
.uni-picker-view-indicator {
fxy060608's avatar
fxy060608 已提交
240 241 242 243 244 245
  height: 34px;
  /* top: 102px; */
  top: 50%;
  transform: translateY(-50%);
}

246 247
.uni-picker-view-indicator,
.uni-picker-view-mask {
fxy060608's avatar
fxy060608 已提交
248 249 250 251 252 253 254
  position: absolute;
  left: 0;
  width: 100%;
  z-index: 3;
  pointer-events: none;
}

255
.uni-picker-view-content {
fxy060608's avatar
fxy060608 已提交
256 257 258 259 260 261 262 263
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  will-change: transform;
  padding: 102px 0;
}

264
.uni-picker-view-content > * {
fxy060608's avatar
fxy060608 已提交
265 266 267 268
  height: 34px;
  overflow: hidden;
}

269 270
.uni-picker-view-indicator:after,
.uni-picker-view-indicator:before {
fxy060608's avatar
fxy060608 已提交
271 272 273 274 275 276 277 278
  content: " ";
  position: absolute;
  left: 0;
  right: 0;
  height: 1px;
  color: #e5e5e5;
}

279
.uni-picker-view-indicator:before {
fxy060608's avatar
fxy060608 已提交
280 281 282 283 284 285 286 287
  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);
}

288
.uni-picker-view-indicator:after {
fxy060608's avatar
fxy060608 已提交
289 290 291 292 293 294 295 296
  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);
}

297 298
.uni-picker-view-indicator:after,
.uni-picker-view-indicator:before {
fxy060608's avatar
fxy060608 已提交
299 300 301 302 303 304 305 306
  content: " ";
  position: absolute;
  left: 0;
  right: 0;
  height: 1px;
  color: #e5e5e5;
}
</style>