简体   繁体   English

将div更改为绝对位置时,它会跳到原始位置

[英]When changing div to position absolute it jumps to original position

I am currently trying to make a floating table of the right hand side of the screen, while having other content on the left. 我目前正在尝试在屏幕的右侧制作一个浮动表格,同时在左侧放置其他内容。

The left and right columns are separated using bootstrap class="col-md-6 . 左边和右边的列使用bootstrap class="col-md-6分隔。

making the table float is easy however i want it to stop floating at the bottom of the left hand div. 使表浮动很容易,但是我希望它在左div的底部停止浮动。

I followed this tutorial https://stackoverflow.com/a/8653109/9364403 我遵循了本教程https://stackoverflow.com/a/8653109/9364403

and my floating div does change to position:absolute; 我的浮动div确实更改为position:absolute; when it reaches the bottom of the div. 当它到达div的底部时。

However when the position is set to absolute, the table goes back to the position in the top of the div and does not stay at the bottom. 但是,如果将位置设置为绝对值,则表将返回到div顶部的位置,而不会停留在底部。 When i scroll back up a little bit the table goes back to floating. 当我向上滚动一点时,桌子又回到浮动状态。

How can i ensure that the flaoting div does not jump back up like the jsfiddle showing the comment link above? 如何确保浮动的div不会像显示上方评论链接的jsfiddle那样跳回? ( http://jsfiddle.net/Kkv7X/ ) http://jsfiddle.net/Kkv7X/

current html: 当前HTML:

<div class="container">
  <div class="row" id="">

    <div class="col-md-6">
    Content for the left hand column
    </div>

    <div class="col-md-6" style="position:relative;">
      <div class="positionfixed" id="fixedcard">
        <table class="table" id="recipetable">
          <tr class="tablerow">
            <th class="tableheader" style="width:45%;">header 1</th>
            <th class="tableheader" style="width:25%;">header 2</th>
            <th class="tableheader" style="width:30%;">header 3</th>
            <th class="tableheader" style="width:30%;">header 4</th>
          </tr>
          <tr class="tablerow">
            <td>row 1</td>
            <td></td>
            <td></td>
            <td></td>
          </tr>
          <tr class="tablerow">
            <td>row 2</td>
            <td></td>
            <td></td>
            <td></td>
          </tr>
          <tr class="tablerow">
            <td>row 3</td>
            <td></td>
            <td></td>
            <td></td>
          </tr>
          <tr class="tablerow">
            <td>row 4</td>
            <td></td>
            <td></td>
            <td></td>
          </tr>
          <tr class="tablerow" id="TableTotalRow">
            <td>row 5</td>
            <td ></td>
            <td ></td>
            <td ></td>
          </tr>
        </table>
      </div>
    </div>

  </div>
</div>
<div id="stop">
</div>

current js: 当前的js:

function checkOffset() {
    if($('#fixedcard').offset().top + $('#fixedcard').height() >= $('#stop').offset().top - 10){
      $('#fixedcard').css('position', 'absolute');
    }
    if($(document).scrollTop() + window.innerHeight < $('#stop').offset().top){
      $('#fixedcard').css('position', 'fixed'); // restore when you scroll up
    }
  }
  $(document).scroll(function() {
    checkOffset();
  });

current css: 当前的CSS:

 .positionfixed{
    position: fixed;
  }

I made a jsFiddle of what my code is currently doing here: https://jsfiddle.net/fjfkbrtn/12/ 我对我的代码当前在这里所做的事情做了jsFiddle: https ://jsfiddle.net/fjfkbrtn/12/

Thanks. 谢谢。

When you change to absolute just set the bottom CSS property to 0px and change it back when the position goes back to fixed , like so: 当您更改为absolute只需将bottom CSS属性设置为0px,然后在位置恢复为fixed时将其更改回原来的样子,如下所示:

function checkOffset() {
  if ($('#fixedcard').offset().top + $('#fixedcard').height() >= $('#stop').offset().top - 10) {
    $('#fixedcard').css('position', 'absolute');
    $('#fixedcard').css('bottom', '0px'); // ADD THIS
  }
  if ($(document).scrollTop() + window.innerHeight < $('#stop').offset().top) {
    $('#fixedcard').css('position', 'fixed'); 
     $('#fixedcard').css('bottom', 'initial'); // AND THIS 
  }
}
$(document).scroll(function() {
  checkOffset();
});

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

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