123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- "use strict";
- Object.defineProperty(exports, "__esModule", { value: true });
- var component_1 = require("../common/component");
- var ROOT_ELEMENT = '.van-sticky';
- component_1.VantComponent({
- props: {
- zIndex: {
- type: Number,
- value: 99
- },
- offsetTop: {
- type: Number,
- value: 0,
- observer: 'observeContent'
- },
- disabled: {
- type: Boolean,
- observer: function (value) {
- if (!this.mounted) {
- return;
- }
- value ? this.disconnectObserver() : this.initObserver();
- }
- },
- container: {
- type: null,
- observer: function (target) {
- if (typeof target !== 'function' || !this.data.height) {
- return;
- }
- this.observeContainer();
- this.updateFixed();
- }
- }
- },
- data: {
- height: 0,
- fixed: false
- },
- methods: {
- getContainerRect: function () {
- var nodesRef = this.data.container();
- return new Promise(function (resolve) {
- return nodesRef.boundingClientRect(resolve).exec();
- });
- },
- initObserver: function () {
- var _this = this;
- this.disconnectObserver();
- this.getRect(ROOT_ELEMENT).then(function (rect) {
- _this.setData({ height: rect.height });
- wx.nextTick(function () {
- _this.observeContent();
- _this.observeContainer();
- });
- });
- },
- updateFixed: function () {
- var _this = this;
- Promise.all([this.getRect(ROOT_ELEMENT), this.getContainerRect()]).then(function (_a) {
- var content = _a[0], container = _a[1];
- _this.setData({ height: content.height });
- _this.containerHeight = container.height;
- wx.nextTick(function () {
- _this.setFixed(content.top);
- });
- });
- },
- disconnectObserver: function (observerName) {
- if (observerName) {
- var observer = this[observerName];
- observer && observer.disconnect();
- }
- else {
- this.contentObserver && this.contentObserver.disconnect();
- this.containerObserver && this.containerObserver.disconnect();
- }
- },
- observeContent: function () {
- var _this = this;
- var offsetTop = this.data.offsetTop;
- this.disconnectObserver('contentObserver');
- var contentObserver = this.createIntersectionObserver({
- thresholds: [0.9, 1]
- });
- contentObserver.relativeToViewport({ top: -offsetTop });
- contentObserver.observe(ROOT_ELEMENT, function (res) {
- if (_this.data.disabled) {
- return;
- }
- _this.setFixed(res.boundingClientRect.top);
- });
- this.contentObserver = contentObserver;
- },
- observeContainer: function () {
- var _this = this;
- if (typeof this.data.container !== 'function') {
- return;
- }
- var height = this.data.height;
- this.getContainerRect().then(function (rect) {
- _this.containerHeight = rect.height;
- _this.disconnectObserver('containerObserver');
- var containerObserver = _this.createIntersectionObserver({
- thresholds: [0.9, 1]
- });
- _this.containerObserver = containerObserver;
- containerObserver.relativeToViewport({
- top: _this.containerHeight - height
- });
- containerObserver.observe(ROOT_ELEMENT, function (res) {
- if (_this.data.disabled) {
- return;
- }
- _this.setFixed(res.boundingClientRect.top);
- });
- });
- },
- setFixed: function (top) {
- var _a = this.data, offsetTop = _a.offsetTop, height = _a.height;
- var containerHeight = this.containerHeight;
- var fixed = containerHeight && height
- ? top >= height - containerHeight && top < offsetTop
- : top < offsetTop;
- this.$emit('scroll', {
- scrollTop: top,
- isFixed: fixed
- });
- this.setData({ fixed: fixed });
- }
- },
- mounted: function () {
- this.mounted = true;
- if (!this.data.disabled) {
- this.initObserver();
- }
- },
- destroyed: function () {
- this.disconnectObserver();
- }
- });
|