简体   繁体   English

在调整大小时将图像的一部分与css重叠

[英]overlap part of image with css on resize

I have a sprite image and i want it to be animated on hover, as in slide up/down to reveal the other hidden part(coloured sprite). 我有一个精灵图片,希望将其在悬停时进行动画处理,如上下滑动以显示其他隐藏部分(彩色精灵)。 My css below works fine for pixels but i also want to make it responsive so that it resizes when the browser is minimised/resized. 我下面的css适用于像素,但我也想使其响应,以便在最小化/调整浏览器大小时对其进行调整。

<div class="img" style="background-image: url(a.jpg)"></div>

<style>
.img {
background-position: 0 -258px;
display: block;
height: 258px;
margin: 0 0 3px;
position: relative;
width: 205px;
z-index: 1;
}
</style>
在此处输入图片说明

The easiest way to make an image responsive is to give it a width of 100% and leave it at that. 使图像具有响应性的最简单方法是将其宽度设置为100%,然后保留该宽度。 That way, it will always use the width of the parent container, and also use that width to find a ratio to render the height appropriately. 这样,它将始终使用父容器的宽度,并且还将使用该宽度来找到适当渲染高度的比率。

Take a look at this: 看看这个:

<div class="wrapper"><img class="img" src="a.jpg" width="100%" alt="image" /></div>

<style>
  .wrapper { width:20%;}
</style> 

In the above code, the image will adjust acording to the wrapper's dimensions and will resize when the window is resized. 在上面的代码中,图像将根据包装器的尺寸进行调整,并在调整窗口大小时调整大小。

But with your code, you need to hide part of the image, thus styling the wrapper class to be responsive will be a headache. 但是,对于您的代码,您需要隐藏部分图像,因此将包装器类设置为响应式将是一件令人头疼的事情。

Here is what i came up with: 这是我想出的:

<style>
  .wrapper { width:244px; height:256.5px; overflow:hidden;}
  .img{transition: 1s ease-in;}
  .wrapper:hover > .img{ margin-top:-105%;}
</style> 

Now all you have to do is make your wrapper class fluid, and the image will follow suite. 现在,您要做的就是使包装器类变得流畅,并且图像将紧随其后。 Mybe percentage will do the trick. Mybe百分比将解决问题。

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

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