首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当滚动超过元素底部时触发事件?

当滚动超过元素底部时触发事件?
EN

Stack Overflow用户
提问于 2013-12-19 12:10:52
回答 2查看 27.3K关注 0票数 16

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

代码语言:javascript
复制
$("#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的底部,而不是像现在一样显示顶部。帮助?

EN

回答 2

Stack Overflow用户

发布于 2013-12-19 12:15:34

我认为如果你把div的高度加到顶部偏移量上,你会得到你想要的行为

代码语言:javascript
复制
$("#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();
    }
});
票数 35
EN

Stack Overflow用户

发布于 2019-08-25 06:07:56

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

解决方案:

我知道这有点晚了,但是我有一个非常好的代码片段,其中我在页面中间的div/文章中包装了一个动态表格。

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

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

一旦用户超出了表的范围,我就隐藏了固定的标题。

这是HTML:

代码语言:javascript
复制
<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:

代码语言:javascript
复制
<style>
#header-fixed {
  position: fixed;
  top: 50px;
  display: none;
}
.ppr{position:relative;}
</style>

这里的是document.ready js/jQuery:

代码语言:javascript
复制
//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();
  }
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20673293

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档