Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scroll to Next DIV using jQuery

Tags:

jquery

scroll

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.

like image 886
okoboko Avatar asked Jun 15 '14 05:06

okoboko


1 Answers

$('.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

like image 131
mohamedrias Avatar answered Sep 18 '22 19:09

mohamedrias