LarryZhu-dev / autofit.js

autofit.js 迄今为止最易用的自适应工具
https://larryzhu-dev.github.io/autoLarryPages/autofit.js/
MIT License
1.4k stars 146 forks source link

临时解决antd、element等遇到的组件有Popover、dropdown、select下拉错位的问题 #67

Open LarryZhu-dev opened 1 month ago

LarryZhu-dev commented 1 month ago
          > > > antd作为国内比较热门的组件库,能否抽空做一下适配呢,大佬。 现在antd遇到的组件有Popover、dropdown、select下拉

@zhengdechang我暂时没有发现呢

getPopupContainer 挂载一下

elementUI 有办法处理吗

实测element-ui@2.15.14已解决,贴出方案供大家参考:

element-ui/lib/utils/popper.js的第421行下新增代码:

// 修复使用autofit.js导致的popover错位问题
// 这里的body对应的是autofit.init({el: 'body‘})
const targetEl = document.querySelector('body')
const scaleMatch = targetEl.style.getPropertyValue('transform').match(/scale\((.*?)\)/)
// 获取缩放比例
const scale = scaleMatch ? +scaleMatch[1] : 1
Object.keys(referenceOffsets).forEach((key) => {
    referenceOffsets[key] = referenceOffsets[key] / scale
})

刷新页面,元素位置已经是正确的了

因为直接改的源码,需要使用patch-package包把修改以补丁的形式保存起来,并纳入git管理,具体用法自行百度。

其他版本不知道代码位置会不会变,可以自己找找,应该变化不大。贴个图: image

再来记录一下。后来发现在弹框比较贴边时定位还是不对,又经过一番排查,原来是还有一处没改: 在element-ui/lib/utils/popper.js的第564行(上面代码加过后)下新增代码:

// 修复使用autofit.js导致的popover错位问题
// 这里的body对应的是autofit.init({el: 'body‘})
const targetEl = document.querySelector('body')
const scaleMatch = targetEl.style.getPropertyValue('transform').match(/scale\((.*?)\)/)
// 获取缩放比例
const scale = scaleMatch ? +scaleMatch[1] : 1
Object.keys(boundaries).forEach((key) => {
  boundaries[key] = boundaries[key] / scale
})

image

目前看应该没问题了

Originally posted by @wanglei1020 in https://github.com/LarryZhu-dev/autofit.js/issues/55#issuecomment-2263374959

LarryZhu-dev commented 1 month ago

感谢 #55 @wanglei1020

hellorayza commented 1 month ago

弹出层对页面元素显示和定位上一般没有太大影响,是否可以考虑ignore忽略适配? 如果可行的话,一般UI组件会对弹出层组件ID/类名选择器加随机数,是否可以考虑加上规则匹配特性?

LarryZhu-dev commented 1 month ago

弹出层对页面元素显示和定位上一般没有太大影响,是否可以考虑ignore忽略适配? 如果可行的话,一般UI组件会对弹出层组件ID/类名选择器加随机数,是否可以考虑加上规则匹配特性?

ignore不可行哈

zhangjingyu1024 commented 1 week ago

antd也有这个问题,感觉还是不要动依赖包的内容了,可以打补丁,下面是我针对antdv写的补丁测试还ok,其他组件库应该也可以根据类名改改: image

使用: image

LarryZhu-dev commented 1 week ago

@zhangjingyu1024 感谢提供思路

1liza commented 1 week ago

antd也有这个问题,感觉还是不要动依赖包的内容了,可以打补丁,下面是我针对antdv写的补丁测试还ok,其他组件库应该也可以根据类名改改: image

使用: image

我也遇到这个问题,但是不太会写,能提供下完整外码嘛,非常感谢!!!

zhangjingyu1024 commented 1 week ago

antd也有这个问题,感觉还是不要动依赖包的内容了,可以打补丁,下面是我针对antdv写的补丁测试还ok,其他组件库应该也可以根据类名改改: image 使用: image

我也遇到这个问题,但是不太会写,能提供下完整外码嘛,非常感谢!!!

autofitfix.zip

1liza commented 1 week ago

antd也有这个问题,感觉还是不要动依赖包的内容了,可以打补丁,下面是我针对antdv写的补丁测试还ok,其他组件库应该也可以根据类名改改: image 使用: image

我也遇到这个问题,但是不太会写,能提供下完整外码嘛,非常感谢!!!

autofitfix.zip

非常感谢!!!