[英]How to make a scroll footer to fixed then back to scroll
Hello all, I have seen all of these sticky footer script and sticky sidebar scripts! 大家好,我已经看过所有这些粘性页脚脚本和粘性侧栏脚本! what I want is something similar, I have a long page with a footer and then more content below the footer, I would like the page to only show the footer when the content reaches the footer, once the footer shows on the screen, it gets fixed to the bottom so I can keep on scrolling the page and view the "more content" and when scrolling back up, the footer detaches from fixed at the bottom back to normal! 我想要的是类似的东西,我有一个长页脚的页面,然后在页脚下面有更多内容,我希望页面仅在内容到达页脚时显示页脚,一旦页脚在屏幕上显示,它就会固定在底部,因此我可以继续滚动页面并查看“更多内容”,并且在向上滚动时,页脚会从固定在底部的位置分离回正常!
I have attached a screenshot to maybe help explaining it better! 我附上了屏幕截图,可能有助于更好地解释它!
Well it requires some steps... 好吧,这需要一些步骤...
See demo here : http://jsfiddle.net/techsin/MgQQm/2/embedded/result/ 在此处查看演示 : http : //jsfiddle.net/techsin/MgQQm/2/embedded/result/
See code here: http://jsfiddle.net/techsin/MgQQm/2/ 在此处查看代码: http : //jsfiddle.net/techsin/MgQQm/2/
$footer = $('#footer');
$win = $(window);
var ipos = $footer.offset().top;
var wpos, space, width;
width = $footer.width();
function h(e) {
wpos = $win.scrollTop();
space = $win.height() - $footer.height();
if ((wpos + space) > ipos) {
$footer.addClass('fixed');
$footer.width(width);
} else {
$footer.removeClass('fixed');
}
}
$(window).scroll(h);
<div id="content"></div>
<div id="moreContent"></div>
<div id="footer"></div>
css 的CSS
#content {
height:1000px;
width:400px;
position: relative;
z-index: 2;
background-color:red;
}
#moreContent{
height:500px;
width:450px;
margin:0px 0px 100px 0px;
position: relative;
z-index: 0;
background-color:yellow;
}
#footer {
position: fixed;
bottom: 0;
width:400px;
left: 8px;
right: 0;
height: 100px;
z-index: 1;
background-color:blue;
}
In css itself before, you may keep it to position:fixed;
在CSS之前,您可以将其保持在position:fixed;
. 。
Or use JQuery for that, 或为此使用JQuery,
$('footer').css({position:'fixed'});
Or pure js, 还是纯js,
document.getElementByTagName('footer').style.position = 'fixed';
To make it scrollable later use jQuery, 要使其在以后滚动,请使用jQuery,
$('footer').css({position:'relative'});
Or use javascript, 或使用javascript,
document.getElementByTagName('footer').style.position = 'relative';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.