index.vue 6.5 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
<script>
import touchtrack from 'uni-mixins/touchtrack'
import scroller from 'uni-mixins/scroller/index'

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: {
22 23 24
    height () {
      return this.$parent.height
    },
fxy060608's avatar
fxy060608 已提交
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
    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
    // this.__pageRerender = this._pageRerender.bind(this)
  },
  mounted: function () {
    this.touchtrack(this.$refs.main, '_handleTrack', true)
    this.setCurrent(this.current)
    this.$nextTick(() => {
      this.init()
      this.update()
    })
  },
  methods: {
    _setItemHeight (height) {
      var style = document.createElement('style')
64
      style.innerText = `.uni-picker-content.${this.scope}>*{height: ${height}px;overflow: hidden;}`
fxy060608's avatar
fxy060608 已提交
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
      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)
        }
      }
    },
    _handleTap: function (e) {
      if (e.target !== e.currentTarget && !this._scroller.isScrolling()) {
        var t = e.touches && e.touches[0] && e.touches[0].clientY
        var n = typeof t === 'number' ? t : e.detail.y - document.body.scrollTop
        var i = this.$el.getBoundingClientRect()
        var r = n - i.top - this._height / 2
        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
          this.current += s
          this._scroller.scrollTo(this.current * this.indicatorHeight)
        }
      }
    },
    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,
        onSnap: (index) => {
          if ((!isNaN(index)) && index !== this.current) {
            this.current = index
          }
        }
      })
      this.inited = true
    },
    update: function () {
      this.$nextTick(() => {
        var index = Math.max(this.length - 1, 0)
        var current = Math.min(this.current, index)
        this._scroller.update(current * this.indicatorHeight, undefined, this.indicatorHeight)
      })
125 126 127 128 129
    },
    _resize ({
      height
    }) {
      this.indicatorHeight = height
fxy060608's avatar
fxy060608 已提交
130 131 132 133 134 135 136 137 138 139 140 141 142
    }
  },
  render (createElement) {
    this.length = (this.$slots.default && this.$slots.default.length) || 0
    return createElement('uni-picker-view-column', {
      staticClass: '_picker-view-column',
      on: {
        tap: this._handleTap
      }
    },
    [
      createElement('div', {
        ref: 'main',
143
        staticClass: 'uni-picker-group'
fxy060608's avatar
fxy060608 已提交
144 145 146 147
      },
      [
        createElement('div', {
          ref: 'mask',
148
          staticClass: 'uni-picker-mask',
fxy060608's avatar
fxy060608 已提交
149 150 151 152 153
          class: this.maskClass,
          style: `background-size: 100% ${this.maskSize}px;${this.maskStyle}`
        }),
        createElement('div', {
          ref: 'indicator',
154
          staticClass: 'uni-picker-indicator',
fxy060608's avatar
fxy060608 已提交
155 156
          class: this.indicatorClass,
          style: this.indicatorStyle
157 158 159 160 161 162 163 164
        }, [createElement('v-uni-resize-sensor', {
          attrs: {
            initial: true
          },
          on: {
            resize: this._resize
          }
        })]),
fxy060608's avatar
fxy060608 已提交
165 166
        createElement('div', {
          ref: 'content',
167
          staticClass: 'uni-picker-content',
fxy060608's avatar
fxy060608 已提交
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
          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;
}

188 189 190 191 192
uni-picker-view-column[hidden] {
  display: none;
}

.uni-picker-group {
fxy060608's avatar
fxy060608 已提交
193 194 195
  height: 100%;
}

196
.uni-picker-mask {
fxy060608's avatar
fxy060608 已提交
197 198 199 200
  transform: translateZ(0);
  -webkit-transform: translateZ(0);
}

201 202
.uni-picker-indicator,
.uni-picker-mask {
fxy060608's avatar
fxy060608 已提交
203 204 205 206 207 208
  position: absolute;
  left: 0;
  width: 100%;
  z-index: 3;
}

209
.uni-picker-mask {
fxy060608's avatar
fxy060608 已提交
210 211 212 213 214 215 216 217 218 219 220 221 222 223
  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;
}

224
.uni-picker-indicator {
fxy060608's avatar
fxy060608 已提交
225 226 227 228 229 230
  height: 34px;
  /* top: 102px; */
  top: 50%;
  transform: translateY(-50%);
}

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

240
.uni-picker-content {
fxy060608's avatar
fxy060608 已提交
241 242 243 244 245 246 247 248
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  will-change: transform;
  padding: 102px 0;
}

249
.uni-picker-content > * {
fxy060608's avatar
fxy060608 已提交
250 251 252 253
  height: 34px;
  overflow: hidden;
}

254 255
.uni-picker-indicator:after,
.uni-picker-indicator:before {
fxy060608's avatar
fxy060608 已提交
256 257 258 259 260 261 262 263
  content: " ";
  position: absolute;
  left: 0;
  right: 0;
  height: 1px;
  color: #e5e5e5;
}

264
.uni-picker-indicator:before {
fxy060608's avatar
fxy060608 已提交
265 266 267 268 269 270 271 272
  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);
}

273
.uni-picker-indicator:after {
fxy060608's avatar
fxy060608 已提交
274 275 276 277 278 279 280 281
  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);
}

282 283
.uni-picker-indicator:after,
.uni-picker-indicator:before {
fxy060608's avatar
fxy060608 已提交
284 285 286 287 288 289 290 291
  content: " ";
  position: absolute;
  left: 0;
  right: 0;
  height: 1px;
  color: #e5e5e5;
}
</style>