如何在悬停在图像上时放大,但让边框保持原位?

时间:2015-02-20 23:05:27

标签: css hover border zoom image-zoom

我是CSS和HTML的新手,我想创建一个在悬停时快速放大的图像。但是盒子半径也会放大。我知道我的div课程出了问题,但我无法弄明白究竟是什么。

所以我的意思是我想要这样的效果: http://designshack.net/tutorialexamples/imagehovers/zoomandpan.html (金发女郎一个)

这是相关代码:

 #circle {
    border: 4px solid;
height:200px;
width:200px;
    border-color: #000;
    border-radius: 100px;
    }



.bolimg{
background: url('http://i60.tinypic.com/wb82e0.jpg') no-repeat; 
height:200px;
width:200px;
-webkit-border-radius: 100px;
    -webkit-transition: 2s ease-out;
     -moz-transition: 2s ease-out;
     -o-transition: 2s ease-out;
     -ms-transition: 2s ease-out;
     transition: 2s ease-out;
}


.bolimg:hover {
    opacity:1;
     background-image: url('http://i59.tinypic.com/k0szuv.jpg'); 
     -webkit-transition: 2.5s ease-out;
     -moz-transition: 2.5s ease-out;
     -o-transition: 2.5s ease-out;
     -ms-transition: 2.5s ease-out;
     transition: 2.5s ease-out;
      -webkit-transform:scale(1.25); /* Safari and Chrome */
    -moz-transform:scale(1.25); /* Firefox */
    -ms-transform:scale(1.25); /* IE 9 */
    -o-transform:scale(1.25); /* Opera */
     transform:scale(1.25);


}

这就是身体

<div id="circle" class="bolimg"> 
</div>
</div>

请原谅我的英语非常糟糕,如果这是一个愚蠢的问题,我很抱歉。提前谢谢。

2 个答案:

答案 0 :(得分:5)

将背景图片div放在容器中:

<div class="container">
  <div class="bolimg"></div>
</div>

将尺寸和边框移动到容器,并为其指定overflow: hidden

.container {
  width: 200px;
  height: 200px;
  border: 4px solid black;
  border-radius: 100px;
  overflow: hidden;
}

将这些样式用于bolimg类:

.bolimg {
  background: url('http://i60.tinypic.com/wb82e0.jpg') no-repeat;
  background-size: cover;
  width: 100%;
  height: 100%;
  transition: 2s ease-out;
}

在悬停时使用这些样式:

.bolimg:hover {
  background-image: url('http://i59.tinypic.com/k0szuv.jpg'); 
  height: 125%;
  width: 125%;
}

动画高度和宽度而不是缩放可以解决Chrome和边框的问题。

完整代码:

&#13;
&#13;
.container {
  width: 200px;
  height: 200px;
  overflow: hidden;
  border: 4px solid black;
  border-radius: 100px;
}

.bolimg {
  background: url('http://i60.tinypic.com/wb82e0.jpg') no-repeat;
  background-size: cover;
  width: 100%;
  height: 100%;
  -webkit-transition: 2s ease-out;
  -moz-transition: 2s ease-out;
  -o-transition: 2s ease-out;
  -ms-transition: 2s ease-out;
  transition: 2s ease-out;
}

.bolimg:hover {
  background-image: url('http://i59.tinypic.com/k0szuv.jpg'); 
  height: 125%;
  width: 125%;
}
&#13;
<div class="container">
  <div class="bolimg"></div>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

您需要为overflow: hidden;元素添加#circle样式,并使.bolimg成为嵌套<div>。 请参阅代码段:

#circle {
    border: 4px solid;
height:200px;
width:200px;
    border-color: #000;
    border-radius: 100px;
    overflow:hidden;
    }



.bolimg{
background: url('http://i60.tinypic.com/wb82e0.jpg') no-repeat; 
height:200px;
width:200px;
-webkit-border-radius: 100px;
    -webkit-transition: 2s ease-out;
     -moz-transition: 2s ease-out;
     -o-transition: 2s ease-out;
     -ms-transition: 2s ease-out;
     transition: 2s ease-out;
}


.bolimg:hover {
    opacity:1;
     background-image: url('http://i59.tinypic.com/k0szuv.jpg'); 
     -webkit-transition: 2.5s ease-out;
     -moz-transition: 2.5s ease-out;
     -o-transition: 2.5s ease-out;
     -ms-transition: 2.5s ease-out;
     transition: 2.5s ease-out;
      -webkit-transform:scale(1.25); /* Safari and Chrome */
    -moz-transform:scale(1.25); /* Firefox */
    -ms-transform:scale(1.25); /* IE 9 */
    -o-transform:scale(1.25); /* Opera */
     transform:scale(1.25);


}
<div id="circle"> 
    <div class="bolimg"></div>
</div>

在Firefox中看起来很酷,但Chrome会做一些丑陋的过渡 - .-