native.js 1.3 KB
Newer Older
Q
qiang 已提交
1 2 3 4
export default {
  name: 'Native',
  data () {
    return {
Q
qiang 已提交
5
      position: {
Q
qiang 已提交
6 7 8 9 10 11
        top: '0px',
        left: '0px',
        width: '0px',
        height: '0px',
        position: 'static'
      },
Q
qiang 已提交
12
      hidden: false
Q
qiang 已提交
13 14
    }
  },
Q
qiang 已提交
15 16 17 18
  created () {
    this.isNative = true
    this.onCanInsertCallbacks = []
  },
Q
qiang 已提交
19
  mounted () {
Q
qiang 已提交
20
    this._updatePosition()
Q
qiang 已提交
21
    this.$nextTick(() => {
Q
qiang 已提交
22
      this.onCanInsertCallbacks.forEach(callback => callback())
Q
qiang 已提交
23
    })
Q
qiang 已提交
24
    this.$on('uni-view-update', this._requestPositionUpdate)
Q
qiang 已提交
25 26
  },
  methods: {
Q
qiang 已提交
27 28 29 30 31 32 33 34
    _updatePosition () {
      const rect = (this.$refs.container || this.$el).getBoundingClientRect()
      this.hidden = rect.width === 0 || rect.height === 0
      if (!this.hidden) {
        ['top', 'left', 'width', 'height'].forEach(key => {
          let val = rect[key]
          val = key === 'top' ? val + (document.documentElement.scrollTop || document.body.scrollTop || 0) : val
          this.position[key] = val + 'px'
Q
qiang 已提交
35 36
        })
      }
Q
qiang 已提交
37 38 39 40 41 42 43 44 45
    },
    _requestPositionUpdate () {
      if (this._positionUpdateRequest) {
        cancelAnimationFrame(this._positionUpdateRequest)
      }
      this._positionUpdateRequest = requestAnimationFrame(() => {
        delete this._positionUpdateRequest
        this._updatePosition()
      })
Q
qiang 已提交
46 47 48
    }
  }
}