简体   繁体   English

如果div的底部和屏幕匹配,则将div粘在屏幕上

[英]stick div to screen if bottom of div and screen matches

Hello all I am working on a project where in a page I have to stick the div to screen (disable scrolling) when its bottom is at the bottom of screen. 大家好,我正在做一个项目,在该项目中,当页面底部位于屏幕底部时,我必须将div粘贴到屏幕上(禁用滚动)。 I have two divs in the page and both the divs are of variable height. 我在页面中有两个div,并且两个div的高度都是可变的。 I want to stick the div2 and scroll the div1. 我要贴上div2并滚动div1。

<script>
  var divheight
  var scrolltop
  var screenheight
  if(divheight-scrolltop <= screenheight){ 
  /* now stick the div wherever it is i can not 
  use fixed position as both the divs are floating and
   fixing the position will make it to change the position*/ } 
   else { /*un stick the div*/ }
 </script>

i dont know what to put in if and else please help me 我不知道该怎么办,否则请帮助我

Make a function which fire on scroll. 使功能在滚动时触发。 The main aim of the function would be to see the difference between screen height and bottom of the div. 该功能的主要目的是查看屏幕高度和div底部之间的差异。 Once the difference is less than or equal to zero modify the css position to fixed. 一旦差异小于或等于零,则将css位置修改为固定。 This will help you 这对你有帮助

(function ($) {
  $('.DIV1').scroll(function () {
    var $this = $(this),
        win_ht = $(window).height(),
        div_ht = $this.height(),
        div_bot = $this.offset().top + div_ht;
    if (win_ht - div_bot <= 0) {
        $this.css({
            'position': 'fixed',
            'bottom': '0'
        })
    }
  });
})(jQuery);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM