简体   繁体   English

动态改变尺寸的顶部位置

[英]Dynamically change top position on resize

I'm trying on resize the window to change the position top of the image so the image to be fit in the box, because it's changing on resize and the image is going too much outside. 我正在尝试调整窗口大小,以更改图像的顶部位置,以使图像适合放在框中,因为调整大小时图像会改变,并且图像在外部的位置过多。 I'm not sure do I need the height of the box and the image and then to do something with position top. 我不确定是否需要框和图像的高度,然后在顶部位置做些什么。

 $(window).resize(function() { var boxHeight = $('.box').innerHeight(); var imgHeight = $('.img').height(); }); 
 .container { max-width: 850px; margin: 0 auto; } .box { background: #fff; padding: 69px 55px; border: 1px solid #000; margin-top: 150px; border-radius: 6px; } .image { position: absolute; top: -90px; right: -41px; } @media only screen and (max-width:767px) { .image { position: relative; max-width: 100%; top: -88px } } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" /> <div class="container"> <div class="box"> <div class="row"> <div class="col-sm-6 col-sm-push-6"> <img src="https://i.imgur.com/uHoTAhr.png" class="image"> </div> <div class="col-sm-6 col-sm-pull-6"> It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. </div> </div> </div> </div> 

You are facing problems because of your .box container padding. 由于.box容器填充,您遇到了问题。 Remove the padding. 除去填充物。

I would also suggest to adjust your image using transform and wrap the content in <p> tag and set it center using position:absolute . 我还建议您使用transform调整图像并将内容包装在<p>标记中,并使用position:absolute将其设置为居中。 And for responsive set position:relative for content. 对于响应式设置position:relative对于内容。 I have also done some changes in your HTML markup. 我还对您的HTML标记做了一些更改。

Stack Snippet 堆栈片段

 .container { max-width: 850px; margin: 0 auto; } .box { background: #fff; padding: 0; border: 1px solid #000; margin-top: 150px; border-radius: 6px; position: relative; } .image { position: relative; max-width: 100%; transform: translateY(-9%) translateX(0%); margin-bottom: -20px; } .text { position: absolute; width: 50%; top: 50%; transform: translateY(-50%); left: 30px; } @media only screen and (max-width:767px) { .text { position: relative; padding: 20px 50px; width: auto; top: auto; transform: none; left: auto; } } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" /> <div class="container"> <div class="box"> <div class="row"> <div class="col-sm-10 col-sm-offset-2 text-right"> <img src="https://i.imgur.com/uHoTAhr.png" class="image"> </div> <p class="text">It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout.</p> </div> </div> </div> 

Maybe the problem is in the overlapping head in the picture, aside from what Bhuwan mentioned. 除了布万提到的问题之外,问题可能出在图片的重叠头部。 I see that it is overlapping 9.3% of the whole height of the image so we can use this constraint to calculate the difference from the top. 我看到它重叠了图像整个高度的9.3%,因此我们可以使用此约束来计算与顶部的差异。

Considering your original code where you had a 69px top padding, you need to put this line in the .resize() listener: 考虑到原始代码的顶部填充为69px,您需要将此行放在.resize()侦听器中:

$(".image").css(
    "top", 
    parseInt($(".box").css("padding-top")) - ($(".image").height() * 0.093)
);

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

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