Reset scrollTo() data

148 Views Asked by At

I have some weird problem. I'm using scrollTo() plugin for scrolling my page. I have up and down arrows for vertical scroll and left and right for horizontal scroll. Problem is, when I click on right arrow, it scrolls to right side, then if I click on down arrow, it first goes to left section, then goes down and then it goes right again. But it should scroll only down. As you can see in my code, I reinitalize call() function, so I believe scrollTo() remembers previous position and then follows that previous path.

So my question is, how to reset scrollTo() data when I call that function again.

Html

<section class="main">
        <section class="section0">
            <img src="1-1.jpg">
        </section>

        <section class="section0">
            <img src="1-2.jpg">
        </section>

        <section class="section0">
            <img src="1-3.jpg">
        </section>
    </section>

    <section class="main">
        <section class="section1">
            <img src="2-1.jpg">
        </section>

        <section class="section1">
            <img src="2-2.jpg">
        </section>

        <section class="section1">
            <img src="2-3.jpg">
        </section>
    </section>

Jquery

$(document).ready(function(){
    call(0);
});

function scrollToPosition(element) {
    if (element !== undefined) {
        $("html").scrollTo(element, 800, {
            margin: true
        });
    }
}
var counter = 0;
var call = function(counter) {

    var sections = $('.section'+counter);
    var position = 0;
    var next = $('#up');
    var prev = $('#down').hide();

    next.click(function(evt) {
        prev.show();
        scrollToPosition(sections[position += 1]);
        if (position === sections.length - 1) {
            next.hide();
        }
    });

    prev.click(function(evt) { 
        next.show();
        scrollToPosition(sections[position -= 1]);
        if (position === 0) {
            prev.hide();
        }
    });

}

$(function() {

    var sections = $('.main');
    var position = 0;
    var next = $('#right');
    var prev = $('#left').hide();

    next.click(function(evt) {
        counter++;
        call(counter);
        prev.show();
        scrollToPosition(sections[position += 1]);
        if (position === sections.length - 1) {
            next.hide();
        }
    });

    prev.click(function(evt) { 
        counter--;
        call(counter);
        next.show();
        scrollToPosition(sections[position -= 1]);
        if (position === 0) {
            prev.hide();
        }
    });

});
0

There are 0 best solutions below