te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>html - How to set body height with JavaScript? - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - How to set body height with JavaScript? - Stack Overflow

programmeradmin3浏览0评论

document.body.height = document.getElementById("page-main").clientHeight + 400;
#page-main {
    position:absolute;
    width:100%;
    height:2000px;
    z-index:2;
    background:#eeeeee;
}

#footer {
    position:fixed;
    width:100%:
    bottom:0px;
    height:400px;
    z-index:1;
    background:#aaaaaa;
}
<body>

<div id='page-main'>main</div>

<div id='footer'>footer</div>

</body>

document.body.height = document.getElementById("page-main").clientHeight + 400;
#page-main {
    position:absolute;
    width:100%;
    height:2000px;
    z-index:2;
    background:#eeeeee;
}

#footer {
    position:fixed;
    width:100%:
    bottom:0px;
    height:400px;
    z-index:1;
    background:#aaaaaa;
}
<body>

<div id='page-main'>main</div>

<div id='footer'>footer</div>

</body>

I have a footer div with position: fixed; bottom: 0px; and a main content div with position: absolute;.

Basically the idea is to have the main content div act like a sheet of paper on top of the static background of the document, so you would scroll through the content of the page and when you get to the bottom you would need to be able to scroll a couple hundred more pixels to reveal the footer div below the main content div.

I allowed this in my landing page by finding out the height of the body necessary to facilitate this extra space at the bottom and setting the height using height: 1720px; on the body itself. However, I'd like to implement this in a way that it does not need to be constant, as I fear browsers and devices may have different rendered heights for the main content div and I'd like to use this on multiple pages without having to individually hard code the body height.

I tried using JavaScript to find the height of the main content div (using clientHeight, which seems to work perfectly) and add a couple hundred pixels to that number for the height of the body as follows:

document.body.height = document.getElementById("page-main").clientHeight + 400; 

and also tried changing the following:

document.body.style.height
document.body.style.paddingBottom

This does not change the height of the body at all. I tried using a similar approach to change the body's background to red, which works, but for some reason it just refuses to change the height specifically. I've tried placing this script in the head, above the body, and at the end of the body. Doesn't help. Finding the clientHeight of the main content div works fine, adding 400 to that number seems easy enough, and I know the document definitely has a body, so I'm very confused as to why it could possibly be that JavaScript refuses to change the height of the body.

I've checked the console in Edge and Chrome and it seems there's no issue, so I'm pletely lost here. Normally I can find answers online and I've never had to ask for help but at this point I feel like it's such a simple question and I have no idea why it won't work.

Sorry if this question is't written well, but does anybody have an idea of why JavaScript might not be allowing the changing of the height of the body?

TL;DR:

  • content div is positioned absolute and can change depending on scenario

  • footer div is positioned static on the bottom and is supposed to be revealed below the content div by allowing user to scroll a couple hundred pixels below the end of the content div

  • I want to achieve this by altering the height of the body, which works perfectly through hardcoding in html but for some reason JavaScript refuses to change the height of the body

Share Improve this question edited Mar 1, 2018 at 7:52 John M asked Mar 1, 2018 at 7:30 John MJohn M 811 gold badge2 silver badges4 bronze badges 3
  • Hey John - It's easier to get a helpful response here with some code. You may want to provide a minimal, runnable example so people can point you the right direction rather than rebuilding the scenario from your description. – JasonB Commented Mar 1, 2018 at 7:34
  • Please click <> and provide a minimal reproducible example – mplungjan Commented Mar 1, 2018 at 7:34
  • 1 Thankyou for the advice @JasonB Gerry's answer solved my problem but I will do so regardless in the interest of anybody who reads this. – John M Commented Mar 1, 2018 at 7:47
Add a ment  | 

3 Answers 3

Reset to default 8

Try it like this:

document.body.style.height = document.getElementById("page-main").clientHeight + 400 + 'px';

You have to specify the units to get a proper result. Like you would do in CSS.

Setting the height of the body element, the way you want in your question, is plicated by it's relationship with html element and their default CSS (like position: static on body), and by the overflow property. Read more here.

From my experiments on the chrome console, you can't set body height via document.body.clientHeight, it seems to be read-only. You'll need to set height (and possibly overflow) properties in CSS (via document.body.style for javascript).

However, I think the best solution for the effect you want doesn't involve setting body (or html) properties at all. Try this:

  1. Let the footer element by default have CSS: display: none

  2. Detect when user has scrolled to the bottom of the page (using jQuery or scrollTop) or bottom minus some offset

  3. Change the footer's CSS to display: block (by toggling classes preferably, or editing the style property). This will automatically increase the body's scrollbar to acmodate the footer.

  4. When user scrolls back up beyond the footer (or point 2 is false), you set it's CSS to display: none again.

With the above approach, there is no need to hard code or know before hand the height of your footer and non-footer content. You don't need to mess with html or body element CSS. You can also apply CSS animations if you want!

document.body.height = document.getElementById("page-main").clientHeight + 400;
#page-main {
    position:absolute;
    width:100%;
    height:2000px;
    z-index:2;
    background:#eeeeee;
}

#footer {
    position:fixed;
    width:100%:
    bottom:0px;
    height:400px;
    z-index:1;
    background:#aaaaaa;
}
<body>

<div id='page-main'>main</div>

<div id='footer'>footer</div>

</body>

emphasized text

发布评论

评论列表(0)

  1. 暂无评论