简体   繁体   English

将 Div 定位在背景图像的顶部(覆盖/响应)

[英]Position Div On Top of Background Image ( covered / repsonsive )

I'm trying to position multiple divs on top of a background image so it makes a cool effect.我正在尝试将多个 div 放置在背景图像的顶部,以便产生很酷的效果。 The main issue is that the website is responsive and so is the image - it's background-size: cover which makes it hard to keep the overlays exactly where they need to be when viewing larger / smaller screens.主要问题是网站是响应式的,图像也是如此 - 它是background-size: cover ,这使得在查看更大/更小的屏幕时很难将覆盖图准确地保持在它们需要的位置。 I've created a crude JSFiddle to show what I mean.我创建了一个粗略的JSFiddle来说明我的意思。 In this example I'm am trying to just overlay the animated boxes over the Koala's eyes.在这个例子中,我试图将动画框覆盖在考拉的眼睛上。

I'm not sure if it's easier to get the coords of where I want the overlays to be and work with that or if I need to work with the window offset.我不确定是否更容易获得我想要覆盖的位置的坐标并使用它,或者我是否需要使用窗口偏移量。 Since it's also centered and when zoomed out it changes position-y and I'm unsure how to even track that being a background image.由于它也是居中的,并且在缩小时会改变position-y ,我不确定如何甚至跟踪它是背景图像。

Really what I'm hoping for is to get some insight on how to track the exact position of coords on this background image or point me in different direction to solve this issue.我真正希望的是深入了解如何在此背景图像上跟踪坐标的确切位置或将我指向不同的方向以解决此问题。

 #koala { background: url( 'http://i.imgur.com/1xZ17bk.jpg' ) no-repeat center; background-size: cover; position: relative; width: 100%; height: 768px; } #leftEye, #rightEye { width: 20px; height: 20px; background-color: #F00; position: absolute; -webkit-animation: rotating 2s linear infinite; -moz-animation: rotating 2s linear infinite; -o-animation: rotating 2s linear infinite; animation: rotating 2s linear infinite; } #leftEye { top: 48%; left: 37%; } #rightEye { top: 59%; right: 35%; } @-webkit-keyframes rotating { from{ -webkit-transform: rotate(0deg); -moz-transform: rotate(0deg); -ms-transform: rotate(0deg); -o-transform: rotate(0deg); transform: rotate(0deg); } to{ -webkit-transform: rotate(360deg); -moz-transform: rotate(360deg); -ms-transform: rotate(360deg); -o-transform: rotate(360deg); transform: rotate(360deg); } } @-moz-keyframes rotating { from{ -webkit-transform: rotate(0deg); -moz-transform: rotate(0deg); -ms-transform: rotate(0deg); -o-transform: rotate(0deg); transform: rotate(0deg); } to{ -webkit-transform: rotate(360deg); -moz-transform: rotate(360deg); -ms-transform: rotate(360deg); -o-transform: rotate(360deg); transform: rotate(360deg); } } @-ms-keyframes rotating { from{ -webkit-transform: rotate(0deg); -moz-transform: rotate(0deg); -ms-transform: rotate(0deg); -o-transform: rotate(0deg); transform: rotate(0deg); } to{ -webkit-transform: rotate(360deg); -moz-transform: rotate(360deg); -ms-transform: rotate(360deg); -o-transform: rotate(360deg); transform: rotate(360deg); } } @-o-keyframes rotating { from{ -webkit-transform: rotate(0deg); -moz-transform: rotate(0deg); -ms-transform: rotate(0deg); -o-transform: rotate(0deg); transform: rotate(0deg); } to{ -webkit-transform: rotate(360deg); -moz-transform: rotate(360deg); -ms-transform: rotate(360deg); -o-transform: rotate(360deg); transform: rotate(360deg); } } @keyframes rotating { from{ -webkit-transform: rotate(0deg); -moz-transform: rotate(0deg); -ms-transform: rotate(0deg); -o-transform: rotate(0deg); transform: rotate(0deg); } to{ -webkit-transform: rotate(360deg); -moz-transform: rotate(360deg); -ms-transform: rotate(360deg); -o-transform: rotate(360deg); transform: rotate(360deg); } } .notneeded { width: 100%; height: 50px; background-color: beige; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="header" class="notneeded"></div> <div id="koala"> <div id="leftEye"></div> <div id="rightEye"></div> </div> <div id="footer" class="notneeded"></div>

