javascript - 如何在 jQuery 中从一个航点滚动到另一个航点?

标签 javascript jquery html css scroll

在我的网页中,我有 4 个“航点”,它们在菜单中有各自的链接。我需要的是将页面的滚动也绑定(bind)到这些航点。因此,当页面加载时,指针位于顶部并根据滚动方向,页面移动到下一个/上一个路径点。到目前为止,我已经想出了这种简单的方法,由于 scrollTo() 函数再次触发整个方法,它进入了滚动循环。

$(function () {
    var top = $(window).scrollTop();
    var currentWaypoint = 0;
    var previousWaypoint = 0;
    $(window).scroll(function () {
        console.log("Scroll triggered");
        console.log("Current Waypoint: " + currentWaypoint);
        var curTop = $(window).scrollTop();
        if (top < curTop) {
            if (currentWaypoint < 4) {
                previousWaypoint = currentWaypoint;
                currentWaypoint=currentWaypoint+1;
            }
        }
        else {
            if (currentWaypoint > 0) {
                previousWaypoint = currentWaypoint;
                currentWaypoint=currentWaypoint-1;
            }
        }
        top = curTop;
        if (previousWaypoint != currentWaypoint) {
            switch (currentWaypoint) {
            case 1:
                $.scrollTo(document.getElementById("waypoint-collection"));
            case 2:
                $.scrollTo(document.getElementById("waypoint-report"));
            case 3:
                $.scrollTo(document.getElementById("waypoint-video"));
            case 4:
                $.scrollTo(document.getElementById("waypoint-mail"));
            default:
            }
        }
        console.log("New Waypoint: " + currentWaypoint);
    });
});

我在一些网站上看到过这种行为,但似乎找不到与谷歌相关的任何内容。有什么想法吗?

编辑:

相关HTML代码:

<html>
<head>
    <style>
        body {
            background-color: #FFFFFF;
        }

        .features-container-wh {
            min-height: 12.5rem;
            text-align: center;
            height: 100%;
        }

        .features-container-bl {
            background-color: #43bfcb;
            text-align: center;
            color: #FFFFFF;
            height: 100%;
        }

        .features-container-bottom {
            height: 100%;
        }
    </style>
</head>
<body>
    <div class="features-container-wh">
        <a href="#waypoint-collection">Collection</a>
        <a href="#waypoint-report">Report</a>
        <a href="#waypoint-video">Video</a>
        <a href="#waypoint-mail">Mail</a>
    </div>
    <div class="features-container-bl" id="waypoint-collection">
        <p>Stuff...</p>
    </div>
    <div class="features-container-wh" id="waypoint-report">
        <p>Stuff...</p>
    </div>
    <div class="features-container-bl" id="waypoint-video">
        <p>Stuff...</p>
    </div>
    <div class="features-container-bottom" id="waypoint-mail">
        <p>Stuff...</p>
    </div>
</body>

最佳答案

$('html, body').animate({scrollTop: $("#.wayPoint1").offset().top}, 2000);

关于javascript - 如何在 jQuery 中从一个航点滚动到另一个航点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38174419/

相关文章:

javascript - 从输入文本中获取属性值

jquery - 在可折叠的 jQuery 中对齐图像和文本

javascript - 查询 : Hide first row of all the tables except first table

javascript - 如果已选中复选框,则使用 JavaScript 更改 CSS 可见性

javascript - 如何嵌套一组项目,将它们除以第一个值?

jquery - 以编程方式在打开模式和 CSS 时首先在选项卡上设置步骤 1 - Accordion 滑动流畅

javascript - 展开折叠图标更改 Bootstrap CSS

html - 如何自定义 bootstrap 下拉列表?

javascript - 从输入表单获取文本返回空字符串,但 console.log 显示显示文本

javascript - Chrome未捕获范围错误: Maximum call stack size exceeded click recursion