在悬停时缩放圆形图像

时间:2014-11-07 16:54:38

标签: html css html5 css3

我正在尝试使用带边框的“圆形”图像,当用户将鼠标悬停在图像上时,内部图像是缩放边框。为什么它似乎是故障?谁能告诉我发生了什么?

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

JSFiddle

5 个答案:

答案 0 :(得分:2)

Border-radius不是可动画的属性,因此它不会随图像缩放。它只会在转换结束时进行缩放。 http://www.w3.org/TR/css3-transitions/#properties-from-css-

答案 1 :(得分:1)

如果要在缩放时保持边框,则可以在图像上放置带边框和框阴影(框阴影将充当实际灰色边框)的透明圆。这样你基本上会有一个显示图像的小窗口。

#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/

答案 2 :(得分:0)

您可以在scale

项目上应用border-radius
.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

答案 3 :(得分:0)

这是基于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;
}

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

答案 4 :(得分:-1)

添加css&#34; portfolio-item&#34;:

translateZ(0);

我做了一些更改:this answer

:)