Since you asked for pointing in a direction this is what I would try: If the #koala-divs height is fixed 768px and width responsive 100% I would try to work with:由于您要求指向一个方向,这就是我要尝试的:如果 #koala-divs 高度固定为 768px,宽度响应为 100%,我将尝试使用:

background-size: 100% auto;
background-position: center; 

I guess now you could calculate the position for the rotating divs relative to the #koala-divs width and the proportion of the background-image.我想现在您可以计算相对于 #koala-divs 宽度和背景图像比例的旋转 div 的位置。

X-coordinates should be easy once you know the x-coordinates of the eyes relative in the image (for eg 40% left and 60% right)一旦您知道图像中眼睛相对的 x 坐标(例如 40% 左和 60% 右),X 坐标应该很容易

Y-coordinates are a bit trickier I assume.我认为 Y 坐标有点棘手。 Lets say your image is sized 1200 x 800. Its proportion is 3:2 (12:8 -> 6:4 -> 3:2).假设您的图像大小为 1200 x 800。其比例为 3:2 (12:8 -> 6:4 -> 3:2)。

Now you could get the total width of the #koala-div with jquery/javascript and multiply it with 0,666666666666666666666666 (two thirds) to get the actual height of the background image.现在,您可以使用 jquery/javascript 获取 #koala-div 的总宽度并将其乘以 0,66666666666666666666666666(三分之二)以获得背景图像的实际高度。

From there you can derive the y-coordinates similar to the way i proposed for the x-coordinates.从那里你可以推导出类似于我为 x 坐标提出的方式的 y 坐标。 But you need to imagine it to be verticaly cut in half and position in relatively in each half.但是你需要想象它被垂直切割成两半,并在每一半中相对定位。

I don't know if you can follow my thoughs or even if they are useful.我不知道你是否可以遵循我的想法,或者它们是否有用。 Since I got not enough time today I hope this helps nevertheless...由于我今天没有足够的时间,我希望这会有所帮助...

Have a nice Tuesday!祝你周二愉快!

edit/note:编辑/注意:

The image will just cover the div as long as the div is at least as wide as the image... If it gets to small/narrow you will get empty space above and below the image.只要 div 至少与图像一样宽,图像就会覆盖 div...如果它变小/变窄,您将在图像上方和下方获得空白空间。

A much simpler solution is to use the window width changes to your advantage.一个更简单的解决方案是利用窗口宽度的变化对您有利。 So, ordinarily, you would measure the changes in your window sizes and subtract that from the eye's current position.因此,通常,您会测量窗口大小的变化并从眼睛的当前位置中减去该变化。 However, because your background is centered, you need to alter that slightly - take the old window position, subtract the new window position and divide the result by two.但是,因为您的背景居中,所以您需要稍微改变它 - 取旧窗口位置,减去新窗口位置并将结果除以二。

I've done this with jQuery.我已经用 jQuery 做到了这一点。

var leftEye = $('#leftEye');
var rightEye = $('#rightEye');
var oldLeftEyePos = leftEye.position().left;
var oldRightEyePos = rightEye.position().left;
var oldWindowWidth = $(window).width();

$(window).resize(function(){
  var newWindowWidth = $(window).width();

    leftEye.css({
      left: oldLeftEyePos - ((oldWindowWidth - newWindowWidth) / 2)
    });

    rightEye.css({
      left: oldRightEyePos - ((oldWindowWidth - newWindowWidth) / 2)
    });
});

Here's an updated jsfiddle with an example;这是一个带有示例的更新的 jsfiddle;

http://jsfiddle.net/joe5hjr3/10/ http://jsfiddle.net/joe5hjr3/10/

Disclaimer: This isn't perfect.免责声明:这并不完美。 It shrinks great, but as you go above a certain size, they start to close in on eachother - but hopefully, this is enough to get you on the right track.它缩小得很好,但是当你超过一定的尺寸时,它们开始相互靠近——但希望这足以让你走上正轨。 Also, it does not work great for zooming out, but works fine for zooming in.此外,它不适用于缩小,但适用于放大。

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

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