123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- <template>
- <view
- class="u-sticky"
- :id="elId"
- :style="[style]"
- >
- <view
- :style="[stickyContent]"
- class="u-sticky__content"
- >
- <slot />
- </view>
- </view>
- </template>
- <script>
- import props from './props.js';;
-
- export default {
- name: 'u-sticky',
- mixins: [uni.$u.mpMixin, uni.$u.mixin, props],
- data() {
- return {
- cssSticky: false,
- stickyTop: 0,
- elId: uni.$u.guid(),
- left: 0,
- width: 'auto',
- height: 'auto',
- fixed: false,
- }
- },
- computed: {
- style() {
- const style = {}
- if(!this.disabled) {
- if (this.cssSticky) {
- style.position = 'sticky'
- style.zIndex = this.uZindex
- style.top = uni.$u.addUnit(this.stickyTop)
- } else {
- style.height = this.fixed ? this.height + 'px' : 'auto'
- }
- } else {
-
-
- style.position = 'relative'
-
-
- style.position = 'static'
-
- }
- style.backgroundColor = this.bgColor
- return uni.$u.deepMerge(uni.$u.addStyle(this.customStyle), style)
- },
-
- stickyContent() {
- const style = {}
- if (!this.cssSticky) {
- style.position = this.fixed ? 'fixed' : 'static'
- style.top = this.stickyTop + 'px'
- style.left = this.left + 'px'
- style.width = this.width == 'auto' ? 'auto' : this.width + 'px'
- style.zIndex = this.uZindex
- }
- return style
- },
- uZindex() {
- return this.zIndex ? this.zIndex : uni.$u.zIndex.sticky
- }
- },
- mounted() {
- this.init()
- },
- methods: {
- init() {
- this.getStickyTop()
-
- this.checkSupportCssSticky()
-
- if (!this.cssSticky) {
- !this.disabled && this.initObserveContent()
- }
- },
- initObserveContent() {
-
- this.$uGetRect('#' + this.elId).then((res) => {
- this.height = res.height
- this.left = res.left
- this.width = res.width
- this.$nextTick(() => {
- this.observeContent()
- })
- })
- },
- observeContent() {
-
- this.disconnectObserver('contentObserver')
- const contentObserver = uni.createIntersectionObserver({
-
- thresholds: [0.95, 0.98, 1]
- })
-
- contentObserver.relativeToViewport({
- top: -this.stickyTop
- })
-
- contentObserver.observe(`#${this.elId}`, res => {
- this.setFixed(res.boundingClientRect.top)
- })
- this.contentObserver = contentObserver
- },
- setFixed(top) {
-
- const fixed = top <= this.stickyTop
- this.fixed = fixed
- },
- disconnectObserver(observerName) {
-
- const observer = this[observerName]
- observer && observer.disconnect()
- },
- getStickyTop() {
- this.stickyTop = uni.$u.getPx(this.offsetTop) + uni.$u.getPx(this.customNavHeight)
- },
- async checkSupportCssSticky() {
-
-
- if (this.checkCssStickyForH5()) {
- this.cssSticky = true
- }
-
-
- if (uni.$u.os() === 'android' && Number(uni.$u.sys().system) > 8) {
- this.cssSticky = true
- }
-
-
- this.cssSticky = await this.checkComputedStyle()
-
-
- if (uni.$u.os() === 'ios') {
- this.cssSticky = true
- }
-
-
- this.cssSticky = true
-
- },
-
- checkComputedStyle() {
-
-
- return new Promise(resolve => {
- uni.createSelectorQuery().in(this).select('.u-sticky').fields({
- computedStyle: ["position"]
- }).exec(e => {
- resolve('sticky' === e[0].position)
- })
- })
-
- },
-
-
- checkCssStickyForH5() {
-
-
- const vendorList = ['', '-webkit-', '-ms-', '-moz-', '-o-'],
- vendorListLength = vendorList.length,
- stickyElement = document.createElement('div')
- for (let i = 0; i < vendorListLength; i++) {
- stickyElement.style.position = vendorList[i] + 'sticky'
- if (stickyElement.style.position !== '') {
- return true
- }
- }
- return false;
-
- }
- },
- beforeDestroy() {
- this.disconnectObserver('contentObserver')
- }
- }
- </script>
- <style lang="scss" scoped>
- .u-sticky {
-
-
- position: sticky;
-
- }
- </style>
|