onresizeMixins.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import { debounce } from 'lodash';
  2. export const onresizeHandler = {
  3. data() {
  4. return {
  5. docmHeight: this.getDocumentHeight(),
  6. showHeight: this.getDocumentHeight(),
  7. isBtn: true // 是否显示隐藏保存提交按钮(演练登记&教育培训)
  8. }
  9. },
  10. created() {},
  11. watch: {
  12. // 监听显示高度
  13. showHeight: function() {
  14. this.updateButtonVisibility();
  15. }
  16. },
  17. mounted() {
  18. // 添加事件监听器,并使用防抖处理函数
  19. window.addEventListener('resize', this.handleResize);
  20. },
  21. beforeDestroy() {
  22. // 移除事件监听器,避免内存泄漏
  23. window.removeEventListener('resize', this.handleResize);
  24. },
  25. methods: {
  26. handleResize: debounce(function() {
  27. this.showHeight = this.getDocumentHeight();
  28. }, 100), // 使用防抖,确保不频繁调用
  29. updateButtonVisibility() {
  30. this.isBtn = this.docmHeight <= this.showHeight;
  31. },
  32. // 获取文档高度的工具函数
  33. getDocumentHeight() {
  34. return document.documentElement.clientHeight || document.body.clientHeight;
  35. }
  36. }
  37. }