简体   繁体   English

通过JavaScript根据HTML文档的高度更改边距/填充?

[英]Change margin/padding based on height of total HTML document via JavaScript?

I saw a trick on a stack-overflow solution a couple months ago (I can't seem to find it by searching now though). 几个月前我在堆栈溢出解决方案上看到了一个技巧(我现在无法通过搜索找到它)。 Anyway, it basically said that if you wanted two divs the same height, you could do something along these lines: 无论如何,它基本上说如果你想要两个高度相同的div,你可以沿着这些方向做点什么:

<div id="wrapper">
    <div id="nav" style="padding-bottom: 500px; margin-bottom: -500px;">
    </div>
    <div id="content" style="padding-bottom: 500px; margin-bottom: -500px;">
    </div>
</div>

The padding and negative margin seem to cancel each other out, rendering the proper height as long as the data content inside the divs doesn't spill over the 500px limit (pretty nifty, but it obviously has a huge limitation as is). 填充和负边距似乎相互抵消,只要div内的数据内容没有溢出超过500px的限制就会呈现正确的高度(非常漂亮,但显然有很大的限制)。

We'll just assume that the divs are set up style wise to be next to each other like one's taking 20% of screen width and the other is taking 80%. 我们只是假设div设置风格明智,彼此相邻,就像一个占据屏幕宽度的20%,另一个占80%。

That works pretty well for me, though I'll admit I don't understand it as well as I'd like to. 这对我来说非常有效,不过我承认我并不理解它。 But here's my problem - I could set an absurdly large value for the margin/padding to make this work for a very long page length, but I think that's probably very inefficient and poor programming. 但这是我的问题 - 我可以为margin / padding设置一个非常大的值来使这个工作的页面长度很长,但我认为这可能效率很低而且编程很差。

How can I set the style of a div to the height of the total HTML document using JavaScript so I can do this programatically and properly? 如何使用JavaScript将div的样式设置为整个HTML文档的高度,以便我可以以编程方式正确地执行此操作? And where should I call the line of JavaScript that would do that? 我应该在哪里调用可以做到这一点的JavaScript行?

Here was my closest shot so far: 到目前为止,这是我最接近的镜头:

function varPageHeight()
{
    document.getElementById('nav').style.padding-bottom = self.innerHeight + 'px';
    document.getElementById('nav').style.margin-bottom  = self.innerHeight + 'px';
    document.getElementById('content').style.padding-bottom = self.innerHeight + 'px';
    document.getElementById('content').style.margin-bottom  = self.innerHeight + 'px';
}

It doesn't work though - it looks like it does screen height instead of document height or something like that. 虽然它不起作用 - 它看起来像屏幕高度而不是文档高度或类似的东西。

If you use CSS properties in JavaScript you can't use dash. 如果您在JavaScript中使用CSS属性,则无法使用dash。 CSS property: padding-bottom is paddingBottom in JavaScript CSS属性: padding-bottom是JavaScript中的paddingBottom

so your code should look like: 所以你的代码应该是这样的:

function varPageHeight()
{
    document.getElementById('nav').style.paddingBottom = self.innerHeight + 'px';
    document.getElementById('nav').style.marginBottom  = self.innerHeight + 'px';
    document.getElementById('content').style.paddingBottom = self.innerHeight + 'px';
    document.getElementById('content').style.marginBottom  = self.innerHeight + 'px';
}

or you can use jQuery: 或者您可以使用jQuery:

function varPageHeight() {
    var innerHeight = self.innerHeight;
    $('#nav').css({
        'padding-bottom': self.innerHeight,
        'margin-bottom': self.innerHeight
    });
    $('#content').css({
        'padding-bottom': self.innerHeight,
        'margin-bottom': self.innerHeight
    });
}

To be honest, if you're trying to get equal height floated columns and you're using javascript for the solution anyway, don't depend on padding / margin. 说实话,如果你想要获得相同的高度浮动列,并且你仍然使用javascript作为解决方案,不要依赖于填充/边距。 It's ugly as hell and brings its own set of problems. 这很丑陋,并带来了一系列问题。 Best thing is just to set the height of all the columns to w/e is highest. 最好的办法就是将所有列的高度设置为w / e最高。

Here is a solution that does this in jQuery: http://jsfiddle.net/sg3s/6W4wb/ 这是一个在jQuery中执行此操作的解决方案: http//jsfiddle.net/sg3s/6W4wb/

Pure javascript could be done but you'll have a hell of a job making it work properly/cross-browser (old versions of IE specifically) with all their interpetations of what height should be. 纯粹的javascript可以完成,但你将有一个地狱的工作使其正常工作/跨浏览器(特别是IE的旧版本)与他们所有的高度应该是什么。

If you really want that padding/margin solution I want to know why because I can't really think of a good reason to do that. 如果你真的想要填充/边距解决方案我想知道为什么,因为我真的不能想到这样做的好理由。 That specific solution was designed as a workarround for a css limitation... When you're using javascript that limitation is no longer there. 该特定解决方案被设计为css限制的工作范围......当你使用javascript时,限制不再存在。

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

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