简体   繁体   English

当用户使用Jquery和Waypoints滚动到页面顶部时如何触发事件?

[英]How to trigger event when user scrolls to the top of the page using Jquery and Waypoints?

I have a relatively standard page layout. 我有一个相对标准的页面布局。

I have multiple 我有多个

<section id="unique-id">Content</section> 

which hold the content of the page. 其中包含页面的内容。 I also have shim at the top of the page due to a fixed navbar (height: 100px). 由于导航栏固定(高度:100像素),我在页面顶部也安装了垫片。

as the user scrolls to the bottom of the page, as they reach each section, I trigger a Waypoints event, with an offset: 100. This makes sure that I am not hiding content under the menu as I go. 当用户滚动到页面底部(到达每个部分时),我触发了一个Waypoints事件,其偏移量为100。这确保了我不会在菜单中隐藏内容。

the issue is that when the user scrolls back up, they reach the top of the page/ the first section, with the shim above it, but the waypoints event is not triggered. 问题是,当用户向后滚动时,他们到达页面/第一部分的顶部,且垫片位于其上方,但不会触发路标事件。 I have tried playing with the offset and making it say 150 but it still does not trigger at the top. 我试过使用偏移量,使其表示为150,但仍无法在顶部触发。

My js code: 我的js代码:

// FOR SCROLLING
$('body section').waypoint(function() {
    var id = $(this).attr('id');
    var subTarget = $('#sub-nav li a[href=#'+id+']');
        $('.current-sub-nav').removeClass('current-sub-nav');
        subTarget.parent('li').addClass('current-sub-nav');

}, {offset: 101 });

My html is formed like: 我的html格式如下:

<header></header>
<div class="shim"></div>
<section id="uniqueid">Content</section>
<Section id="uniqueid2">Content 2</section>
<Section id="uniqueid3">Content 3</section>

The trigger happens fine between 1/2 and 2/3 but not when I scroll back up. 触发发生在1/2和2/3之间,但是当我向上滚动时却不能。

You were correct in trying to update your offset, because that is the issue. 您试图更新偏移量是正确的,因为这就是问题所在。 Basically, the top of the first section was never coming back across the top of the viewport and so it was not firing the waypoint. 基本上,第一部分的顶部永远不会再回到视口顶部,因此它不会触发航路点。 If you were to set the offset to 0, you would have had the same problem, except the waypoint would not trigger for the third section. 如果将偏移量设置为0,则将遇到相同的问题,除了第三部分不会触发航路点。

Here are two solutions; 这是两个解决方案; I came up with some artificial markup to work against. 我想出了一些人工标记来应对。 They may need to be modified slightly to match your specific markup; 它们可能需要稍作修改以匹配您的特定标记。 given the lack of detail in the question itself. 鉴于问题本身缺乏细节。

First Solution Fiddle - This option maintains your markup pretty much the way described. First Solution Fiddle-此选项几乎按照描述的方式维护您的标记。 I switched the offset to 1%, and that seemed to have done the trick. 我将偏移量设置为1%,这似乎可以解决问题。

HTML 的HTML

<header></header>
<nav class="navbar navbar-inverse navbar-default navbar-fixed-top" role="navigation">
    <div class="container">
        <ul id="sub-nav" class="nav navbar-nav">
            <li class="active"><a href="#uniqueid">Content</a></li>
            <li><a href="#uniqueid2">Content2</a></li>
            <li><a href="#uniqueid3">Content3</a></li>
        </ul>
    </div>
</nav>
<div class="shim" style="margin-top: 50px;"></div>
<section id="uniqueid" style="height: 600px; background-color: red;">Content</section>
<section id="uniqueid2" style="height: 600px; background-color: white;">Content 2</section>
<section id="uniqueid3" style="height: 600px; background-color: blue;">Content 3</section>

Javascript Java脚本

// FOR SCROLLING
$('body section').waypoint(function () {
    var id = $(this).attr('id');
    var subTarget = $('#sub-nav li a[href=#' + id + ']');
    $('.active').removeClass('active');
    subTarget.parent('li').addClass('active');    
}, { offset: '1%' });





Second Solution Fiddle - This solution modifies your markup slightly by removing the shim and adding a margin to the top of the first container. 第二个解决方案小提琴 -此解决方案通过删除垫片并在第一个容器的顶部添加边距来稍微修改标记。

HTML 的HTML

<header></header>
<nav class="navbar navbar-inverse navbar-default navbar-fixed-top" role="navigation">
    <div class="container">
        <ul id="sub-nav" class="nav navbar-nav">
            <li class="active"><a href="#uniqueid">Content</a></li>
            <li><a href="#uniqueid2">Content2</a></li>
            <li><a href="#uniqueid3">Content3</a></li>
        </ul>
    </div>
</nav>
<section id="uniqueid" style="height: 1100px; background-color: red; margin-top: 51px;">Content</section>
<section id="uniqueid2" style="height: 1100px; background-color: white;">Content 2</section>
<section id="uniqueid3" style="height: 1100px; background-color: blue;">Content 3</section>

Javascript Java脚本

// FOR SCROLLING
$('body section').waypoint(function () {
    var id = $(this).attr('id');
    var subTarget = $('#sub-nav li a[href=#' + id + ']');
    $('.active').removeClass('active');
    subTarget.parent('li').addClass('active');

}, { offset: 50 });

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

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