index.vue 6.6 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-view-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
    }
  },
  render (createElement) {
    this.length = (this.$slots.default && this.$slots.default.length) || 0
    return createElement('uni-picker-view-column', {
      on: {
        tap: this._handleTap
      }
    },
    [
      createElement('div', {
        ref: 'main',
142
        staticClass: 'uni-picker-view-group'
fxy060608's avatar
fxy060608 已提交
143 144 145 146
      },
      [
        createElement('div', {
          ref: 'mask',
147
          staticClass: 'uni-picker-view-mask',
fxy060608's avatar
fxy060608 已提交
148 149 150 151 152
          class: this.maskClass,
          style: `background-size: 100% ${this.maskSize}px;${this.maskStyle}`
        }),
        createElement('div', {
          ref: 'indicator',
153
          staticClass: 'uni-picker-view-indicator',
fxy060608's avatar
fxy060608 已提交
154 155
          class: this.indicatorClass,
          style: this.indicatorStyle
156 157 158 159 160 161 162 163
        }, [createElement('v-uni-resize-sensor', {
          attrs: {
            initial: true
          },
          on: {
            resize: this._resize
          }
        })]),
fxy060608's avatar
fxy060608 已提交
164 165
        createElement('div', {
          ref: 'content',
166
          staticClass: 'uni-picker-view-content',
fxy060608's avatar
fxy060608 已提交
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
          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;
}

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

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

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

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

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

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

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

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

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

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

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

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

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