index.js 2.73 KB
Newer Older
程默 committed
1
import { getRect } from '../common/utils';
李嘉林 committed
2
import { VantComponent } from '../common/component';
程默 committed
3
import { pageScrollMixin } from '../mixins/page-scroll';
李嘉林 committed
4 5
const ROOT_ELEMENT = '.van-sticky';
VantComponent({
程默 committed
6 7 8 9
  props: {
    zIndex: {
      type: Number,
      value: 99,
李嘉林 committed
10
    },
程默 committed
11 12 13 14
    offsetTop: {
      type: Number,
      value: 0,
      observer: 'onScroll',
李嘉林 committed
15
    },
程默 committed
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
    disabled: {
      type: Boolean,
      observer: 'onScroll',
    },
    container: {
      type: null,
      observer: 'onScroll',
    },
    scrollTop: {
      type: null,
      observer(val) {
        this.onScroll({ scrollTop: val });
      },
    },
  },
  mixins: [
    pageScrollMixin(function (event) {
      if (this.data.scrollTop != null) {
        return;
      }
      this.onScroll(event);
    }),
  ],
  data: {
    height: 0,
    fixed: false,
    transform: 0,
  },
  mounted() {
    this.onScroll();
  },
  methods: {
    onScroll({ scrollTop } = {}) {
      const { container, offsetTop, disabled } = this.data;
      if (disabled) {
        this.setDataAfterDiff({
          fixed: false,
          transform: 0,
        });
        return;
      }
      this.scrollTop = scrollTop || this.scrollTop;
      if (typeof container === 'function') {
        Promise.all([
          getRect(this, ROOT_ELEMENT),
          this.getContainerRect(),
        ]).then(([root, container]) => {
          if (offsetTop + root.height > container.height + container.top) {
            this.setDataAfterDiff({
              fixed: false,
              transform: container.height - root.height,
李嘉林 committed
67
            });
程默 committed
68 69 70 71 72
          } else if (offsetTop >= root.top) {
            this.setDataAfterDiff({
              fixed: true,
              height: root.height,
              transform: 0,
李嘉林 committed
73
            });
程默 committed
74 75 76 77 78 79 80 81 82 83 84 85
          } else {
            this.setDataAfterDiff({ fixed: false, transform: 0 });
          }
        });
        return;
      }
      getRect(this, ROOT_ELEMENT).then((root) => {
        if (offsetTop >= root.top) {
          this.setDataAfterDiff({ fixed: true, height: root.height });
          this.transform = 0;
        } else {
          this.setDataAfterDiff({ fixed: false });
李嘉林 committed
86
        }
程默 committed
87
      });
李嘉林 committed
88
    },
程默 committed
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
    setDataAfterDiff(data) {
      wx.nextTick(() => {
        const diff = Object.keys(data).reduce((prev, key) => {
          if (data[key] !== this.data[key]) {
            prev[key] = data[key];
          }
          return prev;
        }, {});
        this.setData(diff);
        this.$emit('scroll', {
          scrollTop: this.scrollTop,
          isFixed: data.fixed || this.data.fixed,
        });
      });
    },
    getContainerRect() {
      const nodesRef = this.data.container();
      return new Promise((resolve) =>
        nodesRef.boundingClientRect(resolve).exec()
      );
李嘉林 committed
109
    },
程默 committed
110
  },
李嘉林 committed
111
});