javascript - 放大图像时,如何让溢出不超过旁边的图像?

标签 javascript html css image zooming

我有 2 个问题。

1) 当图片放大时,如何让图片溢出到旁边图片的后面?

我试过 overflow: hidden; 但那不起作用。

2) 当不再悬停时,如何让图像恢复到原来的形式?

我不知道有什么办法可以做到这一点,我到处都看过。

.main{
  font-size: 0px;
}

.pic{
  display: inline-block;
  padding: 0px;
  margin: 0;
  font-size: 0px;
}

.pic img {
    -webkit-transition: all 2s ease-in-out; /* Safari and Chrome */
    -moz-transition: all 2s ease-in-out; /* Firefox */
    -ms-transition: all 2s ease-in-out; /* IE 9 */
    -o-transition: all 2s ease-in-out; /* Opera */
    transition: all 2s ease-in-out;
}

.pic:hover img {
    -webkit-transform:scale(1.05); /* Safari and Chrome */
    -moz-transform:scale(1.05); /* Firefox */
    -ms-transform:scale(1.05); /* IE 9 */
    -o-transform:scale(1.05); /* Opera */
     transform:scale(1.05);
}
  <div class="main">
    <div class="pic">
      <a href=""><img src="img/1.png" alt="food" height="337.2" width="337.2"></a>
    </div>
    <div class="pic">
      <a href=""><img src="img/2.jpg" alt="food" height="337.2" width="337.2"></a>
    </div>
    <div class="pic">
      <a href=""><img src="img/3.jpg" alt="food" height="337.2" width="337.2"></a>
    </div>
    <div class="pic">
      <a href=""><img src="img/4.jpg" alt="food" height="337.2" width="337.2"></a>
    </div>
    <div class="pic">
      <a href=""><img src="img/5.jpg" alt="food" height="337.2" width="337.2"></a>
    </div>
    <div class="pic">
      <a href=""><img src="img/6.jpg" alt="food" height="337.2" width="337.2"></a>
    </div>
    <div class="pic">
      <a href=""><img src="img/7.jpg" alt="food" height="337.2" width="337.2"></a>
    </div>
    <div class="pic">
      <a href=""><img src="img/8.jpg" alt="food" height="337.2" width="337.2"></a>
    </div>
    <div class="pic">
      <a href=""><img src="img/9.jpg" alt="food" height="337.2" width="337.2"></a>
    </div>
    <div class="pic">
      <a href=""><img src="img/10.jpg" alt="food" height="337.2" width="337.2"></a>
    </div>
    <div class="pic">
      <a href=""><img src="img/11.jpg" alt="food" height="337.2" width="337.2"></a>
    </div>
    <div class="pic">
      <a href=""><img src="img/12.jpg" alt="food" height="337.2" width="337.2"></a>
    </div>
    <div class="pic">
      <a href=""><img src="img/13.jpg" alt="food" height="337.2" width="337.2"></a>
    </div>
    <div class="pic">
      <a href=""><img src="img/14.jpg" alt="food" height="337.2" width="337.2"></a>
    </div>
    <div class="pic">
      <a href=""><img src="img/15.jpg" alt="food" height="337.2" width="337.2"></a>
    </div>
    <div class="pic">
      <a href=""><img src="img/16.jpg" alt="food" height="337.2" width="337.2"></a>
    </div>
  </div>

最佳答案

您需要将“.pic img”下的样式移动到“.pic:hover img”中,因为您只希望在悬停时出现这些效果。我还为悬停图像添加了一个 z-index 以使其移动到其他图像下方。最后,为了清晰和简洁,我删除了一些图像并添加了边框。 https://jsfiddle.net/n5m70m2m/

HTML

<div class="main">
    <div class="pic">
      <a href=""><img src="http://placehold.it/337X337" alt="food" height="337.2" width="337.2"></a>
    </div>
    <div class="pic">
      <a href=""><img src="http://placehold.it/337X337" alt="food" height="337.2" width="337.2"></a>
    </div>
    <div class="pic">
      <a href=""><img src="http://placehold.it/337X337" alt="food" height="337.2" width="337.2"></a>
    </div>
    <div class="pic">
      <a href=""><img src="http://placehold.it/337X337" alt="food" height="337.2" width="337.2"></a>
    </div>
    <div class="pic">
      <a href=""><img src="http://placehold.it/337X337" alt="food" height="337.2" width="337.2"></a>
    </div>
    <div class="pic">
      <a href=""><img src="http://placehold.it/337X337" alt="food" height="337.2" width="337.2"></a>
    </div>
    <div class="pic">
      <a href=""><img src="http://placehold.it/337X337" alt="food" height="337.2" width="337.2"></a>
    </div>
    <div class="pic">
      <a href=""><img src="http://placehold.it/337X337" alt="food" height="337.2" width="337.2"></a>
    </div>
    <div class="pic">
      <a href=""><img src="http://placehold.it/337X337" alt="food" height="337.2" width="337.2"></a>
    </div>
    <div class="pic">
      <a href=""><img src="http://placehold.it/337X337" alt="food" height="337.2" width="337.2"></a>
    </div>
    <div class="pic">
      <a href=""><img src="http://placehold.it/337X337" alt="food" height="337.2" width="337.2"></a>
    </div>
    <div class="pic">
      <a href=""><img src="http://placehold.it/337X337" alt="food" height="337.2" width="337.2"></a>
    </div>
  </div>

CSS

.main{
  font-size: 0px;
}

img {
  border: 1px solid black;
}
.pic{
  display: inline-block;
  padding: 0px;
  margin: 0;
  font-size: 0px;
}

.pic:hover img {
    -webkit-transform:scale(1.05); /* Safari and Chrome */
    -moz-transform:scale(1.05); /* Firefox */
    -ms-transform:scale(1.05); /* IE 9 */
    -o-transform:scale(1.05); /* Opera */
     transform:scale(1.05);
     position: relative;
     z-index: -1;
     transition: all 2s ease-in;
     -webkit-transition: all 2s ease-in-out; /* Safari and Chrome */
    -moz-transition: all 2s ease-in-out; /* Firefox */
    -ms-transition: all 2s ease-in-out; /* IE 9 */
    -o-transition: all 2s ease-in-out; /* Opera */
}

关于javascript - 放大图像时,如何让溢出不超过旁边的图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48698137/

相关文章:

javascript - 如何将所选元素的标签添加到 jquery 自动完成?

javascript - 使用 javaScript 将前缀表达式转换为中缀表达式

jquery - 更改 z-index 和宽度但部分清除 sibling 不重叠?

javascript - 移动 safari 中的背景图像被拉伸(stretch)

javascript - html5如何制作随机背景色

javascript - 传送带在短时间后停止滑动

javascript - 对太宽的下拉菜单项的建议?

javascript - 使用 socket.on 处理程序之外的数据

html - div 没有正确排列

javascript - 我正在尝试从存储在数据库中的 pdf 中检索,但是它返回的是路径而不是文件本身