mmdwc
mmdwc

Reputation: 1137

scroll top when clicking on a div, then scroll bottom if scroll is on top

I'm using a Jquery code on my website. when clicking on a div (.bottom), my page scrolls to top. it works perfectly fine, here is my code :

function scroll_cartouche(){
    $('.bottom').click(function() {
  $("html, body").animate({ scrollTop: $(document).height() }, 500);
});

now that my page has scrolled top top, I would like when clicking on the .bottom div again, to scroll to bottom (return at the initial position)

here is the code I tried but I have a bug :

function scroll_cartouche(){
    $(window).scroll(function () {
        if ($(this).scrollTop() != 0) {
            $('.bottom').click(function() {
                $("html, body").animate({ scrollTop: $(document).height() }, 500);
            });
        } else {
            $('.bottom').click(function() {
            $("html, body").animate({ scrollTop: 0 }, 500);
            });
        }
    });
}

can anybody help me with this ? thanks a lot,

Upvotes: 0

Views: 704

Answers (1)

cforcloud
cforcloud

Reputation: 589

do something like

$('.bottom').click(function() {
    if ($(window).scrollTop() == 0) {
        $("html, body").animate({ scrollTop: $(document).height() }, 500);
    } else {
        $("html, body").animate({ scrollTop: 0 }, 500);
    }
});

Upvotes: 1

Related Questions