Advertisement
Guest User

Untitled

a guest
Aug 26th, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. $(function() {
  2. var fix = $('.fixed'); //滚动悬浮块
  3. var end = $('.bottom'); //滚动到这个元素后结束固定
  4. var fixTop = fix.offset().top, //滚动悬浮块与顶部的距离
  5. fixHeight = fix.height(); //滚动悬浮块高度
  6. var endTop, miss; //结束元素与顶部的距离
  7.  
  8. $(window).scroll(function() {
  9. //页面与顶部高度
  10. var docTop = Math.max(document.body.scrollTop, document.documentElement.scrollTop);
  11.  
  12. //如果有结束块
  13. if (end.length > 0) {
  14. endTop = end.offset().top;
  15. miss = endTop - docTop - fixHeight;
  16. }
  17.  
  18. if (fixTop < docTop) {
  19. fix.css({'position': 'fixed'});
  20. if ((end.length > 0) && (endTop < (docTop + fixHeight))) {
  21. fix.css({top: miss}); //滚动悬浮块滑到结束块上时,top值为负,即慢慢隐藏出浏览器
  22. } else{
  23. fix.css({top: 0}); //滚动悬浮块未到结束块上时,top为0
  24. }
  25. } else {
  26. fix.css({'position': 'static'});
  27. }
  28. })
  29. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement