Scroll to Next DIV using jQuery

2019-06-23 23:33发布

问题:

I have a page template like this:

<div class="page-wrapper">
    <header class="header-5">
        <div class="container">
           content
        </div>
        <div class="background"></div>
    </header>

<section class="content-3">
    <div class="row">
        <div class="col-sm-8 col-sm-offset-2 text-center">
            content
            <a class="control-btn" href="#"> </a>
        </div>
    </div>
    <div class="container">

When <a class="control-btn" href="#"> </a> is clicked, I want it to scroll to the last <div class="container"> (the last line in the code snippet).

With this jQuery, it scrolls to the first div.container (toward the top of the snippet):

 $('.control-btn').on('click', function() {
        $.scrollTo($(".container"), {
            axis : 'y',
            duration : 500
        });
        return false;
    });

How can I get it to scroll to the next div.container (the last one in my code example)?

Note: There are other div.container elements further down -- this isn't the "last" one on the page.

回答1:

$('.control-btn').on('click', function () {
    var ele = $(this).closest("section").find(".container");
    // this will search within the section
    $("html, body").animate({
         scrollTop: $(ele).offset().top
    }, 100);
    return false;
});

DEMO



标签: jquery scroll