简体   繁体   English

网格中的悬停效果

[英]Hover effect in a grid

For some reason when hovering over a box, it increases in size a little bit. 由于某些原因,将鼠标悬停在盒子上时,它的大小会稍微增加。 It's supposed to only zoom in within the box, not increase the size of it. 它应该仅在框内放大,而不增加框的大小。

JSFiddle JSFiddle

 #wrapper { overflow: hidden; width: 100%; max-width: 900px; margin: 0 auto; border: 1px solid #c5c5c5; -webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing: border-box; background: #eee; } #outer { width: 20%; padding-bottom: 20%; color: #FFF; position: relative; float: left; overflow: hidden; display: inline-block; } #inner { position: absolute; left: 0px; right: 0px; top: 0px; bottom: 0px; background: #66F; padding: 10px; -webkit-transition: all .3s ease; -moz-transition: all .3s ease; -ms-transition: all .3s ease; -o-transition: all .3s ease; transition: all .3s ease; } #inner:hover { transform: scale(1.1); -ms-transform: scale(1.1); /* IE 9 */ -moz-transform: scale(1.1); /* Firefox */ -webkit-transform: scale(1.1); /* Safari and Chrome */ -o-transform: scale(1.1); /* Opera */ vertical-align: middle; } 
 <div id="wrapper"> <div id="outer"> <div id="inner"> test </div> </div> <div id="outer"> <div id="inner"> test </div> </div> <div id="outer"> <div id="inner"> test </div> </div> <div id="outer"> <div id="inner"> test </div> </div> <div id="outer"> <div id="inner"> test </div> </div> <div id="outer"> <div id="inner"> test </div> </div> <div id="outer"> <div id="inner"> test </div> </div> <div id="outer"> <div id="inner"> test </div> </div> <div id="outer"> <div id="inner"> test </div> </div> <div id="outer"> <div id="inner"> test </div> </div> </div> 

The problem you are seeing is because of your positioning attributes on the #inner which is leaving a 1px gap on either side. 您看到的问题是由于您在#inner上的定位属性而在#inner都留有1px的间隙。 The parent is effectively 1px larger than the inner box on all sides and so you see a small increase when you hover. 父级实际上比各侧的内部框大1px,因此,当您将鼠标悬停时,会看到很小的增加。 Only after this 1px is covered, the overflow: hidden comes into effect. 仅在覆盖此1px之后, overflow: hidden才会生效。

I have added a background: red to the parent ( #outer ) so that you can see how the parent is larger than the child. 我已经添加了一个background: red作为父级( #outer ),以便您可以看到父级比孩子大。

 #wrapper { overflow: hidden; width: 100%; max-width: 900px; margin: 0 auto; border: 1px solid #c5c5c5; box-sizing: border-box; background: #eee; } #outer { width: 20%; padding-bottom: 20%; color: #FFF; position: relative; float: left; overflow: hidden; display: inline-block; background: red; } #inner { position: absolute; left: 1px; right: 1px; top: 1px; bottom: 1px; background: #66F; padding: 10px; transition: all .3s ease; } #inner:hover { transform: scale(1.1); vertical-align: middle; } 
 <div id="wrapper"> <div id="outer"> <div id="inner"> test </div> </div> <div id="outer"> <div id="inner"> test </div> </div> <div id="outer"> <div id="inner"> test </div> </div> <div id="outer"> <div id="inner"> test </div> </div> <div id="outer"> <div id="inner"> test </div> </div> <div id="outer"> <div id="inner"> test </div> </div> <div id="outer"> <div id="inner"> test </div> </div> <div id="outer"> <div id="inner"> test </div> </div> <div id="outer"> <div id="inner"> test </div> </div> <div id="outer"> <div id="inner"> test </div> </div> </div> 


Instead of using positioning attributes to leave that 1px gap on all sides, you could use a transparent border on the parent. 可以使用父对象上的透明边框代替使用定位属性在所有面上都留出1px的间隙。 This will leave a 1px gap on all side like your original snippet but since there is no space left between the parent's border and child, the zoom would not look like the child is growing outside the parent. 就像您的原始代码片段一样,这将在所有面上留下1px的间隙,但是由于父母的边界和孩子之间没有剩余空间,因此缩放看起来不会像孩子在父母外部长大。

 #wrapper { overflow: hidden; width: 100%; max-width: 900px; margin: 0 auto; border: 1px solid #c5c5c5; box-sizing: border-box; background: #eee; } #outer { width: 20%; padding-bottom: 20%; color: #FFF; position: relative; float: left; overflow: hidden; display: inline-block; border: 1px solid transparent; } #inner { position: absolute; left: 0px; right: 0px; top: 0px; bottom: 0px; background: #66F; padding: 10px; transition: all .3s ease; } #inner:hover { transform: scale(1.1); vertical-align: middle; } 
 <div id="wrapper"> <div id="outer"> <div id="inner"> test </div> </div> <div id="outer"> <div id="inner"> test </div> </div> <div id="outer"> <div id="inner"> test </div> </div> <div id="outer"> <div id="inner"> test </div> </div> <div id="outer"> <div id="inner"> test </div> </div> <div id="outer"> <div id="inner"> test </div> </div> <div id="outer"> <div id="inner"> test </div> </div> <div id="outer"> <div id="inner"> test </div> </div> <div id="outer"> <div id="inner"> test </div> </div> <div id="outer"> <div id="inner"> test </div> </div> </div> 

Couple of other points to note: 需要注意的其他几点:

  • I didn't give this much importance as I thought of it to be a copy-paste demo but same id should not be used for multiple elements. 我没有这么重视,因为我认为这是一个复制粘贴演示,但是同一id不应用于多个元素。 If we do, the HTML becomes invalid (as putvande has pointed in his comment). 如果这样做,HTML将无效(如putvande在其评论中指出的那样)。
  • As torazaburo has pointed out in his comment, the box-sizing and transition properties don't require prefixed versions anymore (unless you have to support very very old browser versions). 正如torazaburo在他的评论中指出的那样, box-sizingtransition属性不再需要带前缀的版本(除非您必须支持非常老的浏览器版本)。

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

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