html - 如何使单独 div 中的所有图像停止溢出?
问题描述
我在网页的右上角有一张使用 flexbox 的图片。在鼠标悬停期间,图像溢出到左侧的图像上。我想阻止这种情况。此外,如果有一种方法可以使图像缩小一点。任何建议表示赞赏。
我试过溢出:隐藏,它似乎适用于每个图像,但右上角。
这是一个查看动画的codepen示例https://codepen.io/dev20tx/pen/zXgRQj
.grid-item-1, .grid-item-2, .grid-item-3, .grid-item-4 {
overflow: hidden;
width: 50%;
height: auto;
z-index: 0;
}
.grid-item-1, .grid-item-2, .grid-item-3, .grid-item-4 img {
-webkit-transition: all 1s ease; /* Safari and Chrome */
-moz-transition: all 1s ease; /* Firefox */
-ms-transition: all 1s ease; /* IE 9 */
-o-transition: all 1s ease; /* Opera */
transition: all 1s ease;
}
.grid-item-1:hover, .grid-item-2:hover, .grid-item-3:hover, .grid-
item-4:hover img {
-webkit-transform:scale(1.08); /* Safari and Chrome */
-moz-transform:scale(1.08); /* Firefox */
-ms-transform:scale(1.08); /* IE 9 */
-o-transform:scale(1.08); /* Opera */
transform:scale(1.08);
}
.work {
height: 50vh;
display: flex;
overflow: hidden;
}
.contact {
height: 50vh;
display: flex;
overflow: hidden;
align-items: center;
}
<!DOCTYPE html>
<html>
<head>
<title>TEST</title>
<link rel="stylesheet" type="text/css" href="test.css">
</head>
<body>
<div class="work">
<div class="grid-item grid-item-1"><img
src="https://images.pexels.com/photos/1803024/pexels-photo-
1803024.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260"></div>
<div class="grid-item grid-item-2"><img
src="https://images.pexels.com/photos/1555313/pexels-photo-
1555313.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260"></div>
</div>
<div class="contact">
<div class="grid-item grid-item-3"><img
src="https://images.pexels.com/photos/300857/pexels-photo-
300857.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260"></div>
<div class="grid-item grid-item-4"><img
src="https://images.pexels.com/photos/2062080/pexels-photo-
2062080.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260"></div>
</div>
</body>
</html>
解决方案
您的图像非常重叠,所以我基本上将图像添加到背景属性中,希望对您有所帮助。
解决方案:
.grid-item {
overflow: hidden;
/* [1.2] Hide the overflowing of child elements */
width: 50vw;
height: 50%;
z-index: 0;
}
.grid-item-1 {
background-image: url('https://images.pexels.com/photos/1803024/pexels-photo-1803024.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260');
height: 100%;
width: 100%;
background-position: center;
background-size: cover;
transition: all .5s;
}
.grid-item-2 {
background-image: url('https://images.pexels.com/photos/2062080/pexels-photo-2062080.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260');
height: 100%;
width: 100%;
background-position: center;
background-size: cover;
transition: all .5s;
}
.grid-item-3 {
background-image: url('https://images.pexels.com/photos/1555313/pexels-photo-1555313.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260');
height: 100%;
width: 100%;
background-position: center;
background-size: cover;
transition: all .5s;
}
.grid-item-4 {
background-image: url('https://images.pexels.com/photos/300857/pexels-photo-300857.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260');
height: 100%;
width: 100%;
background-position: center;
background-size: cover;
transition: all .5s;
}
.container {
display: flex;
height: 100vh;
width: 100vw;
flex-flow: wrap;
}
.grid-item:hover .grid-item-1,
.grid-item:hover .grid-item-2,
.grid-item:hover .grid-item-3,
.grid-item:hover .grid-item-4 {
transform: scale(1.2);
}
<div class="container">
<div class="grid-item">
<div class="grid-item-1">
</div>
</div>
<div class="grid-item">
<div class="grid-item-2">
</div>
</div>
<div class="grid-item">
<div class="grid-item-3">
</div>
</div>
<div class="grid-item">
<div class="grid-item-4">
</div>
</div>
</div>
推荐阅读
- python - 在 Python 中迭代 CSV 阅读器对象
- menu - ICP4D - 已安装但无法创建新项目
- vega-lite - 当图例中使用的查找中链接的字段时,Vega-lite 图表失败
- php - PHP::Imagick 从合成图像创建临时文件,用于通过标签显示
- azerothcore - 艾泽拉斯核,从改变模型到改变种族的欺骗之球
- python - 在 NiFi Container 中添加额外的 python 模块
- firebase - 如何在颤动中将数据从一个小部件传递到另一个小部件?
- reactjs - 反应传单加载 1000 个标记
- python - 将天转换为年、月和日
- reactjs - 无法在reactjs中获取下拉值