简体   繁体   English

调整窗口大小jQuery时,如何使隐藏的DIV叠加层不显示在页面上?

[英]How can keep a hidden DIV Overlay off of the page when window is resized jQuery?

The problem i have is that i have 6 overlay DIVs which are hidden just off of the page as it loads. 我的问题是我有6个重叠的DIV,它们在加载时就隐藏在页面外。 If the user loads the page when the browser is maximised, this works perfectly, the DIVs are position just off the top of the page. 如果用户在浏览器最大化时加载页面,则效果很好,DIV位于页面顶部之外。 However if the browser is started with a smaller window, and then the user changes the height of the window (or maximises the browser) the hidden DIVs are no longer hidden off of the top, and creep on to the top of the page. 但是,如果从较小的窗口启动浏览器,然后用户更改窗口的高度(或最大化浏览器),则隐藏的DIV将不再隐藏在顶部,而是爬到页面顶部。

This is the CSS code i have written for the DIV: 这是我为DIV编写的CSS代码:

.Overlay {
position:absolute;
text-align:justify;
width:100%;
z-index:1;
background:#111111;
display:block;
opacity:0.9;
min-height:100%;
top: -100000px;
}

And here is the jQuery that calculates the position of the div, just off the top of the screen: 这是计算div位置的jQuery,就在屏幕顶部:

var currentHeight1 = $('#Overlay1').outerHeight(true);
var currentHeight2 = $('#Overlay2').outerHeight(true);
var currentHeight3 = $('#Overlay3').outerHeight(true);
var currentHeight4 = $('#Overlay4').outerHeight(true);
var currentHeight5 = $('#Overlay5').outerHeight(true);
var currentHeight6 = $('#Overlay6').outerHeight(true);

$(document).ready(function() {   
$('#Overlay1').css({'top':-currentHeight1-50},function(){
});
$('#Overlay2').css({'top':-currentHeight2-50},function(){
});
$('#Overlay3').css({'top':-currentHeight3-50},function(){
});
$('#Overlay4').css({'top':-currentHeight4-50},function(){
});
$('#Overlay5').css({'top':-currentHeight5-50},function(){
});
$('#Overlay6').css({'top':-currentHeight6-50},function(){
});
});

I am fairly certain that i need to have a function that utilises 我相当确定我需要一个可以利用的功能

$(window).resize(function(){...})

However, i cannot seem to get the function to recalculate where the DIVs should be placed when the browser window size is changed. 但是,当浏览器窗口大小更改时,我似乎无法获得重新计算DIV位置的功能。 Any help appreciated! 任何帮助表示赞赏!

If your intention is to hide the elements you should toggle between display: none and display: block in css. 如果要隐藏元素,则应在CSS中的display: nonedisplay: block之间切换。 But if you really needed to get them off screen you would need 3 arrays. 但是,如果您真的需要使它们脱离屏幕,则需要3个阵列。

  • Array 1 element position in the start. 数组1元素在开始处的位置。
  • Array 2 for where they are after $(window).resize(function(){ // your code to get new current position }) $(window).resize(function(){ // your code to get new current position })之后的数组2
  • Array 3 an array that compares the difference between Array 1 and Array 2 数组3比较数组1和数组2之间差异的数组

However as @Pete mentioned it's not good to render a bunch of very LARGE elements. 然而,由于@Pete提到它不是好的渲染了一堆非常的元素。 When you do something like 当你做类似的事情

    min-height:100%;
    top: -100000px;

What you've just down is make a bunch of (6 from the looks of your code) very large overlays with a width of 100000px + their own width since the browser has to render huge element box boundaries for such elements. 您刚下的就是制作一堆(从代码的外观来看为6个)非常大的叠加层,其宽度为100000px +自己的宽度,因为浏览器必须为此类元素呈现巨大的元素框边界。

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

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