index.js 3.38 KB
var t = function() {
    function t(t, e) {
        var r = [], n = !0, i = !1, o = void 0;
        try {
            for (var a, f = t[Symbol.iterator](); !(n = (a = f.next()).done) && (r.push(a.value), 
            !e || r.length !== e); n = !0) ;
        } catch (t) {
            t = VM2_INTERNAL_STATE_DO_NOT_USE_OR_PROGRAM_WILL_FAIL.handleException(t);
            i = !0, o = t;
        } finally {
            try {
                !n && f.return && f.return();
            } finally {
                if (i) throw o;
            }
        }
        return r;
    }
    return function(e, r) {
        if (Array.isArray(e)) return e;
        if (Symbol.iterator in Object(e)) return t(e, r);
        throw new TypeError("Invalid attempt to destructure non-iterable instance");
    };
}(), e = require("../common/component"), r = require("../mixins/page-scroll");

(0, e.VantComponent)({
    props: {
        zIndex: {
            type: Number,
            value: 99
        },
        offsetTop: {
            type: Number,
            value: 0,
            observer: "onScroll"
        },
        disabled: {
            type: Boolean,
            observer: "onScroll"
        },
        container: {
            type: null,
            observer: "onScroll"
        }
    },
    mixins: [ (0, r.pageScrollMixin)(function(t) {
        this.onScroll(t);
    }) ],
    data: {
        height: 0,
        fixed: !1,
        transform: 0
    },
    mounted: function() {
        this.onScroll();
    },
    methods: {
        onScroll: function() {
            var e = this, r = (arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : {}).scrollTop, n = this.data, i = n.container, o = n.offsetTop;
            n.disabled ? this.setDataAfterDiff({
                fixed: !1,
                transform: 0
            }) : (this.scrollTop = r || this.scrollTop, "function" != typeof i ? this.getRect(".van-sticky").then(function(t) {
                o >= t.top ? (e.setDataAfterDiff({
                    fixed: !0,
                    height: t.height
                }), e.transform = 0) : e.setDataAfterDiff({
                    fixed: !1
                });
            }) : Promise.all([ this.getRect(".van-sticky"), this.getContainerRect() ]).then(function(r) {
                var n = t(r, 2), i = n[0], a = n[1];
                o + i.height > a.height + a.top ? e.setDataAfterDiff({
                    fixed: !1,
                    transform: a.height - i.height
                }) : o >= i.top ? e.setDataAfterDiff({
                    fixed: !0,
                    height: i.height,
                    transform: 0
                }) : e.setDataAfterDiff({
                    fixed: !1,
                    transform: 0
                });
            }));
        },
        setDataAfterDiff: function(t) {
            var e = this;
            wx.nextTick(function() {
                var r = Object.keys(t).reduce(function(r, n) {
                    return t[n] !== e.data[n] && (r[n] = t[n]), r;
                }, {});
                e.setData(r), e.$emit("scroll", {
                    scrollTop: e.scrollTop,
                    isFixed: t.fixed || e.data.fixed
                });
            });
        },
        getContainerRect: function() {
            var t = this.data.container();
            return new Promise(function(e) {
                return t.boundingClientRect(e).exec();
            });
        }
    }
});