naive-ui/packages/mixins/texttransparentable.js

40 lines
1.2 KiB
JavaScript
Raw Normal View History

2019-08-01 18:19:03 +08:00
export default {
data () {
return {
ascendantBackgroundColor: null,
cssNode: null
}
},
mounted () {
let cursor = this.$el
while (cursor.parentElement) {
cursor = cursor.parentElement
const backgroundColor = getComputedStyle(cursor).backgroundColor
if (backgroundColor && backgroundColor !== 'rgba(0, 0, 0, 0)') {
this.ascendantBackgroundColor = backgroundColor
break
}
}
const id = 'x' + Math.random().toString(16).slice(9)
2019-09-06 16:15:19 +08:00
this.$el.setAttribute('n-background-id', id)
2019-08-01 18:19:03 +08:00
this.cssNode = document.createElement('style')
2019-09-06 16:15:19 +08:00
this.cssNode.innerHTML = `[n-background-id=${id}] .simulate-transparent-text {
2019-09-05 18:06:01 +08:00
color: ${this.ascendantBackgroundColor}!important;
}
2019-09-06 16:15:19 +08:00
[n-background-id=${id}] .simulate-transparent-background {
2019-09-05 18:06:01 +08:00
background-color: ${this.ascendantBackgroundColor}!important;
2019-09-06 13:36:27 +08:00
}
2019-09-06 16:15:19 +08:00
[n-background-id=${id}] .simulate-transparent-stroke circle {
2019-09-06 13:36:27 +08:00
stroke: ${this.ascendantBackgroundColor}!important;
}
`
2019-08-01 18:19:03 +08:00
this.cssNode.type = 'text/css'
document.querySelector('head').appendChild(this.cssNode)
},
beforeDestroy () {
window.setTimeout(() => {
document.querySelector('head').removeChild(this.cssNode)
}, 1000)
2019-08-01 18:19:03 +08:00
}
}