Как поддерживать нижний колонтитул внизу даже с динамической высотой сайта
Как сохранить div нижнего колонтитула всегда в нижней части окна, когда у меня есть страница, которая динамически устанавливает высоту (например, получить информацию из базы данных) с помощью CSS?
Если вы хотите работать с jQuery, я придумал это и отлично работает:
Установите CSS нижнего колонтитула:
#footer { position:absolute; width:100%; height:100px; }
Установите script:
<script>
x = $('#div-that-increase-height').height()+20; // +20 gives space between div and footer
y = $(window).height();
if (x+100<=y){ // 100 is the height of your footer
$('#footer').css('top', y-100+'px');// again 100 is the height of your footer
$('#footer').css('display', 'block');
}else{
$('#footer').css('top', x+'px');
$('#footer').css('display', 'block');
}
</script>
Этот script должен быть в конце вашего кода;
Ответы
Ответ 1
Я считаю, что вы ищете липкий нижний колонтитул
Попробуйте следующее: http://ryanfait.com/sticky-footer/
Из приведенной выше статьи:
layout.css:
* {
margin: 0;
}
html, body {
height: 100%;
}
.wrapper {
min-height: 100%;
height: auto !important;
height: 100%;
margin: 0 auto -142px; /* the bottom margin is the negative value of the footer height */
}
.footer, .push {
height: 142px; /* .push must be the same height as .footer */
}
/*
Sticky Footer by Ryan Fait
http://ryanfait.com/
*/
Страница html:
<html>
<head>
<link rel="stylesheet" href="layout.css" ... />
</head>
<body>
<div class="wrapper">
<p>Your website content here.</p>
<div class="push"></div>
</div>
<div class="footer">
<p>Copyright (c) 2008</p>
</div>
</body>
</html>
Ответ 2
Я думаю, что это решит все ваши проблемы:
<script>
$(document).ready(function() {
var docHeight = $(window).height();
var footerHeight = $('#footer').height();
var footerTop = $('#footer').position().top + footerHeight;
if (footerTop < docHeight) {
$('#footer').css('margin-top', 10+ (docHeight - footerTop) + 'px');
}
});
</script>
Вам нужен хотя бы элемент с #footer
Если вам не нужна полоса прокрутки, если содержимое будет соответствовать экрану, просто измените значение 10 на 0
Полоса прокрутки будет отображаться, если содержимое не подходит для экрана.
Ответ 3
Мое любимое решение jQuery/CSS для липкого нижнего колонтитула (нефиксированное):
CSS
html {
position: relative;
min-height: 100%;
}
footer {
display:none;
position: absolute;
left: 0;
bottom: 0;
height: auto;
width: 100%;
}
JQuery
function footerAlign() {
$('footer').css('display', 'block');
$('footer').css('height', 'auto');
var footerHeight = $('footer').outerHeight();
$('body').css('padding-bottom', footerHeight);
$('footer').css('height', footerHeight);
}
$(document).ready(function(){
footerAlign();
});
$( window ).resize(function() {
footerAlign();
});
DEMO:
http://codepen.io/anon/pen/ZQxQoR
Примечание: ваш нижний колонтитул должен начинаться с <footer>
и заканчиваться на </footer>
, чтобы использовать этот код как есть, или вы можете изменить код в соответствии с id/class нижнего колонтитула.
Ответ 4
Таким образом, простое решение
CSS
.footer_wrapper { width:100%; background-color:#646464; }
.footer_wrapper.fixed {position:fixed; bottom:0px;}
JS:
if ($(".Page").height()<$(window).height()){
$(".footer_wrapper").addClass("fixed");
}else{
$(".footer_wrapper").removeClass("fixed");
}
HTML:
<div class="Page">
/* PAGE CONTENT */
<div class="footer_wrapper" >
/* FOOTER CONTENT */
</div>
</div>
Ответ 5
Для создания фиксированного нижнего колонтитула на своей веб-странице используйте следующее:
CSS
body, html
{
margin: 0px; padding: 0px;
}
#footer
{
width: 100%;
height: 30px;
position: fixed;
bottom: 0px;
left: 0px;
right: 0px;
/*text-align, background-color, and other specific styles can be applied here. You can change the height from 30px to anything which suits your needs. You can even comment Left: 0px & Right: 0px, but to make sure that it works in all browsers, lets leave them there.*/
}
HTML:
/*Place this div anywhere on the page, inside the form tags.*/
<div id="footer">
/*Your text/elements goes here*/
</div>
Надеюсь, это поможет!
Приветствия,
Veno
Ответ 6
#footer {
position:fixed;
left:0px;
bottom:0px;
height:30px;
width:100%;
background:#999;
}
/* IE 6 */
* html #footer {
position:absolute;
top:expression((0-(footer.offsetHeight)+(document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight)+(ignoreMe = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop))+'px');
}
см. рабочий образец,
http://jsfiddle.net/RC3YX/
Ответ 7
Не уверен, что это то, что вы ищете:
<div style="position: fixed; bottom: 0px; left: 0px; right: 0px;">footer</div>
Ответ 8
Смотрите эти две статьи. http://ryanfait.com/sticky-footer/ и
http://css-tricks.com/snippets/css/fixed-footer/
Ответ 9
Я проверял этот вопрос, чтобы найти тот же ответ. Это было спрошено когда-то назад, и, возможно, это новая функция, добавленная jQuery. Но это простое решение, которое существует сейчас:
Просто добавьте data-position = "fixed" в тег div, если вы используете jQuery.
http://demos.jquerymobile.com/1.2.0/docs/toolbars/bars-fixed.html
<div data-role="footer" data-position="fixed">
<h5>footer - page 3</h5>
</div><!-- /footer -->
Надеюсь, что это поможет.