簡體   English   中英

如果div的底部和屏幕匹配,則將div粘在屏幕上

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

大家好,我正在做一個項目,在該項目中,當頁面底部位於屏幕底部時,我必須將div粘貼到屏幕上(禁用滾動)。 我在頁面中有兩個div,並且兩個div的高度都是可變的。 我要貼上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>

我不知道該怎么辦,否則請幫助我

使功能在滾動時觸發。 該功能的主要目的是查看屏幕高度和div底部之間的差異。 一旦差異小於或等於零,則將css位置修改為固定。 這對你有幫助

(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