tdesign-vue
Version:
141 lines (137 loc) • 5.99 kB
JavaScript
/**
* tdesign v1.14.1
* (c) 2025 tdesign
* @license MIT
*/
import _defineProperty from '@babel/runtime/helpers/defineProperty';
import Vue from 'vue';
import { isFunction } from 'lodash-es';
import { getScrollContainer, on, off } from '../utils/dom.js';
import affixProps from './props.js';
import { getClassPrefixMixins } from '../config-provider/config-receiver.js';
import mixins from '../utils/mixins.js';
import 'raf';
import '../utils/easing.js';
import '../config-provider/context.js';
import '../_common/js/global-config/default-config.js';
import '../_common/js/global-config/locale/zh_CN.js';
import '../_chunks/dep-ba613a02.js';
import '@babel/runtime/helpers/typeof';
import '../_chunks/dep-fdb1b253.js';
import 'dayjs';
import '../_common/js/global-config/t.js';
import '@babel/runtime/helpers/slicedToArray';
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
var classMixins = getClassPrefixMixins("affix");
var _Affix = mixins(Vue, classMixins).extend({
name: "TAffix",
props: _objectSpread({}, affixProps),
watch: {
offsetTop: function offsetTop() {
this.handleScroll();
},
offsetBottom: function offsetBottom() {
this.handleScroll();
},
zIndex: function zIndex() {
this.handleScroll();
}
},
methods: {
handleScroll: function handleScroll() {
var _this = this;
var scrollContainer = this.scrollContainer,
offsetTop = this.offsetTop,
offsetBottom = this.offsetBottom;
var _this$$refs = this.$refs,
affixWrapRef = _this$$refs.affixWrapRef,
affixRef = _this$$refs.affixRef;
if (!this.ticking) {
window.requestAnimationFrame(function () {
var _affixWrapRef$getBoun;
var _ref = (_affixWrapRef$getBoun = affixWrapRef === null || affixWrapRef === void 0 ? void 0 : affixWrapRef.getBoundingClientRect()) !== null && _affixWrapRef$getBoun !== void 0 ? _affixWrapRef$getBoun : {
top: 0,
width: 0,
height: 0
},
wrapToTop = _ref.top,
wrapWidth = _ref.width,
wrapHeight = _ref.height;
var containerTop = 0;
if (scrollContainer instanceof HTMLElement) {
var _scrollContainer$getB;
containerTop = scrollContainer === null || scrollContainer === void 0 || (_scrollContainer$getB = scrollContainer.getBoundingClientRect) === null || _scrollContainer$getB === void 0 ? void 0 : _scrollContainer$getB.call(scrollContainer).top;
}
var fixedTop;
var calcTop = wrapToTop - containerTop;
var containerHeight = (scrollContainer === null || scrollContainer === void 0 ? void 0 : scrollContainer[scrollContainer instanceof Window ? "innerHeight" : "clientHeight"]) - wrapHeight;
var calcBottom = containerTop + containerHeight - offsetBottom;
if (offsetTop !== void 0 && calcTop <= offsetTop) {
fixedTop = containerTop + offsetTop;
} else if (offsetBottom !== void 0 && wrapToTop >= calcBottom) {
fixedTop = calcBottom;
} else {
fixedTop = false;
}
if (affixRef) {
var affixed = fixedTop !== false;
var placeholderStatus = affixWrapRef.contains(_this.placeholderEL);
if (affixed) {
affixRef.className = _this.componentName;
affixRef.style.top = "".concat(fixedTop, "px");
affixRef.style.width = "".concat(wrapWidth, "px");
affixRef.style.height = "".concat(wrapHeight, "px");
if (_this.zIndex) {
affixRef.style.zIndex = "".concat(_this.zIndex);
}
if (!placeholderStatus) {
_this.placeholderEL.style.width = "".concat(wrapWidth, "px");
_this.placeholderEL.style.height = "".concat(wrapHeight, "px");
affixWrapRef.appendChild(_this.placeholderEL);
}
} else {
affixRef.removeAttribute("class");
affixRef.removeAttribute("style");
placeholderStatus && _this.placeholderEL.remove();
}
_this.$emit("fixedChange", affixed, {
top: fixedTop
});
if (isFunction(_this.onFixedChange)) _this.onFixedChange(affixed, {
top: Number(fixedTop)
});
}
_this.ticking = false;
});
this.ticking = true;
}
}
},
mounted: function mounted() {
var _this2 = this;
this.placeholderEL = document.createElement("div");
var timer = setTimeout(function () {
_this2.scrollContainer = getScrollContainer(_this2.container);
_this2.handleScroll();
on(_this2.scrollContainer, "scroll", _this2.handleScroll);
on(window, "resize", _this2.handleScroll);
clearTimeout(timer);
}, 0);
},
destroyed: function destroyed() {
if (!this.scrollContainer) return;
off(this.scrollContainer, "scroll", this.handleScroll);
off(window, "resize", this.handleScroll);
},
render: function render() {
var h = arguments[0];
return h("div", {
"ref": "affixWrapRef"
}, [h("div", {
"ref": "affixRef"
}, [this.$slots["default"]])]);
}
});
export { _Affix as default };
//# sourceMappingURL=affix.js.map