Javascript 如何在使用滚动上下移动元素时将其停止在页脚行

Javascript 如何在使用滚动上下移动元素时将其停止在页脚行,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我添加了一个元素,它可以随着scroll上下移动,但问题是它会随着footer而塌陷。下面是我正在使用的示例代码 $(document).ready(function () { var top = $('#comment').offset().top - parseFloat($('#comment').css('marginTop').replace(/auto/, 0)); $(window).scroll(function (event) { // what the y p

我添加了一个元素,它可以随着scroll上下移动,但问题是它会随着footer而塌陷。下面是我正在使用的示例代码

$(document).ready(function () {
  var top = $('#comment').offset().top - parseFloat($('#comment').css('marginTop').replace(/auto/, 0));
  $(window).scroll(function (event) {
    // what the y position of the scroll is
    var y = $(this).scrollTop();

    // whether that's below the form
    if (y >= top) {
      // if so, ad the fixed class
      $('#comment').addClass('fixed');
    } else {
      // otherwise remove it
      $('#comment').removeClass('fixed');
    }
  });
});
示例HTML代码

<div id="commentWrapper">
  <div id="comment">
    <form>
      <!-- take their response -->
    </form>
  </div>
</div>

我通过给出
bottom:somevalue
和它现在的工作状态来解决它