transparent.js 2.6 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3 4 5 6 7 8
import {
  hexToRgba
} from 'uni-shared'

export default {
  mounted () {
    if (this.type === 'transparent') {
      const transparentElemStyle = this.$el.querySelector('.uni-page-head-transparent').style
9
      const titleElem = this.$el.querySelector('.uni-page-head__title')
fxy060608's avatar
fxy060608 已提交
10 11
      const iconElems = this.$el.querySelectorAll('.uni-btn-icon')
      const iconElemsStyles = []
12
      const textColor = this.textColor
fxy060608's avatar
fxy060608 已提交
13 14 15
      for (let i = 0; i < iconElems.length; i++) {
        iconElemsStyles.push(iconElems[i].style)
      }
16 17
      const borderRadiusElems = this.$el.querySelectorAll('.uni-page-head-btn')
      const oldColors = []
fxy060608's avatar
fxy060608 已提交
18 19
      const borderRadiusElemsStyles = []
      for (let i = 0; i < borderRadiusElems.length; i++) {
20 21 22
        let borderRadiusElem = borderRadiusElems[i]
        oldColors.push(getComputedStyle(borderRadiusElem).backgroundColor)
        borderRadiusElemsStyles.push(borderRadiusElem.style)
fxy060608's avatar
fxy060608 已提交
23 24 25 26 27 28 29 30 31 32 33
      }
      this._A = 0
      UniViewJSBridge.on('onPageScroll', ({
        scrollTop
      }) => {
        const alpha = Math.min(scrollTop / this.offset, 1)
        if (alpha === 1 && this._A === 1) {
          return
        }
        if (alpha > 0.5 && this._A <= 0.5) {
          iconElemsStyles.forEach(function (iconElemStyle) {
34
            iconElemStyle.color = textColor
fxy060608's avatar
fxy060608 已提交
35 36 37 38 39 40 41 42
          })
        } else if (alpha <= 0.5 && this._A > 0.5) {
          iconElemsStyles.forEach(function (iconElemStyle) {
            iconElemStyle.color = '#fff'
          })
        }
        this._A = alpha
        // TODO 暂时仅处理背景色
43 44 45
        if (titleElem) {
          titleElem.style.opacity = alpha
        }
fxy060608's avatar
fxy060608 已提交
46
        transparentElemStyle.backgroundColor = `rgba(${this._R},${this._G},${this._B},${alpha})`
47 48
        borderRadiusElemsStyles.forEach(function (borderRadiusElemStyle, index) {
          let oldColor = oldColors[index]
49
          // eslint-disable-next-line
50
          let rgba = oldColor.match(/[\d+\.]+/g)
51 52
          rgba[3] = (1 - alpha) * (rgba.length === 4 ? rgba[3] : 1)
          borderRadiusElemStyle.backgroundColor = `rgba(${rgba})`
fxy060608's avatar
fxy060608 已提交
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
        })
      })
    }
  },
  computed: {
    color () {
      return this.type === 'transparent' ? '#fff' : this.textColor
    },
    offset () {
      return parseInt(this.coverage)
    },
    bgColor () {
      if (this.type === 'transparent') {
        const {
          r,
          g,
          b
        } = hexToRgba(this.backgroundColor)
        this._R = r
        this._G = g
        this._B = b
        return `rgba(${r},${g},${b},0)`
      }
      return this.backgroundColor
    }
  }
79
}