简体   繁体   English

在悬停时缩放圆形图像

[英]Scale rounded image on hover

I am attempting to have a 'circular' image with a border, when the user hovers over the image the image inside is the border is scaled. 我正在尝试使用带有边框的“圆形”图像,当用户将鼠标悬停在图像上方时,内部的图像即为边框缩放。 Why does it appear to be glitching? 为什么似乎出现故障? can someone tell me whats going on? 有人可以告诉我怎么回事吗?

HTML 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 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;
}

JSFiddle 的jsfiddle

Border-radius is not an animatable property, so it doesn't scale with the image. 边框半径不是可设置动画的属性,因此它不会随图像缩放。 It only gets scaled when the transition ends. 仅在过渡结束时才进行缩放。 http://www.w3.org/TR/css3-transitions/#properties-from-css- http://www.w3.org/TR/css3-transitions/#properties-from-css-

If you want to maintain the border while scaling, then you can position a transparent circle with a border and box shadow (box shadow will act as the actual grey border) over the image. 如果要在缩放时保持边框,则可以在图像上放置带有边框和框阴影(框阴影将作为实际的灰色边框)的透明圆。 This way you basically have a little window showing the image. 这样,您基本上会有一个显示图像的小窗口。

#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;
}

Here is my fiddle: http://jsfiddle.net/a05or1uw/ 这是我的小提琴: http : //jsfiddle.net/a05or1uw/

You could apply the border-radius on the item that scale : 您可以将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;
}

Check this DemoFiddle 检查此DemoFiddle

Here's a fiddle based on user3765149's answer. 这是基于user3765149的答案的小提琴。

I added a border element that can be dimensioned independently of the image scaling, while the circle element acts only as a mask. 我添加了一个边框元素,该边框元素的大小可以与图像缩放比例无关,而圆形元素仅充当蒙版。 It just needs a little math to adjust it correctly. 它只需要一点数学就可以正确调整它。

<div id="container">
    <div id="border"> </div>
    <div id="circle"> </div>
    <img src="https://i.imgur.com/aLJnBtV.jpg" alt=""/>
</div>            

CSS: 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;
}

http://jsfiddle.net/isaacalves/dwpy4sm7/2/ http://jsfiddle.net/isaacalves/dwpy4sm7/2/

add in css "portfolio-item": 在CSS中添加“作品集项目”:

translateZ(0);

I made some changes: JsFiddle 我做了一些更改: JsFiddle

:) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM