简体   繁体   English

根据屏幕尺寸调整div大小

[英]Resizing div depending on screen size

I am more of a front end dev so am only really a copy and paste jQuery/Javascript kinda guy. 我更像是一个前端开发人员,所以实际上只是一个复制粘贴jQuery / Javascript的家伙。 I can amend bits after reading but any help or amending of the code would be great. 我可以在阅读后修改位,但是对代码的任何帮助或修改都很好。

I want to have a banner div fill all but the last 50px or 100px of the screen so that the user can see there is more content to scroll onto (this is done with and arrow graphically). 我希望横幅div填充屏幕的最后50像素或100像素以外的所有内容,以便用户可以看到还有更多内容可以滚动到屏幕上(这是通过图形和箭头完成的)。

This also needs to happen no matter the screen size desktop to mobile. 无论台式机到手机的屏幕大小如何,这都需要发生。 I have the following code that resizes my containing div to fill the screen and works for all devices from testing i just need the last part that then removes xx pixels to bing in the below content to be seen on the screen. 我有以下代码可以调整我包含的div的大小以填充屏幕,并适用于测试中的所有设备,我只需要最后一部分,然后将xx像素删除即可在下面的内容中显示在屏幕上。

I have these 2 snippets that work great to full the screen 我有这2个片段,非常适合全屏播放

Snippet 1 片段1

<script type="text/javascript">
function handleResize() {
var h = jQuery(window).height();
    jQuery('.banner_slide').css({'height':h+'px'});
}
jQuery(function(){
    handleResize();
    jQuery(window).resize(function(){
    handleResize();
});
});
</script>

Snippet 2 片段2

<script type="text/javascript">
jQuery(function(){
    var windowH = jQuery(window).height();
    var wrapperH = jQuery('.banner_slide').height();
    if(windowH > wrapperH) {                            
        jQuery('.banner_slide').css({'height':(jQuery(window).height())+'px'});
    }                                                                               
jQuery(window).resize(function(){
    var windowH = jQuery(window).height();
    var wrapperH = jQuery('.banner_slide').height();
    var differenceH = windowH - wrapperH;
    var newH = wrapperH + differenceH;
    var truecontentH = jQuery('.banner_slide div').height();
    if(windowH > truecontentH) {
        jQuery('.banner_slide').css('height', (newH)+'px');
    }
    })          
});
</script>

Any help would be amazing 任何帮助都将是惊人的

If ancient browser support is not a problem you can use css3 calc() function. 如果对旧版浏览器的支持不是问题,则可以使用css3 calc()函数。

set padding-right:50px for the parent, apply width: calc(100% - 50px) and absolutely position the image at the padding space 为父级设置padding-right:50px ,应用width: calc(100% - 50px)并将图像绝对定位在padding空间

Why don't you use media queries in css instead? 为什么不在CSS中使用媒体查询呢?

Then you can adjust to all screen sizes, height and width depending on which way you set it. 然后,您可以根据设置方式来调整所有屏幕尺寸,高度和宽度。

For example: 例如:

    @media (min-height: 500px) and (max-height: 796px){

     .banner_slide{
    height: 746px;
           }

    }

Then you can adjust the style to whatever screen size you want so if it's mobile size depending on the mobile it could be smaller. 然后,您可以将样式调整为所需的任何屏幕尺寸,因此,如果移动设备的尺寸取决于移动设备的尺寸,则尺寸可能会更小。 (Adjust to whatever suits!) (调整为适合任何衣服!)

 @media (min-height: 0px) and (max-height: 350px){

     .banner_slide{
    height: 300px;
           }

    }

In snippet 2 the resulting height is calculated by substracting the banner height from the window height 在代码段2中,通过从窗口高度中减去横幅高度来计算最终的高度

var windowH = jQuery(window).height();
var wrapperH = jQuery('.banner_slide').height();
var differenceH = windowH - wrapperH;

can u substract the height of your bottom offset just like that ? 您可以像这样减去您的底部偏移的高度吗?

var windowH = jQuery(window).height();
var wrapperH = jQuery('.banner_slide').height();
var bottomOffset = jQuery('.image_on_bottom_page').height;
var differenceH = windowH - wrapperH - bottomOffset;

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

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