The stick div element on the footer

I am trying to make a stick div in the footer on a window scroll. my code works almost right, but cannot figure out what is missing.

function checkOffset() {
  var eTop = $('#footer').offset().top;
  var chatTop = $('#chatArea').offset().top + $('#chat').innerHeight();
  var zero = ($(window).innerHeight() + $(window).scrollTop());
  var posFooter = eTop - zero;
  var posChat = chatTop - zero;
  if (posChat >= posFooter - 2) {
    $('#chatArea').css('bottom', -posFooter);
  } else {
    $('#chatArea').css('bottom', 0);
  }
  console.log(posFooter);
}
$(document).scroll(function() {
  checkOffset();
});
#wrapper {
  height: 540px;
  background-color: #ffffff;
}
#chatArea {
  width: 100%;
  position: fixed;
  bottom: 0;
  padding: 10px;
  border: 1px solid red;
}
#footer {
  background-color: #000000;
  color: #ffffff;
  padding: 20px;
  border-top: 2px solid #007cdc;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="wrapper">
  ...
  <div id="chatArea"></div>
</div>
<div id="footer"></div>
Run codeHide result

This is the script I made for this case (the height of the wrapper was for this example only). https://jsfiddle.net/v92qk4tn/

early.

+4
source share
2 answers

Just figured out how to make this work.

JS function:

function checkOffset() {
    var eTop = $('#footer').offset().top;
    var zero = ($(window).innerHeight()+$(window).scrollTop());
    var posFooter = eTop - zero;

    if(posFooter <= 0) {
        $('#chatArea').css('bottom', -posFooter);
    }
    else {
        $('#chatArea').css('bottom', 0);
    }
}

Thanks everyone!

+1
source

I think you just skip the sleeping name Id #chatArea https://jsfiddle.net/v92qk4tn/2/

+1
source

Source: https://habr.com/ru/post/1653683/


All Articles