123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- <template>
- <view class="u-sticky" :id="elId" :style="[style]">
- <view :style="[stickyContent]" class="u-sticky__content"><slot /></view>
- </view>
- </template>
- <script>
- import { deepMerge, addStyle, addUnit, sleep, guid, getPx, os, sys } from '@/sheep/helper';
- import sheep from '@/sheep';
-
- export default {
- name: 'su-sticky',
- props: {
-
- offsetTop: {
- type: [String, Number],
- default: 0,
- },
-
- customNavHeight: {
- type: [String, Number],
-
-
- default: 44,
-
-
- default: sheep.$platform.navbar,
-
- },
-
- stickyToTop: {
- type: Boolean,
- default: false,
- },
-
- bgColor: {
- type: String,
- default: 'transparent',
- },
-
- zIndex: {
- type: [String, Number],
- default: '',
- },
-
- index: {
- type: [String, Number],
- default: '',
- },
- customStyle: {
- type: [Object, String],
- default: () => ({}),
- },
- },
- data() {
- return {
- cssSticky: false,
- stickyTop: 0,
- elId: guid(),
- left: 0,
- width: 'auto',
- height: 'auto',
- fixed: false,
- };
- },
- computed: {
- style() {
- const style = {};
- if (!this.stickyToTop) {
- if (this.cssSticky) {
- style.position = 'sticky';
- style.zIndex = this.uZindex;
- style.top = addUnit(this.stickyTop);
- } else {
- style.height = this.fixed ? this.height + 'px' : 'auto';
- }
- } else {
-
-
- style.position = 'relative';
-
-
- style.position = 'static';
-
- }
- style.backgroundColor = this.bgColor;
- return deepMerge(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 : 970;
- },
- },
- mounted() {
- this.init();
- },
- methods: {
- init() {
- this.getStickyTop();
-
- this.checkSupportCssSticky();
-
- if (!this.cssSticky) {
- !this.stickyToTop && this.initObserveContent();
- }
- },
- $uGetRect(selector, all) {
- return new Promise((resolve) => {
- uni.createSelectorQuery()
- .in(this)
- [all ? 'selectAll' : 'select'](selector)
- .boundingClientRect((rect) => {
- if (all && Array.isArray(rect) && rect.length) {
- resolve(rect);
- }
- if (!all && rect) {
- resolve(rect);
- }
- })
- .exec();
- });
- },
- 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 = getPx(this.offsetTop) + getPx(this.customNavHeight);
- },
- async checkSupportCssSticky() {
-
-
- if (this.checkCssStickyForH5()) {
- this.cssSticky = true;
- }
-
-
- if (os() === 'android' && Number(sys().system) > 8) {
- this.cssSticky = true;
- }
-
-
- this.cssSticky = await this.checkComputedStyle();
-
-
- if (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>
|