jQuery粘性页脚
詹姆斯在这里!我已经尝试了大约两个小时来获得一个粘性页脚,但我似乎一直在搞砸CSS。我正在寻找jQuery可以处理的脚本。我了解大多数脚本是如何工作的(这很令人惊讶,因为我只是在学习),但无论页脚的高度是多少,我都需要脚本工作,因为它在我页面的CSS文件中没有设置动态高度。有人能为粘性页脚提供工作脚本吗?我希望页脚本身始终位于页面底部,而不是固定位置。内容元素是#posts,页脚区域是ID为#bottom的元素。下面是一个页面示例:确定。 HTML: jQuery:jQuery粘性页脚,jquery,css,footer,sticky,Jquery,Css,Footer,Sticky,詹姆斯在这里!我已经尝试了大约两个小时来获得一个粘性页脚,但我似乎一直在搞砸CSS。我正在寻找jQuery可以处理的脚本。我了解大多数脚本是如何工作的(这很令人惊讶,因为我只是在学习),但无论页脚的高度是多少,我都需要脚本工作,因为它在我页面的CSS文件中没有设置动态高度。有人能为粘性页脚提供工作脚本吗?我希望页脚本身始终位于页面底部,而不是固定位置。内容元素是#posts,页脚区域是ID为#bottom的元素。下面是一个页面示例:确定。 HTML: jQuery: $(document).re
$(document).ready(function(){
var footer_height=$("#footer").height();
$("#wrapper").css({
'padding-bottom' : footer_height
});
});
我必须警告您,jquery.height()函数可能无法正常工作,因此请小心填充和边距(只需将边距/填充值添加到“footer\u height”中,您就可以了)。我刚刚创建了一个页脚有粘性的页面 这将创建一个页眉和页脚各为55px的页面,页脚粘贴在浏览器窗口的最底部 下面是我最后做的: HTML:
如果我理解正确,就不需要jQuery。简单明了
我把这个放在这里
<!DOCTYPE html>
<html>
<head>
<title>jQuery Sticky footer</title>
<!-- include jQuery -->
<script src="jquery-2.1.0.min.js"></script>
<!-- include our scripts -->
<script type="text/javascript">
$(function () {
// sticky footer
(function () {
var
$window = $(window),
$body = $(document.body),
$footer = $("#footer"),
condition = false,
resizing = false,
interval = 500
;
function positionFooter() {
if (resizing) {
setTimeout(function(){
if(resizing == false) {
positionFooter();
}
}, interval);
return true;
}
var
footer_position = $footer.css('position'),
body_height = $body.height(),
window_height = $window.height(),
footer_height = $footer.outerHeight();
if (footer_position == 'absolute') {
condition = body_height + footer_height < window_height
}
else {
condition = body_height < window_height
}
if (condition) {
$footer.css('position', 'absolute').css('bottom', 0);
}
else {
$footer.css('position', 'relative');
}
resizing = setTimeout(function () {
resizing = false;
}, interval);
return true;
}
$window.bind("load", function () {
positionFooter()
});
$window.resize(positionFooter);
}());
});
</script>
<style>
body {
text-align: center;
}
#header {
width: 100%;
background-color: green;
color: white;
height: 100px;
}
#footer {
width: 100%;
background-color: blue;
color: white;
}
</style>
</head>
<body>
<header id='header'>
Header content
</header>
<div id='content'>
Content is here!
</div>
<footer id='footer'>
Sticky footer content
</footer>
</body>
</html>
jQuery粘性页脚
$(函数(){
//粘脚
(功能(){
变量
$window=$(window),
$body=$(document.body),
$footer=$(“#footer”),
条件=假,
调整大小=错误,
间隔=500
;
函数positionFooter(){
如果(调整大小){
setTimeout(函数(){
如果(调整大小==false){
positionFooter();
}
},间隔);
返回true;
}
变量
页脚位置=$footer.css('position'),
body_height=$body.height(),
窗口高度=$window.height(),
页脚高度=$footer.outerHeight();
如果(页脚位置==“绝对”){
条件=车身高度+页脚高度<车窗高度
}
否则{
条件=车身高度<车窗高度
}
如果(条件){
$footer.css('position','absolute').css('bottom',0);
}
否则{
$footer.css('position','relative');
}
调整大小=设置超时(函数(){
调整大小=错误;
},间隔);
返回true;
}
$window.bind(“加载”,函数(){
positionFooter()
});
$window.resize(位置页脚);
}());
});
身体{
文本对齐:居中;
}
#标题{
宽度:100%;
背景颜色:绿色;
颜色:白色;
高度:100px;
}
#页脚{
宽度:100%;
背景颜色:蓝色;
颜色:白色;
}
标题内容
内容在这里!
粘性页脚内容
JQuery
function getWndowSize()
{
var windows_height=$(windows).height();
var current_height=windows_height-100;/*change values of 100 how much u need based on your requirement*/
$("#wrapper").css({'min-Height' : current_height});
}
代码:
<body onload="getWndowSize()">
<div id="container">
<div id="wrapper">
<!-- CONTENT GOES HERE -->
</div>
<div id="footer">
<!-- FOOTER GOES HERE -->
</div>
</div>
试试看,因为它在我的页面上运行得很好。
我知道现在回答这个问题太晚了,但对我来说也很难,我在这个伟大的地方找到了一些东西,我通过了这个问题,我很想分享一个代码,它对我帮助很大 您可以在下面的演示中找到它 我希望这能帮助一些人,因为我总是从这里的其他人那里得到帮助 谢谢你
$(文档).ready(函数(){
$(窗口).on(“滚动”,函数(){
var footer_height=$(“#footer”).outerHeight();
var dim_height=$(“.dim”).outerHeight();
var scrollHeight=$(document.height();
var scrollPosition=$(窗口).height()+$(窗口).scrollTop();
如果((滚动高度-滚动位置)/scrollHeight==0){
//当滚动到页面底部时
$(“.dim”).removeClass(“dim固定”);
$(“.dim”).addClass(“dim static”).css({
“底部”:页脚高度,
});
}否则{
$(“.dim”).removeClass(“dim static”);
$(“.dim”).addClass(“dim fixed”).css({'bottom':0,});
}
});
}); //文件准备功能结束
正文{
保证金:0px自动;
背景:#ffffff;
字体大小:14px;
颜色:#4444;
字体系列:“开放式Sans”,无衬线;
字体大小:300;
}
.第1点{
宽度:100%;
保证金:0自动;
}
.clearfix:之前,.clearfix:之后{
显示:表格;
内容:“;
}
#页脚{
z指数:104;
显示:块;
}
页脚。页脚{
宽度:100%;
背景:#333333;
颜色:#fff;
边框顶部:2件白色实心;
位置:绝对位置;
左:0;
}
footer.page-footer>div{
填充:30px 0 40px;
最小高度:162px;
保证金:0自动;
位置:相对位置;
}
导航页脚菜单{
位置:相对位置;
浮动:左;
宽度:75%;
右侧填充:30px;
显示:块;
}
导航页脚菜单>ul{
利润上限:-3px;
}
正文:非([class*=“静态页面”])ul,正文:非([class*=“静态页面”])li{
列表样式:无;
}
导航页脚菜单>ul>li{
显示:内联块;
宽度:33.33%;
保证金:0;
填充:0;
边界:0;
}
导航页脚菜单a{
文字装饰:无;
颜色:#fefefe;
空白:nowrap;
文本溢出:省略号;
溢出:隐藏;
字号:1.071em;
填充:0 10px 8px;
垂直对齐:顶部;
显示:内联块;
}
.页脚数据{
字体大小:0.929em;
颜色:#A0;
溢出:隐藏;
}
/*页脚额外菜单容器*/
.暗静态{
位置:绝对位置;
左边距:自动;
右边距:自动;
背景:ddd*/
左:0;
右:0;
宽度:100%;
文本对齐:居中;}
.昏暗固定{
P
.ui-frame {
width: 100%;
height: 55px;
background: #000000;
font-family: Segoe UI, Arial, sans-serif;
color: #ffffff;
text-align: right;
vertical-align: middle;
font-size: 16px;
}
.ui-frame-header {
position: absolute;
top: 0;
}
.ui-mainContent {
position: absolute;
top: 55px;
bottom: 55px;
background: #ffffff;
font-family: Segoe UI, Arial, sans-serif;
}
.ui-frame-footer {
position: absolute;
bottom: 0
}
<!DOCTYPE html>
<html>
<head>
<title>jQuery Sticky footer</title>
<!-- include jQuery -->
<script src="jquery-2.1.0.min.js"></script>
<!-- include our scripts -->
<script type="text/javascript">
$(function () {
// sticky footer
(function () {
var
$window = $(window),
$body = $(document.body),
$footer = $("#footer"),
condition = false,
resizing = false,
interval = 500
;
function positionFooter() {
if (resizing) {
setTimeout(function(){
if(resizing == false) {
positionFooter();
}
}, interval);
return true;
}
var
footer_position = $footer.css('position'),
body_height = $body.height(),
window_height = $window.height(),
footer_height = $footer.outerHeight();
if (footer_position == 'absolute') {
condition = body_height + footer_height < window_height
}
else {
condition = body_height < window_height
}
if (condition) {
$footer.css('position', 'absolute').css('bottom', 0);
}
else {
$footer.css('position', 'relative');
}
resizing = setTimeout(function () {
resizing = false;
}, interval);
return true;
}
$window.bind("load", function () {
positionFooter()
});
$window.resize(positionFooter);
}());
});
</script>
<style>
body {
text-align: center;
}
#header {
width: 100%;
background-color: green;
color: white;
height: 100px;
}
#footer {
width: 100%;
background-color: blue;
color: white;
}
</style>
</head>
<body>
<header id='header'>
Header content
</header>
<div id='content'>
Content is here!
</div>
<footer id='footer'>
Sticky footer content
</footer>
</body>
</html>
function getWndowSize()
{
var windows_height=$(windows).height();
var current_height=windows_height-100;/*change values of 100 how much u need based on your requirement*/
$("#wrapper").css({'min-Height' : current_height});
}
<body onload="getWndowSize()">
<div id="container">
<div id="wrapper">
<!-- CONTENT GOES HERE -->
</div>
<div id="footer">
<!-- FOOTER GOES HERE -->
</div>
</div>