固定位置直到到达页脚

时间:2014-11-17 17:28:54

标签: javascript jquery css fixed sticky

我一直在尝试创建一个侧边栏菜单,它将固定定位到页脚,然后在用户从那里向下滚动时停在页脚正上方。 我看过很多其他人使用的jquery代码,但是,即使在页脚之后,我的侧面菜单框仍然保持固定状态。我已在此链接保存了测试样本:http://jsfiddle.net/9281gzrh/2/

$(document).ready(function() {
  function isScrolledTo(elem) {
    var docViewTop = $(window).scrollTop(); //num of pixels hidden above current screen
    var docViewBottom = docViewTop + $(window).height();
    var elemTop = $(elem).offset().top; //num of pixels above the elem
    var elemBottom = elemTop + $(elem).height();
    console.log("Elem Bottom: "+elemBottom);
    console.log("Return: "+ (elemTop <= docViewTop));
    return ((elemTop <= docViewTop || elemTop >= docViewTop));
  }
  var catcher = $('.catcher');
  var sticky = $('.menu');
  var footer = $('#footer');
  var footTop = footer.offset().top;
  var lastStickyTop = sticky.offset().top;
  $(window).scroll(function() {
    if(isScrolledTo(sticky)) {
      sticky.css('position','fixed');
      sticky.css('top','2px');
    }
    var stopHeight = catcher.offset().top + catcher.height();
    var stickyFoot = sticky.offset().top + sticky.height();

    if(stickyFoot > footTop -10){ //Check if sticky's foot passes footer's top
      console.log("Top of Footer");
      sticky.css({
        position:'absolute',
        top: (footTop - 20) - sticky.height()
      });
    } else {
      if ( stopHeight > sticky.offset().top) {
        console.log("Default position");
        sticky.css('position','absolute');
        sticky.css('top',stopHeight);
      }
    }
});
}); 

CSS:
.menu {
    display: inline-block;
    overflow: hidden;
    padding: 8px;
	background-color:#1668af;
  	opacity:0.8;
    color:white;
    font-family: arial;
  	border-radius:6px;
	position: fixed;
  	top:20%;
}
.menu > * {
    display: inline-block;
    vertical-align: top;
}
.menu-handle {
    padding: 10px;
    background: #1668af;
    cursor: pointer;
}
.menu-menu {
    background:rgba(0,0,0,0.8);
    border-color: black;
    border-radius:6px;	
}
.menu-items {
    list-style: none;
    margin: 0;
    padding: 16px 69% 16px 100px;
}
.menu-closed .menu-menu {
    width: 0;
    height: 0;
}

#footer {
 	background-color:#dee5eb; 
}
HTML:
<br /><br />
<br />
<br /><br />
<br />
<br /><br />
<br />
<br />
<br /><br />
<br />
<br /><br />
<br />
<br /><br />
<br />
<br />
<br /><br />
<br />
<br /><br />
<br />
<br /><br />
<br />
<br />
<br /><br />
<br />
<br /><br />
<br />
<br /><br />
<br />
<br />
<div class="catcher">
    <!--catcher-->
</div>
<div class="menu menu-closed">
    <div class="menu-handle">
    </div>
        <div class="menu-menu">
    </div>
</div>
    <div class="catcher">
    <!--catcher-->
</div>
    
    <div id="footer">---------------------------------------------------</div>
    <br /><br />
<br />
<br /><br />
<br />
<br /><br />
<br />
<br />
<br /><br />
<br />
<br /><br /><br /><br />
<br />
<br /><br />
<br />
<br /><br />
<br />
<br />
<br /><br />
<br />
<br /><br />

0 个答案:

没有答案