[英]Calculate background-image ratio
I have a background-image called 1
at the bottom of my page.我的页面底部有一个名为1
的背景图像。 I want to show another div with a background-image called 2
over the bottom 5% of background-image 1
(so, not the bottom 30% of the screen but bottom 30% of the image).我想显示与背景图像另一个DIV称为2
以上的背景图像的底部5% 1
(因此,不是底部30中的屏幕,但图像的底部30%的%)。
However, I have a problem with making this responsive.但是,我在使其响应时遇到问题。 I think I need to calculate the ratio somehow but I have no idea how.我想我需要以某种方式计算比率,但我不知道如何计算。 See below for the code I have.请参阅下面的代码。 Whenever you resize the screen, the size of the background-image 1
changes and the position of the background-image 2
doesn't stay the same.每当您调整屏幕大小时,背景图像1
的大小都会发生变化,而背景图像2
的位置不会保持不变。 How can I make it stick to one spot no matter what?无论如何,我怎样才能让它粘在一个地方?
Try this out.试试这个。 It seems to meet your requirements and is responsive.它似乎满足您的要求并且响应迅速。
$(document).ready(heroResize); $(window).on('resize', heroResize); function heroResize() { var marginTop = $(window).height() * 0.8; $('#layer2').css('margin-top', marginTop); }
#content { position: relative; } .layer { width: 100%; height: 100vh; max-height: 1000px; } #layer1 { background: url('https://cliffsliving.com/wp-content/uploads/2018/02/CLIFFS_DevilsCourthouse_Shoot_2017_3740_cmyk_brighter_PJ.jpg') center top/contain no-repeat #6DB3F2; position: absolute; top: 0; left: 0; } #layer2 { position: absolute; bottom: 0; left: 0; background: url('https://www.pngtube.com/myfile/full/9-93924_png-parallax-background-png.png') center bottom/contain repeat-x; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="container"> <div id="content"> <div id="layer1" class="layer"> <div id="layer2" class="layer"></div> </div> </div> <div id="footer"></div> </div>
我不确定我是否正确理解你的问题,但我认为你希望你的 layer2 被包裹在 layer1 中,然后使用 position absolute 和 bottom: 0 所以它总是粘在底部并且相对于父级(在这个情况将是第 1 层)。
You can do this with one element, multiple background and vw
unit without the need of JS.您可以使用一个元素、多个背景和vw
单元来完成此操作,而无需 JS。 The trick is the value 0.877vw
which is equal to 5% * height of the image
and the the image have a ratio equal to 5.7
and its width will be the window size so 100vw
.诀窍是值0.877vw
等于5% * height of the image
图像的比率等于5.7
,其宽度将是窗口大小,因此100vw
。 We will have then: 0.05 * (100vw/5.7) = 0.877vw
我们将有: 0.05 * (100vw/5.7) = 0.877vw
body { margin:0; } .layer{ height:100vh; min-height:60vw; background: url('https://www.pngtube.com/myfile/full/9-93924_png-parallax-background-png.png') bottom, url('https://cliffsliving.com/wp-content/uploads/2018/02/CLIFFS_DevilsCourthouse_Shoot_2017_3740_cmyk_brighter_PJ.jpg') bottom 0.877vw center; background-size:contain; background-repeat:no-repeat; }
<div class="layer"></div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.