【问题标题】:Trigger event when scroll past bottom of element?滚动超过元素底部时触发事件?
【发布时间】:2013-12-19 04:10:51
【问题描述】:

所以在我的网站上,我在网站的最顶部有一个静态标题——它没有固定在视口的顶部。但是,我想做的是一旦用户滚动到这个 div 的底部,就会出现一个固定的导航栏。我的代码几乎可以工作,但只在 div 的顶部偏移处触发,这是页面的最顶部。这是我的代码:

$("#header-2").hide(); // hide the fixed navbar initially

var topofDiv = $("#header-container").offset().top; //gets offset of header
$(window).scroll(function(){
    if($(window).scrollTop() > topofDiv){
       $("#header-2").show();
    }
    else{
       $("#header-2").hide();
    }
});

再次,一旦用户滚动到#header-container 的底部,而不是像现在这样的顶部,我需要触发显示固定导航栏。帮忙?

【问题讨论】:

  • 只需将元素的高度添加到它的偏移量。
  • 要获得底部只需将#header-container 的高度添加到topfoDiv..

标签: javascript jquery html


【解决方案1】:

我认为,如果您将 div 的高度添加到顶部偏移量,您将获得您想要的行为

$("#header-2").hide(); // hide the fixed navbar initially

var topofDiv = $("#header-container").offset().top; //gets offset of header
var height = $("#header-container").outerHeight(); //gets height of header

$(window).scroll(function(){
    if($(window).scrollTop() > (topofDiv + height)){
       $("#header-2").show();
    }
    else{
       $("#header-2").hide();
    }
});

【讨论】:

    【解决方案2】:

    目标:动态表格上的响应式(on.resize)粘性标题,一旦表格超出范围(超过窗口滚动),它也会隐藏。

    解决方案:

    我知道这有点晚了,但我有一个非常好的 sn-p,我在页面中间的 div/article 中包含了一个动态表。

    您可以在yardpenalty.com/ppr-rankings 看到工作示例

    我还有一个固定的标题/响应式网站,所以我也必须适应菜单导航栏。

    一旦用户超出表格,我就会隐藏固定标题。

    这里是 HTML:

    <article id="ppr" class="ppr">
    <table id="table-1" class="header-table cheat no-margin-auto xs-small table table-condensed 
     table-hover">
    <tbody>
    <tr>
    <td><b>PPR Ranking</b><br>
    <em>Note*</em>
    </td>
    <td><b>First Name</b></td>
    <td><b>Last Name</b></td>
    <td><b>Team</b></td>
    <td><b>Pos</b></td>
    </tr>
    </tbody>
    </table>
    <table id="header-fixed"></table>
    <table id="1" class="first cheat no-margin-auto xs-small table table-condensed table-hover"><tbody>
    <tr class="rb">
    <td>1<span class="abrev"><i class="fa fa-arrow-up">&nbsp;+3</i></span></td>
    <td>Saquon</td>
    <td>Barkley</td>
    <td>NYG</td>
    <td>RB</td>
    </tr>
    <tr class="rb">
    <td>2<span class="abrev"><i class="fa fa-arrow-down">&nbsp;-1</i></span></td>
    <td>Christain</td>
    <td>McCaffrey</td>
    <td>CAR</td>
    <td>RB</td>
    </tr>....
     <!--Dynamic table//-->
    </table>
    </article>
    

    这里是 CSS:

    <style>
    #header-fixed {
      position: fixed;
      top: 50px;
      display: none;
    }
    .ppr{position:relative;}
    </style>
    

    这里是document.ready js/jQuery:

    //sticky table header
     var tableOffset = jQuery("#table-1").offset().top;
        var header = jQuery("#table-1").clone();
    var fixedHeader = jQuery("#header-fixed").append(header).width(header.parent().width());
    
    jQuery(window).on("resize",function(){
    //adjust the global tableOffset for sticky table header
    tableOffset = jQuery("#table-1").offset().top;
    
    if(fixedHeader.not(":hidden")){
      //adjust sticky table header size
      jQuery("#header-fixed").width( jQuery("#header-fixed").parent().width());
    }
       $(window).trigger("scroll");
    });
    jQuery(window).on("scroll", function() {
      var offet = $(this).scrollTop();
      var height = jQuery("#ppr").outerHeight() + tableOffset;
     // console.log("window offset:" + offet + "ppr + table offset:"+height+"window:"+jQuery(this).scrollTop());
    
      if (offet >= tableOffset && fixedHeader.is(":hidden") && offet < height) {
        fixedHeader.show();
      } 
      //lets hide header when reach last two records
      if (offet < tableOffset || offet > (height-80)) { 
        fixedHeader.hide();
      }
    });
    

    【讨论】:

      猜你喜欢
      • 2019-09-25
      • 1970-01-01
      • 1970-01-01
      • 2013-04-03
      • 2016-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-18
      相关资源
      最近更新 更多