[英]Scale rounded image on hover
我正在尝试使用带有边框的“圆形”图像,当用户将鼠标悬停在图像上方时,内部的图像即为边框缩放。 为什么似乎出现故障? 有人可以告诉我怎么回事吗?
HTML
<div class="wrapper">
<div class="portfolio-item">
<div class="portfolio-item-preview">
<img src="https://i.imgur.com/aLJnBtV.jpg" alt="">
</div>
</div>
</div>
CSS
.wrapper {
background-color: #FFF;
border: 1px solid #EDEDED;
padding: 8px;
width:200px;
border-radius:50%;
}
.portfolio-item {
overflow: hidden;
border-radius:50%;
}
.portfolio-item-preview {
position: relative;
}
img {
max-width: 100%;
height: auto;
}
.portfolio-item:hover .portfolio-item-preview {
-webkit-transform: scale(1.1);
-webkit-transition: all 0.125s ease-in-out 0s;
-moz-transition: all 0.125s ease-in-out 0s;
-ms-transition: all 0.125s ease-in-out 0s;
-o-transition: all 0.125s ease-in-out 0s;
transition: all 0.125s ease-in-out 0s;
}
边框半径不是可设置动画的属性,因此它不会随图像缩放。 仅在过渡结束时才进行缩放。 http://www.w3.org/TR/css3-transitions/#properties-from-css-
如果要在缩放时保持边框,则可以在图像上放置带有边框和框阴影(框阴影将作为实际的灰色边框)的透明圆。 这样,您基本上会有一个显示图像的小窗口。
#container{
position:relative;
display:inline-block;
}
#circle{
z-index:2;
position:absolute;
top:0;
left:0;
border-radius:50%;
height:200px;
width:200px;
border:20px solid white;
box-shadow:0 0 2px #666;
}
img {
border-radius:50%;
width:200px;
z-index:1;
position:absolute;
top:20px;
left:20px;
}
#container:hover img{
-webkit-transform: scale(1.1);
-webkit-transition: all 0.125s ease-in-out 0s;
-moz-transition: all 0.125s ease-in-out 0s;
-ms-transition: all 0.125s ease-in-out 0s;
-o-transition: all 0.125s ease-in-out 0s;
transition: all 0.125s ease-in-out 0s;
}
这是我的小提琴: http : //jsfiddle.net/a05or1uw/
您可以将border-radius应用于可scale
的项目:
.portfolio-item-preview {
overflow: hidden;
border-radius:50%;
position: relative;
-webkit-transition: all 0.125s ease-in-out 0s;
-moz-transition: all 0.125s ease-in-out 0s;
-ms-transition: all 0.125s ease-in-out 0s;
-o-transition: all 0.125s ease-in-out 0s;
transition: all 0.125s ease-in-out 0s;
}
检查此DemoFiddle
这是基于user3765149的答案的小提琴。
我添加了一个边框元素,该边框元素的大小可以与图像缩放比例无关,而圆形元素仅充当蒙版。 它只需要一点数学就可以正确调整它。
<div id="container">
<div id="border"> </div>
<div id="circle"> </div>
<img src="https://i.imgur.com/aLJnBtV.jpg" alt=""/>
</div>
CSS:
#container{
position:relative;
display:inline-block;
}
#border{
z-index:3;
position:absolute;
top:6px;
left:6px;
border-radius:50%;
height:206px;
width:206px;
border:1px solid #dedede;
}
#circle{
z-index:2;
position:absolute;
top:0;
left:0;
border-radius:50%;
height:200px;
width:200px;
border:10px solid white;
}
img {
z-index:1;
border-radius:50%;
width:200px;
position:absolute;
top:10px;
left:10px;
transform: scale(1);
transition: all 0.15s ease-out 0s;
}
#container:hover img{
transform: scale(1.08);
transition: all 0.15s ease-out 0s;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.