lazy-component.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. var __defProp = Object.defineProperty;
  2. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  3. var __getOwnPropNames = Object.getOwnPropertyNames;
  4. var __hasOwnProp = Object.prototype.hasOwnProperty;
  5. var __export = (target, all) => {
  6. for (var name in all)
  7. __defProp(target, name, { get: all[name], enumerable: true });
  8. };
  9. var __copyProps = (to, from, except, desc) => {
  10. if (from && typeof from === "object" || typeof from === "function") {
  11. for (let key of __getOwnPropNames(from))
  12. if (!__hasOwnProp.call(to, key) && key !== except)
  13. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  14. }
  15. return to;
  16. };
  17. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  18. var stdin_exports = {};
  19. __export(stdin_exports, {
  20. default: () => stdin_default
  21. });
  22. module.exports = __toCommonJS(stdin_exports);
  23. var import_vue = require("vue");
  24. var import_use = require("@vant/use");
  25. var stdin_default = (lazy) => ({
  26. props: {
  27. tag: {
  28. type: String,
  29. default: "div"
  30. }
  31. },
  32. emits: ["show"],
  33. render() {
  34. return (0, import_vue.h)(
  35. this.tag,
  36. this.show && this.$slots.default ? this.$slots.default() : null
  37. );
  38. },
  39. data() {
  40. return {
  41. el: null,
  42. state: {
  43. loaded: false
  44. },
  45. show: false
  46. };
  47. },
  48. mounted() {
  49. this.el = this.$el;
  50. lazy.addLazyBox(this);
  51. lazy.lazyLoadHandler();
  52. },
  53. beforeUnmount() {
  54. lazy.removeComponent(this);
  55. },
  56. methods: {
  57. checkInView() {
  58. const rect = (0, import_use.useRect)(this.$el);
  59. return import_use.inBrowser && rect.top < window.innerHeight * lazy.options.preLoad && rect.bottom > 0 && rect.left < window.innerWidth * lazy.options.preLoad && rect.right > 0;
  60. },
  61. load() {
  62. this.show = true;
  63. this.state.loaded = true;
  64. this.$emit("show", this);
  65. },
  66. destroy() {
  67. return this.$destroy;
  68. }
  69. }
  70. });