简体   繁体   English

如何将一张图片固定在背景图片上(响应)?

[英]How do I pin one image to the background-image (responsive)?

I need your help! 我需要你的帮助! I have a background image and I need one part of that image to move (rotate or zoom) whenever you hover it. 我有一幅背景图片,每当您将其悬停时,都需要该图片的一部分进行移动(旋转或缩放)。 This image is just an example but it's pretty similar what i have, except mine is more centered. 该图像只是一个例子,但与我的图像非常相似,只是我的图像更居中。

I want the little sticker on the bowl to move a little (rotate like 20 degrees or any sort of animation to be honest) whenever you mouse over it. 我希望每次将鼠标悬停在碗上时,碗上的小贴纸都会移动一点(说实话,旋转20度或其他任何动画形式)。

I've already cropped the sticker so now I have two images. 我已经裁剪了贴纸,所以现在有两个图像。 I'm using the background as my div's bg. 我将背景用作div的背景。 Here's my code: 这是我的代码:

HTML:
<header class="background-image">
    <div class="sticker"></div>
</header>

CSS:
.background-image {
background-image: url(../img/bg-principal.png) !important;
background: white;
height: 90%;
background-position: center;
background-size: contain;
background-repeat: no-repeat; }

I've managed to place the sticker where it's supposed to be, but it's not responsive. 我设法将标贴放置在原本应该放置的位置,但是它没有反应。 When the screen is resized, the little sticker moves and I can't seem to find a solution for that. 调整屏幕大小时,小标签会移动,我似乎找不到解决方案。 Basically what I need is to pin the sticker to the background image, but I have no idea if that's possible. 基本上,我需要将标签固定到背景图像上,但是我不知道是否可行。 I looked around and couldn't find anything that worked. 我环顾四周,找不到任何有效的方法。

Does anyone have any suggestions? 有没有人有什么建议? Is there any way that I can do this using only CSS? 有什么方法可以仅使用CSS做到这一点吗? I only have basic knowledge of jQuery so I'd like to avoid that but if it's the only solution I'd be fine with that too. 我只有jQuery的基本知识,所以我想避免这种情况,但是如果这是唯一的解决方案,我也可以。

Thanks!! 谢谢!!

EDIT: I tried using a transparent png sticker the same size as the background, but since I need the hover effect, I end up having the same problem. 编辑:我尝试使用与背景大小相同的透明png贴纸,但是由于我需要悬停效果,所以我最终遇到了同样的问题。

If you use vw as a unit of measurement, the sticker can track based on the size of the element 如果将vw用作度量单位,则标签可以根据元素的大小进行跟踪

 .background-image { background-image: url(http://images.all-free-download.com/images/graphiclarge/butterfly_flower_01_hd_pictures_166973.jpg) !important; height: 40vw; background-position: center; background-size: contain; background-repeat: no-repeat; } .sticker{ width:20px; height:20px; position:absolute; top: 20vw; left:40vw; background:red; } 
 <header class="background-image"> <div class="sticker"></div> </header> 

I finally found a solution! 我终于找到了解决方案! This is my final code: 这是我的最终代码:

HTML:
<div class="background">
 <img src="..."/>
 <div id="pin" class="sticker">
 </div>
</div>

CSS:
.background {
  margin:10px;
  position: relative;
  display: inline-block;
}

.map img {
  max-width:100%;
  display: block;
}

.box {
  width:20%;
  height:20%;
  background-image:   url(...);
  background-position: top center;
  background-repeat: no-repeat;
  background-size: contain;
  position: absolute;
}

#pin {
  top:60%;
  left:46%;
}

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

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