我正在制作一个画廊,我希望每张照片在您像这样单击时全屏显示:
目前,我在每张图片上都有一个点击处理程序,为点击的图片添加一个类 zoom
。我编写的 CSS 选择器只会将图像炸毁,并没有像示例中那样将它放在整个页面的中心。这是我的代码:
.gallery img {
width: 100%;
height: 100%;
object-fit: cover;
align-content: center;
object-position: center;
transition: transform ease-in-out 0.5s;
}
.zoom {
z-index: 1;
transform: scale(1.5);
display: block;
margin-left: auto;
margin-right: auto;
}
<body onload="getPics()">
<div class="container">
<h1>Photo Gallery</h1>
<div class="gallery">
<img
src="https://images.unsplash.com/photo-1543517515-d6ff15a98642?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&ixid=eyJhcHBfaWQiOjQ0NDc3fQ&s=493802e93ec426171750ac2291e2867e"
/><img
src="https://images.unsplash.com/photo-1543517515-d6ff15a98642?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&ixid=eyJhcHBfaWQiOjQ0NDc3fQ&s=493802e93ec426171750ac2291e2867e"
/><img
src="https://images.unsplash.com/photo-1543517515-d6ff15a98642?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&ixid=eyJhcHBfaWQiOjQ0NDc3fQ&s=493802e93ec426171750ac2291e2867e"
/>
</div>
</div>
</body>
如何更改 zoom
CSS 以使图像进入全屏模式?
答案 0 :(得分:1)
我建议看一下intensejs库:https://github.com/tholman/intense-images
它的实施既快速又简单,将满足您的需求。
答案 1 :(得分:1)
使用缩放很难做到这一点 - 您必须根据当前视口等尺寸进行计算并重新定位图像。
另一种方法是让一个元素填充整个视口并具有高 z-index 但通常不显示。当用户点击图像时,它被设置为这个大元素的背景,背景大小包含所以它总是完全适合。
在此代码段中,单击放大的图像可以消除它。
function getPics() {} //just for this demo
const imgs = document.querySelectorAll('.gallery img');
const fullPage = document.querySelector('#fullpage');
imgs.forEach(img => {
img.addEventListener('click', function() {
fullPage.style.backgroundImage = 'url(' + img.src + ')';
fullPage.style.display = 'block';
});
});
#fullpage {
display: none;
position: absolute;
z-index: 9999;
top: 0;
left: 0;
width: 100vw;
height: 100vh;
background-size: contain;
background-repeat: no-repeat no-repeat;
background-position: center center;
background-color: black;
}
<body onload="getPics()">
<div class="container">
<h1>Photo Gallery</h1>
<div class="gallery">
<img src="https://images.unsplash.com/photo-1543517515-d6ff15a98642?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&ixid=eyJhcHBfaWQiOjQ0NDc3fQ&s=493802e93ec426171750ac2291e2867e" /><img src="https://images.unsplash.com/photo-1543517515-d6ff15a98642?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&ixid=eyJhcHBfaWQiOjQ0NDc3fQ&s=493802e93ec426171750ac2291e2867e"
/><img src="https://images.unsplash.com/photo-1543517515-d6ff15a98642?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&ixid=eyJhcHBfaWQiOjQ0NDc3fQ&s=493802e93ec426171750ac2291e2867e" />
</div>
</div>
<div id="fullpage" onclick="this.style.display='none';"></div>