[英]jQuery Stop Fixed Sidebar When Reach Footer
I have a sidebar widget on my website, when I scroll the page, sidebar will become position fixed. 我的网站上有一个侧边栏小部件,当我滚动页面时,侧边栏将变为固定位置。 But this sidebar obstruct the footer. 但是此侧边栏阻碍了页脚。 I want the sidebar stop fixed when touch the footer, something like this script http://jsfiddle.net/bryanjamesross/VtPcm/ 我希望边栏在触摸页脚时停止固定,类似于此脚本http://jsfiddle.net/bryanjamesross/VtPcm/
This is my website http://www.creativebrain.web.id/view/gadget/171/performa-buas-lg-g2-menantang-samsung-galaxy-s4 这是我的网站http://www.creativebrain.web.id/view/gadget/171/performa-buas-lg-g2-menantang-samsung-galaxy-s4
Here is my code to fixed position the sidebar 这是我固定侧边栏位置的代码
<script type="text/javascript">
$(function() {
var nav = $('#gads300x600');
var navHomeY = nav.offset().top;
var isFixed = false;
var $w = $(window);
$w.scroll(function() {
var scrollTop = $w.scrollTop();
var shouldBeFixed = scrollTop > navHomeY;
if (shouldBeFixed && !isFixed) {
nav.css({
position: 'fixed',
top: '90px',
left: nav.offset().left,
width: nav.width()
});
nav.css('z-index',999);
isFixed = true;
}
else if (!shouldBeFixed && isFixed)
{
nav.css({
position: 'static'
});
isFixed = false;
}
});
});
</script>
I have modified that code like script in jsfiddle above, but I think something wrong in my code 我已经修改了上面jsfiddle中的脚本之类的代码,但是我认为我的代码有问题
<script type="text/javascript">
$(function() {
var nav = $('#gads300x600');
var footer = $('#copyright');
var navHomeY = nav.offset().top;
var navFooterY = footer.offset().top;
var isFixed = false;
var $w = $(window);
$w.scroll(function() {
var scrollTop = $w.scrollTop();
var shouldBeFixed = scrollTop > navHomeY;
var maxY = navFooterY - nav.outerHeight();
if (shouldBeFixed && !isFixed) {
if (scrollTop < maxY) {
nav.css({
position: 'absolute',
top: '0px',
left: nav.offset().left,
width: nav.width()
});
nav.css('z-index',1000);
}
else{
nav.css({
position: 'fixed',
top: '90px',
left: nav.offset().left,
width: nav.width()
});
nav.css('z-index',1000);
}
isFixed = true;
}
else if (!shouldBeFixed && isFixed)
{
nav.css({
position: 'static'
});
isFixed = false;
}
});
});
</script>
Please use below script to make sidebar fixed. 请使用以下脚本修复边栏。
<script type="text/javascript">
$(document).ready(function(e) {
var containerTop = $('.container').offset().top;
$(window).scroll(function(){
var scrollT = $(window).scrollTop();
if(scrollT > containerTop){
$('.sidebar').css({position:'fixed',zIndex:1000,top:'110px'});
}else{
$('.sidebar').css({position:'relative',zIndex:1000,top:'0px'});
}
});
});
</script>
$(function () {
var nav = $('#gads300x600');
var footer = $('#copyright');
var navHomeY = nav.offset().top;
var navFooterY = footer.offset().top;
var isFixed = false;
var $w = $(window);
$w.scroll(function () {
var scrollTop = $w.scrollTop();
var topSidebar = navHomeY;
var topFooter = navFooterY;
var bottomSidebar = nav.offsetHeight + topSidebar;
var footerIsOnSight = (scrollTop + $window.innerHeight) >= topFooter;
var shouldBeFixed = (scrollTop + $window.innerHeight) >= bottomSidebar ;
var maxY = navFooterY - nav.outerHeight();
if (!isFixed && shouldBeFixed) {
nav.css({
position: 'fixed',
top: '90px',
left: nav.offset().left,
width: nav.width()
});
nav.css('z-index', 1000);
isFixed = true;
}
if ((isFixed && (scrollTop <= 0))) {
nav.css({
position: 'absolute',
top: '0px',
left: nav.offset().left,
width: nav.width()
});
nav.css('z-index', 1000);
isSticked = false;
} else {
if (isSticked && footerIsOnSight) {
nav.css({
position: 'static'
});
isFixed = false;
}
}
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.