【发布时间】:2021-12-28 04:45:03
【问题描述】:
有一个网站页面,其高度和宽度等于浏览器视口的 100%(100vw 和 100vh)。
在页面中心绝对定位的包装内有一张高质量的图片 (3000x2857px)。图像包装器具有以像素为单位的最大宽度(例如 300 像素)。
此外,图像具有叠加层。我通过单击叠加将.active 类添加到图像包装器中。此类使图像包装器的最大宽度等于 100vw。
所以,我想对此进行动画处理。我添加了对max-width、top、transform 属性的转换,但它很滞后。我知道这是由于浏览器的大量计算导致的top 和max-width 属性。但我不知道如何以其他方式做到这一点。
欢迎任何帮助!
附:当我写这个问题时,我发现这个实现在 Safari 中是错误的。我认为转换转换在这个浏览器中不起作用,所以如果你建议在其中工作的代码会很棒:(
演示:https://codepen.io/ghettojezuz/pen/ExvGwJB
const imageWrapper = document.getElementById("image-wrapper");
const overlay = document.getElementById("overlay");
function toggleImageWrapper() {
if (imageWrapper.classList.contains('active')) {
imageWrapper.classList.remove('active');
} else {
imageWrapper.classList.add('active');
}
}
overlay.addEventListener("click", () => {
toggleImageWrapper();
})
body {
min-height: 100vh;
overflow-x: hidden;
}
img {
width: 100%;
height: auto;
}
.wrapper {
width: 100vw;
min-height: 100vh;
height: 100%;
}
.image-wrapper {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
max-width: 300px;
width: 100%;
transition: max-width .8s ease, top .8s ease, transform .8s ease;
}
.image-wrapper.active {
max-width: 100vw;
left: 50%;
top: 0;
transform: translate(-50%, 0);
}
.image-overlay {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: transparent;
opacity: .7;
transition: background-color .3s ease;
cursor: pointer;
}
.image-overlay:hover {
background-color: #000000;
}
<div class="wrapper">
<div class="image-wrapper" id="image-wrapper">
<img src="https://via.placeholder.com/3000x2857.webp" alt="">
<div class="image-overlay" id="overlay"></div>
</div>
</div>
【问题讨论】:
标签: javascript html css animation css-transitions