index.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // 引入拖拽js
  2. import { startDrag } from './drag.js'
  3. /**
  4. * 为el-dialog弹框增加拖拽功能
  5. * @param {*} el 指定dom
  6. * @param {*} binding 绑定对象
  7. * desc 只要用到了el-dialog的组件,都可以通过增加v-draggable属性变为可拖拽的弹框
  8. */
  9. const draggable = (el, binding) => {
  10. // 绑定拖拽事件 [绑定拖拽触发元素为弹框头部、拖拽移动元素为整个弹框]
  11. startDrag(el.querySelector('.el-dialog__header'), el.querySelector('.el-dialog'), binding.value);
  12. };
  13. //下拉框懒加载
  14. const SelectLazyLoading={
  15. inserted(el,binding){
  16. let SELECT_DOM = el.querySelector(".el-select-dropdown .el-select-dropdown__wrap") || el.querySelector('.el-autocomplete-suggestion__wrap') ;
  17. // console.log(SELECT_DOM)
  18. if(!SELECT_DOM){
  19. return false
  20. }
  21. SELECT_DOM.addEventListener("scroll", function () {
  22. // scrollHeight:当前所有选项的高度
  23. // scrollTop:滚动的距离
  24. // clientHeight:下拉框的高度
  25. // console.log(SELECT_DOM,this.scrollHeight , this.scrollTop , this.clientHeight,this.scrollHeight - this.scrollTop - 1 <= this.clientHeight)
  26. let condition = this.scrollHeight - this.scrollTop - 1 <= this.clientHeight;
  27. if (condition) {
  28. binding.value();
  29. }
  30. });
  31. }
  32. }
  33. //块禁止样式
  34. const disabled = (el,binding)=>{
  35. if(binding.value){
  36. // el.style.pointerEvents = 'none';
  37. // el.style.cursor='not-allowed';
  38. el.classList.add('pointerEvents')
  39. if(el.parentNode){
  40. // el.parentNode.style.cursor='not-allowed';
  41. // el.parentNode.style.pointerEvents='none';
  42. el.parentNode.classList.add('disabled')
  43. }
  44. el.style.color='#bbb'
  45. }else{
  46. // el.style.pointerEvents = '';
  47. // el.style.cursor='';
  48. el.classList.remove('pointerEvents')
  49. if(el.parentNode){
  50. el.parentNode.classList.remove('disabled')
  51. // el.parentNode.style.cursor='';
  52. // el.parentNode.style.pointerEvents='';
  53. }
  54. el.style.color=''
  55. }
  56. }
  57. //获取高度
  58. const DivHeight={
  59. inserted(el,binding){
  60. if(el){
  61. let height = window.innerHeight
  62. binding.value(el.offsetHeight - 10)
  63. window.addEventListener('resize',()=>{
  64. let newHeight = window.innerHeight;
  65. if (newHeight !== height) {
  66. binding.value(el.offsetHeight - 10)
  67. height = newHeight;
  68. }
  69. })
  70. }
  71. },
  72. unbind(el,binding){
  73. if(el){
  74. window.removeEventListener('resize',()=>{
  75. binding.value(el.offsetHeight - 10)
  76. })
  77. }
  78. },
  79. }
  80. //等高
  81. const equal_heights = {
  82. inserted(el, binding,vnode) {
  83. var that = vnode.context
  84. // 获取另一个容器的引用
  85. const otherContainerSelector = binding.value;
  86. that.$nextTick(()=>{
  87. const otherContainer = that.$refs[otherContainerSelector];
  88. if (!otherContainer) {
  89. console.error('Unable to find the other container');
  90. return;
  91. }
  92. window.addEventListener("resize",that.$commonJS.debounce(()=>{
  93. const children = el.children;
  94. const otherChildren = otherContainer.children;
  95. for (let i = 0; i < children.length && i < otherChildren.length; i++) {
  96. children[i].style.height = 'auto';
  97. otherChildren[i].style.height = 'auto';
  98. const maxHeight = Math.max(children[i].offsetHeight, otherChildren[i].offsetHeight);
  99. children[i].style.height = maxHeight + 'px';
  100. otherChildren[i].style.height = maxHeight + 'px';
  101. otherChildren[i].style.background = '#f3f4f8';
  102. }
  103. },500))
  104. // 遍历当前容器和另一个容器的子div,并设置对应索引的高度
  105. const children = el.children;
  106. const otherChildren = otherContainer.children;
  107. for (let i = 0; i < children.length && i < otherChildren.length; i++) {
  108. const maxHeight = Math.max(children[i].offsetHeight, otherChildren[i].offsetHeight);
  109. children[i].style.height = maxHeight + 'px';
  110. otherChildren[i].style.height = maxHeight + 'px';
  111. otherChildren[i].style.background = '#f3f4f8';
  112. }
  113. })
  114. },
  115. // 如果需要,可以在更新时再次执行逻辑
  116. componentUpdated(el, binding,vnode) {
  117. // 这里可能需要额外的逻辑来处理更新后的高度同步
  118. var that = vnode.context
  119. // 获取另一个容器的引用
  120. const otherContainerSelector = binding.value;
  121. that.$nextTick(()=>{
  122. const otherContainer = that.$refs[otherContainerSelector];
  123. if (!otherContainer) {
  124. console.error('Unable to find the other container');
  125. return;
  126. }
  127. // 遍历当前容器和另一个容器的子div,并设置对应索引的高度
  128. const children = el.children;
  129. const otherChildren = otherContainer.children;
  130. for (let i = 0; i < children.length && i < otherChildren.length; i++) {
  131. const maxHeight = Math.max(children[i].offsetHeight, otherChildren[i].offsetHeight);
  132. children[i].style.height = maxHeight + 'px';
  133. otherChildren[i].style.height = maxHeight + 'px';
  134. otherChildren[i].style.background = '#f3f4f8';
  135. }
  136. })
  137. },
  138. unbind(el) {
  139. var eventListener = window.eventListeners_
  140. if(!eventListener){
  141. return false
  142. }
  143. const listeners = eventListener.resize
  144. if(listeners){
  145. window.removeEventListener("resize");
  146. }
  147. },
  148. }
  149. const directives = {
  150. draggable,
  151. SelectLazyLoading,
  152. disabled,
  153. DivHeight,
  154. equal_heights
  155. };
  156. // 这种写法可以批量注册指令
  157. export default {
  158. install(Vue) {
  159. Object.keys(directives).forEach((key) => {
  160. Vue.directive(key, directives[key]);
  161. });
  162. },
  163. };