目前问题就是放大div后 滚动内容 会出现左侧上侧的内容缺失问题 如果加上transform-origin :top left 缺失问题可以解决 但是效果不好 通过监测放大后手动调整显示区域是可以 但是也会存在抖动问题
<div style="width: 600px;height:600px; overflow: auto;"><div style="transform: scale(4);"><img src="https://img1.baidu.com/it/u=2494940079,2555791724&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=725" alt="" style=" top: 0px; left: 0px; object-fit: cover;" crossorigin="anonymous"></div></div>
使用CSS的clip属性来定义可视区域,避免出现缺失问题。例如:
.container {
width: 600px;
height: 600px;
overflow: auto;
position: relative;
}
.content {
position: absolute;
top: 0;
left: 0;
clip: rect(0px, 600px, 600px, 0px);
transform: scale(4);
}
<div class="container">
<div class="content">
<img src="https://img1.baidu.com/it/u=2494940079,2555791724&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=725" alt="">
</div>
</div>
使用JavaScript来动态计算可视区域的位置和大小,避免出现缺失和抖动问题。例如:
const container = document.querySelector('.container');
const content = document.querySelector('.content');
const img = document.querySelector('img');
const scale = 4;
const width = img.naturalWidth;
const height = img.naturalHeight;
container.style.width = `${width}px`;
container.style.height = `${height}px`;
content.style.transform = `scale(${scale})`;
function updateClip() {
const rect = container.getBoundingClientRect();
const clipX = -rect.left / scale;
const clipY = -rect.top / scale;
const clipWidth = rect.width / scale;
const clipHeight = rect.height / scale;
content.style.clip = `rect(${clipY}px, ${clipX + clipWidth}px, ${clipY + clipHeight}px, ${clipX}px)`;
}
updateClip();
container.addEventListener('scroll', updateClip);
window.addEventListener('resize', updateClip);
这个例子中,我们使用了getBoundingClientRect()方法来获取容器的位置和大小,然后根据缩放比例计算出可视区域的位置和大小,并设置clip属性来定义可视区域。同时,我们还绑定了scroll和resize事件,实时更新可视区域的位置和大小,避免出现抖动和缺失问题。